我想在更改背景图片时从我的PinView中删除所有引脚。 我通过将它们插入坐标ArrayList和pin图像来添加引脚,一切正常。
我该怎么办?
答案 0 :(得分:0)
您提到您已经在arrayList中拥有了Points。使用下面的setPin
函数(它还需要一个" name"对于这一点,你可以简单地使用String.valueOf(//you index);
作为名称从arrayList添加点的索引作为名称销)。现在,当您想要删除点时,只需为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;
}
}