我想在更改背景图像时从我的 PinView 中删除所有图钉。我通过将引脚插入坐标 ArrayList 和引脚图像来添加引脚,并且一切正常。
我能怎么做?
我想在更改背景图像时从我的 PinView 中删除所有图钉。我通过将引脚插入坐标 ArrayList 和引脚图像来添加引脚,并且一切正常。
我能怎么做?
您提到您已经在 arrayList 中有点。使用setPin
下面的函数(它还需要一个点的“名称”,您可以简单地从 arrayList 中添加点的索引作为名称,使用String.valueOf(//you index);
作为引脚的名称)。现在,当您想要删除这些点时,只需对 arrayList 运行一个简单的 for 循环,然后删除所有元素。试试看。
PS 代码还包含一些其他功能,如果您不需要,请随时删除它们。
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;
}
}