Merge remote-tracking branch 'origin/master'

This commit is contained in:
Weblate 2015-10-21 10:55:32 +02:00
commit deeeb7e3a2

View file

@ -1,61 +1,48 @@
package net.osmand.plus.base; package net.osmand.plus.base;
import java.util.TreeMap;
import net.osmand.plus.R;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter; import android.graphics.ColorFilter;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Paint.Style; import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.WindowManager; import net.osmand.plus.R;
import java.util.TreeMap;
public class FavoriteImageDrawable extends Drawable { public class FavoriteImageDrawable extends Drawable {
private int color; private int color;
Paint paintInnerCircle; private Paint paintIcon;
private Paint paintBackground;
private Bitmap favIcon;
private Bitmap favBackground;
private Resources resources; private Resources resources;
private Paint paintOuter;
private Drawable drawable;
private float density;
public FavoriteImageDrawable(Context ctx, int color, float d) { public FavoriteImageDrawable(Context ctx, int color) {
this.resources = ctx.getResources(); this.resources = ctx.getResources();
this.color = color; this.color = color;
WindowManager mgr = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE); paintIcon = new Paint();
this.density = d; paintIcon.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
if (this.density == 0) { paintBackground = new Paint();
DisplayMetrics dm = new DisplayMetrics(); favIcon = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_action_fav_dark);
mgr.getDefaultDisplay().getMetrics(dm); favBackground = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.map_white_shield);
density = dm.density;
}
drawable = getResources().getDrawable(R.drawable.ic_action_fav_dark);
paintOuter = new Paint();
paintOuter.setAntiAlias(true);
paintOuter.setStyle(Style.FILL_AND_STROKE);
paintInnerCircle = new Paint();
paintInnerCircle.setStyle(Style.FILL_AND_STROKE);
paintOuter.setColor(color == 0 || color == Color.BLACK ? 0x88555555 : color);
paintInnerCircle.setColor(color == 0 || color == Color.BLACK ? getResources().getColor(R.color.color_favorite)
: color);
paintInnerCircle.setAntiAlias(true);
} }
@Override @Override
public int getIntrinsicHeight() { public int getIntrinsicHeight() {
return (int) (24 * density); return favBackground.getHeight();
// return (int) (drawable.getIntrinsicHeight() + 8 * density);
} }
@Override @Override
public int getIntrinsicWidth() { public int getIntrinsicWidth() {
return (int) (24 * density); return favBackground.getWidth();
// return (int) (drawable.getIntrinsicWidth() + 8 * density);
} }
public int getColor() { public int getColor() {
@ -66,27 +53,11 @@ public class FavoriteImageDrawable extends Drawable {
return resources; return resources;
} }
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
Rect bs = new Rect(bounds);
bs.inset((int) (4 * density), (int) (4 * density));
// int min = Math.min(bounds.width(), bounds.height());
// bs.inset((int)(bs.width() - min + 3 * density) / 2,
// (int) (bs.height() - min + 3 * density) / 2);
drawable.setBounds(bs);
}
@Override @Override
public void draw(Canvas canvas) { public void draw(Canvas canvas) {
// int max = Math.max(drawable.getMinimumHeight(), drawable.getMinimumWidth());
Rect bs = getBounds(); Rect bs = getBounds();
int min = Math.min(bs.width(), bs.height()); canvas.drawBitmap(favBackground, bs.exactCenterX() - favBackground.getWidth() / 2f, bs.exactCenterY() - favBackground.getHeight() / 2f, paintBackground);
int r = (int) (min / 2); canvas.drawBitmap(favIcon, bs.exactCenterX() - favIcon.getWidth() / 2f, bs.exactCenterY() - favIcon.getHeight() / 2f, paintIcon);
int rs = (int) (min / 2 - 1);
canvas.drawCircle(min / 2, min / 2, r, paintOuter);
canvas.drawCircle(min / 2, min / 2, rs, paintInnerCircle);
drawable.draw(canvas);
} }
public void drawBitmapInCenter(Canvas canvas, int x, int y) { public void drawBitmapInCenter(Canvas canvas, int x, int y) {
@ -104,27 +75,25 @@ public class FavoriteImageDrawable extends Drawable {
@Override @Override
public void setAlpha(int alpha) { public void setAlpha(int alpha) {
paintInnerCircle.setAlpha(alpha); paintBackground.setAlpha(alpha);
} }
@Override @Override
public void setColorFilter(ColorFilter cf) { public void setColorFilter(ColorFilter cf) {
paintInnerCircle.setColorFilter(cf); paintIcon.setColorFilter(cf);
} }
private static TreeMap<Integer, FavoriteImageDrawable> cache = new TreeMap<Integer, FavoriteImageDrawable>(); private static TreeMap<Integer, FavoriteImageDrawable> cache = new TreeMap<>();
public static FavoriteImageDrawable getOrCreate(Context a, int color, float density) { public static FavoriteImageDrawable getOrCreate(Context a, int color, float density) {
color = color | 0xff000000; color = color | 0xff000000;
int hash = (color << 2) + (int) (density * 6); int hash = (color << 2) + (int) (density * 6);
FavoriteImageDrawable drawable = cache.get(hash); FavoriteImageDrawable drawable = cache.get(hash);
if (drawable == null) { if (drawable == null) {
drawable = new FavoriteImageDrawable(a, color, density); drawable = new FavoriteImageDrawable(a, color);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
cache.put(hash, drawable); cache.put(hash, drawable);
} }
return drawable; return drawable;
} }
} }