1 package com.davemorrissey.labs.subscaleview.test.extension.views; 2 3 import android.content.Context; 4 import android.graphics.*; 5 import android.util.AttributeSet; 6 import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; 7 import com.davemorrissey.labs.subscaleview.test.R.drawable; 8 9 10 public class PinView extends SubsamplingScaleImageView { 11 12 private final Paint paint = new Paint(); 13 private final PointF vPin = new PointF(); 14 private PointF sPin; 15 private Bitmap pin; 16 PinView(Context context)17 public PinView(Context context) { 18 this(context, null); 19 } 20 PinView(Context context, AttributeSet attr)21 public PinView(Context context, AttributeSet attr) { 22 super(context, attr); 23 initialise(); 24 } 25 setPin(PointF sPin)26 public void setPin(PointF sPin) { 27 this.sPin = sPin; 28 initialise(); 29 invalidate(); 30 } 31 initialise()32 private void initialise() { 33 float density = getResources().getDisplayMetrics().densityDpi; 34 pin = BitmapFactory.decodeResource(this.getResources(), drawable.pushpin_blue); 35 float w = (density/420f) * pin.getWidth(); 36 float h = (density/420f) * pin.getHeight(); 37 pin = Bitmap.createScaledBitmap(pin, (int)w, (int)h, true); 38 } 39 40 @Override onDraw(Canvas canvas)41 protected void onDraw(Canvas canvas) { 42 super.onDraw(canvas); 43 44 // Don't draw pin before image is ready so it doesn't move around during setup. 45 if (!isReady()) { 46 return; 47 } 48 49 paint.setAntiAlias(true); 50 51 if (sPin != null && pin != null) { 52 sourceToViewCoord(sPin, vPin); 53 float vX = vPin.x - (pin.getWidth()/2); 54 float vY = vPin.y - pin.getHeight(); 55 canvas.drawBitmap(pin, vX, vY, paint); 56 } 57 58 } 59 60 } 61