我已经扩展了SubsamplingScaleImageView以在文档中建议创建PinView。我能够在适当的位置绘制品脱。 但我面临的问题是:当我尝试绘制循环时,Pin会丢失。 我验证了OnDraw是在主线程上调用的。
以下是我正在使用的代码
public class PinView extends SubsamplingScaleImageView {
private PointF sPin;
private Bitmap pin;
private PointF sBlueDot;
private Bitmap blueDot;
public PinView(Context context) {
this(context, null);
}
public PinView(Context context, AttributeSet attr) {
super(context,attr);
initialise();
initialiseBlueDot()
}
public void setPin(PointF sPin) {
this.sPin = sPin;
initialise();
invalidate();
}
public void setBlueDot(PointF sPin) {
this.sBlueDot = sPin;
initialiseBlueDot();
invalidate();
}
public PointF getPin() {
return sPin;
}
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);
}
private void initialiseBlueDot() {
float density = getResources().getDisplayMetrics().densityDpi;
blueDot = BitmapFactory.decodeResource(this.getResources(), drawable.pushpin_blue);
float w = (density/420f) * blueDot.getWidth();
float h = (density/420f) * blueDot.getHeight();
blueDot = Bitmap.createScaledBitmap(blueDot, (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 paint = new Paint();
paint.setAntiAlias(true);
if (sPin != null && pin != null) {
PointF vPin = sourceToViewCoord(sPin);
float vX = vPin.x - (pin.getWidth()/2);
float vY = vPin.y - pin.getHeight();
canvas.drawBitmap(pin, vX, vY, paint);
}
if (sBlueDot != null && pin != null) {
PointF vPin = sourceToViewCoord(sBlueDot);
float vX = vPin.x - (blueDot.getWidth()/2);
float vY = vPin.y - blueDot.getHeight();
canvas.drawBitmap(blueDot, vX, vY, paint);
}
}
}
答案 0 :(得分:0)
我自己修好了。问题出在XY和源xy点上。使用viewToSourceCoord()来获取正确的位置点。