| package com.davemorrissey.labs.subscaleview.test.extension.views; |
| |
| import android.content.Context; |
| import android.graphics.*; |
| import android.util.AttributeSet; |
| import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; |
| import com.davemorrissey.labs.subscaleview.test.R.drawable; |
| |
| |
| public class PinView extends SubsamplingScaleImageView { |
| |
| private final Paint paint = new Paint(); |
| private final PointF vPin = new PointF(); |
| private PointF sPin; |
| private Bitmap pin; |
| |
| public PinView(Context context) { |
| this(context, null); |
| } |
| |
| public PinView(Context context, AttributeSet attr) { |
| super(context, attr); |
| initialise(); |
| } |
| |
| public void setPin(PointF sPin) { |
| this.sPin = sPin; |
| initialise(); |
| invalidate(); |
| } |
| |
| private void initialise() { |
| float density = getResources().getDisplayMetrics().densityDpi; |
| pin = BitmapFactory.decodeResource(this.getResources(), drawable.pushpin_blue); |
| float w = (density/420f) * pin.getWidth(); |
| float h = (density/420f) * pin.getHeight(); |
| pin = Bitmap.createScaledBitmap(pin, (int)w, (int)h, true); |
| } |
| |
| @Override |
| protected void onDraw(Canvas canvas) { |
| super.onDraw(canvas); |
| |
| // Don't draw pin before image is ready so it doesn't move around during setup. |
| if (!isReady()) { |
| return; |
| } |
| |
| paint.setAntiAlias(true); |
| |
| if (sPin != null && pin != null) { |
| sourceToViewCoord(sPin, vPin); |
| float vX = vPin.x - (pin.getWidth()/2); |
| float vY = vPin.y - pin.getHeight(); |
| canvas.drawBitmap(pin, vX, vY, paint); |
| } |
| |
| } |
| |
| } |