Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
deeeb7e3a2
1 changed files with 26 additions and 57 deletions
|
@ -1,61 +1,48 @@
|
|||
package net.osmand.plus.base;
|
||||
|
||||
import java.util.TreeMap;
|
||||
|
||||
import net.osmand.plus.R;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Paint.Style;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.PorterDuffColorFilter;
|
||||
import android.graphics.Rect;
|
||||
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 {
|
||||
|
||||
private int color;
|
||||
Paint paintInnerCircle;
|
||||
private Paint paintIcon;
|
||||
private Paint paintBackground;
|
||||
private Bitmap favIcon;
|
||||
private Bitmap favBackground;
|
||||
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.color = color;
|
||||
WindowManager mgr = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
|
||||
this.density = d;
|
||||
if (this.density == 0) {
|
||||
DisplayMetrics dm = new DisplayMetrics();
|
||||
mgr.getDefaultDisplay().getMetrics(dm);
|
||||
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);
|
||||
paintIcon = new Paint();
|
||||
paintIcon.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
|
||||
paintBackground = new Paint();
|
||||
favIcon = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_action_fav_dark);
|
||||
favBackground = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.map_white_shield);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntrinsicHeight() {
|
||||
return (int) (24 * density);
|
||||
// return (int) (drawable.getIntrinsicHeight() + 8 * density);
|
||||
return favBackground.getHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntrinsicWidth() {
|
||||
return (int) (24 * density);
|
||||
// return (int) (drawable.getIntrinsicWidth() + 8 * density);
|
||||
return favBackground.getWidth();
|
||||
}
|
||||
|
||||
public int getColor() {
|
||||
|
@ -66,27 +53,11 @@ public class FavoriteImageDrawable extends Drawable {
|
|||
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
|
||||
public void draw(Canvas canvas) {
|
||||
// int max = Math.max(drawable.getMinimumHeight(), drawable.getMinimumWidth());
|
||||
Rect bs = getBounds();
|
||||
int min = Math.min(bs.width(), bs.height());
|
||||
int r = (int) (min / 2);
|
||||
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);
|
||||
canvas.drawBitmap(favBackground, bs.exactCenterX() - favBackground.getWidth() / 2f, bs.exactCenterY() - favBackground.getHeight() / 2f, paintBackground);
|
||||
canvas.drawBitmap(favIcon, bs.exactCenterX() - favIcon.getWidth() / 2f, bs.exactCenterY() - favIcon.getHeight() / 2f, paintIcon);
|
||||
}
|
||||
|
||||
public void drawBitmapInCenter(Canvas canvas, int x, int y) {
|
||||
|
@ -104,27 +75,25 @@ public class FavoriteImageDrawable extends Drawable {
|
|||
|
||||
@Override
|
||||
public void setAlpha(int alpha) {
|
||||
paintInnerCircle.setAlpha(alpha);
|
||||
|
||||
paintBackground.setAlpha(alpha);
|
||||
}
|
||||
|
||||
@Override
|
||||
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) {
|
||||
color = color | 0xff000000;
|
||||
int hash = (color << 2) + (int) (density * 6);
|
||||
FavoriteImageDrawable drawable = cache.get(hash);
|
||||
if (drawable == null) {
|
||||
drawable = new FavoriteImageDrawable(a, color, density);
|
||||
drawable = new FavoriteImageDrawable(a, color);
|
||||
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
|
||||
cache.put(hash, drawable);
|
||||
}
|
||||
return drawable;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue