Add "Picasso" library; show header images in wikivoyage search list

This commit is contained in:
Alex Sytnyk 2018-04-03 15:25:20 +03:00
parent de7d091724
commit b651b913c0
5 changed files with 97 additions and 10 deletions

View file

@ -414,6 +414,8 @@ dependencies {
compile 'com.moparisthebest:junidecode:0.1.1' compile 'com.moparisthebest:junidecode:0.1.1'
compile 'org.immutables:gson:2.5.0' compile 'org.immutables:gson:2.5.0'
compile 'com.vividsolutions:jts-core:1.14.0' compile 'com.vividsolutions:jts-core:1.14.0'
compile 'com.squareup.picasso:picasso:2.71828'
// size restrictions // size restrictions
// compile 'com.ibm.icu:icu4j:50.1' // compile 'com.ibm.icu:icu4j:50.1'
// compile 'net.sf.trove4j:trove4j:3.0.3' // compile 'net.sf.trove4j:trove4j:3.0.3'

View file

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<item>
<shape android:shape="oval">
<solid android:color="@color/color_transparent"/>
<size
android:width="28dp"
android:height="28dp"/>
</shape>
</item>
<item
android:id="@+id/placeholder_icon"
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<bitmap
android:gravity="center"
android:src="@drawable/ic_action_placeholder_city"
android:tint="@color/icon_color"/>
</item>
</layer-list>

View file

@ -22,8 +22,8 @@
<ImageView <ImageView
android:id="@+id/icon" android:id="@+id/icon"
android:layout_width="wrap_content" android:layout_width="28dp"
android:layout_height="wrap_content" android:layout_height="28dp"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginEnd="@dimen/content_padding" android:layout_marginEnd="@dimen/content_padding"
android:layout_marginRight="@dimen/content_padding" android:layout_marginRight="@dimen/content_padding"

View file

@ -0,0 +1,49 @@
package net.osmand.plus.widgets.tools;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import com.squareup.picasso.Transformation;
public class CropCircleTransformation implements Transformation {
private static final String KEY = "CropCircleTransformation";
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int width = (source.getWidth() - size) / 2;
int height = (source.getHeight() - size) / 2;
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader =
new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
if (width != 0 || height != 0) {
// source isn't square, move viewport to center
Matrix matrix = new Matrix();
matrix.setTranslate(-width, -height);
shader.setLocalMatrix(matrix);
}
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
source.recycle();
return bitmap;
}
@Override
public String key() {
return KEY;
}
}

View file

@ -1,7 +1,10 @@
package net.osmand.plus.wikivoyage.search; package net.osmand.plus.wikivoyage.search;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -9,9 +12,12 @@ import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.plus.IconsCache; import com.squareup.picasso.Picasso;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.widgets.tools.CropCircleTransformation;
import net.osmand.plus.wikivoyage.data.WikivoyageArticle;
import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult; import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult;
import java.util.ArrayList; import java.util.ArrayList;
@ -23,7 +29,7 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
private static final int ITEM_TYPE = 1; private static final int ITEM_TYPE = 1;
private OsmandApplication app; private OsmandApplication app;
private IconsCache iconsCache; private LayerDrawable placeholder;
private List<Object> items = new ArrayList<>(); private List<Object> items = new ArrayList<>();
@ -35,7 +41,11 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
SearchRecyclerViewAdapter(OsmandApplication app) { SearchRecyclerViewAdapter(OsmandApplication app) {
this.app = app; this.app = app;
this.iconsCache = app.getIconsCache(); placeholder = (LayerDrawable) ContextCompat.getDrawable(app, R.drawable.wikivoyage_search_placeholder);
if (Build.VERSION.SDK_INT < 21 && placeholder != null) {
placeholder.setDrawableByLayerId(R.id.placeholder_icon,
app.getIconsCache().getIcon(R.drawable.ic_action_placeholder_city, R.color.icon_color));
}
} }
@NonNull @NonNull
@ -60,9 +70,11 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
boolean lastItem = pos == getItemCount() - 1; boolean lastItem = pos == getItemCount() - 1;
WikivoyageSearchResult item = (WikivoyageSearchResult) getItem(pos); WikivoyageSearchResult item = (WikivoyageSearchResult) getItem(pos);
holder.icon.setImageDrawable( Picasso.get()
iconsCache.getIcon(R.drawable.ic_action_placeholder_city, R.color.icon_color) .load(WikivoyageArticle.getThumbImageUrl(item.getImageTitle()))
); .transform(new CropCircleTransformation())
.placeholder(placeholder)
.into(holder.icon);
holder.title.setText(item.getArticleTitles().get(0)); holder.title.setText(item.getArticleTitles().get(0));
holder.leftDescr.setText(item.getIsPartOf()); holder.leftDescr.setText(item.getIsPartOf());
holder.rightDescr.setText(item.getFirstLangsString()); holder.rightDescr.setText(item.getFirstLangsString());
@ -102,7 +114,7 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
final TextView title; final TextView title;
public HeaderVH(View itemView) { HeaderVH(View itemView) {
super(itemView); super(itemView);
title = (TextView) itemView.findViewById(R.id.title); title = (TextView) itemView.findViewById(R.id.title);
} }
@ -117,7 +129,7 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
final View divider; final View divider;
final View shadow; final View shadow;
public ItemVH(View itemView) { ItemVH(View itemView) {
super(itemView); super(itemView);
icon = (ImageView) itemView.findViewById(R.id.icon); icon = (ImageView) itemView.findViewById(R.id.icon);
title = (TextView) itemView.findViewById(R.id.title); title = (TextView) itemView.findViewById(R.id.title);