0
我想在更改背景圖像時從我的PinView刪除所有針腳。 我通過將它們插入座標ArrayList和引腳圖像來添加引腳,並且一切正常。subsamplingscaleimageview清潔針圖像
我該怎麼辦?
我想在更改背景圖像時從我的PinView刪除所有針腳。 我通過將它們插入座標ArrayList和引腳圖像來添加引腳,並且一切正常。subsamplingscaleimageview清潔針圖像
我該怎麼辦?
你提到你已經有了一個arrayList中的Points。從下面使用setPin
函數(它也需要一個「名稱」的點,你可以簡單地添加arrayList的點索引作爲名稱使用String.valueOf(//you index);
作爲引腳的名稱)。現在,當你想刪除點時,只需爲arrayList運行一個簡單的for循環,並刪除所有元素。試一試。
P.S.該代碼還包含其他一些功能,如果您不需要,可隨時刪除它們。
public class PinView extends SubsamplingScaleImageView {
private ArrayList<PointF> sPin = new ArrayList<>();
private ArrayList<String> pinNames = new ArrayList<>();
private Bitmap pin;
private final float scale=1.5f;
public PinView(Context context) {
this(context, null);
}
public PinView(Context context, AttributeSet attr) {
super(context, attr);
initialise();
}
public boolean setPin(PointF sPin, String name) {
if (pinNames.contains(name)){
return false;
} else {
this.sPin.add(sPin);
pinNames.add(name);
initialise();
invalidate();
return true;
}
}
public PointF getPin(String name) {
return sPin.get(pinNames.indexOf(name));
}
public boolean removePin(String name){
if (pinNames.contains(name)){
sPin.remove(pinNames.indexOf(name));
pinNames.remove(name);
System.out.println("Removed pin named : "+name);
initialise();
invalidate();
return true;
} else {
System.out.println("No such pins : "+name);
initialise();
invalidate();
return false;
}
}
public ArrayList<String> getPinNames(){
return pinNames;
}
private void initialise() {
float density = getResources().getDisplayMetrics().densityDpi;
pin = BitmapFactory.decodeResource(this.getResources(), R.drawable.squarered);
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 paint = new Paint();
paint.setAntiAlias(true);
for (PointF point : sPin){
if (point != null && pin != null) {
PointF vPin = sourceToViewCoord(point);
float vX = vPin.x - (pin.getWidth()/2);
float vY = vPin.y - pin.getHeight();
canvas.drawBitmap(pin, vX, vY, paint);
}
}
}
public void doAnimationWithTime(PointF target)
{
//System.out.println("Width : " + this.getSWidth() + " height : " + this.getSHeight());
SubsamplingScaleImageView.AnimationBuilder animationBuilder = this.animateScaleAndCenter(scale, target);
animationBuilder.withDuration(700).withEasing(SubsamplingScaleImageView.EASE_OUT_QUAD).withInterruptible(true).start();
}
public boolean isPinVisibleOnImage(String name)
{
boolean result = false;
for(int i=0;i<pinNames.size();i++)
{
if(pinNames.get(i).equals(name.trim()))
{
result = true;
break;
}
}
return result;
}
}