如何使Imageview上的引脚标记可单击

时间:2015-10-12 04:24:52

标签: android bitmap uiimageview imageview subsampling

我正在使用Dave Morrissey的子采样比例图像视图。我正在使用他的Pinview示例(如下所示:https://github.com/davemorrissey/subsampling-scale-image-view/blob/master/sample/src/com/davemorrissey/labs/subscaleview/sample/extension/views/PinView.java

我所做的不同之处在于我创建了一个位列的ArrayList,它是Pins。但我想让每个引脚都可以点击以启动点击功能。我知道无法点击位图。我在地图图像上有多个引脚,并希望每个引脚与一个对象相关联。

实现这一目标的最佳方法是什么?

注意:我确实覆盖了Pinview类中的setOnClickListener方法,但是所有被删除的引脚都会与同一个对象相关联。然后清除1个引脚将清除所有引脚。

1 个答案:

答案 0 :(得分:1)

存储位图,pointF和点名称的模型:

public class CategoryPoint {
    private String category;
    private Bitmap image;
    private PointF pointF;

    public CategoryPoint(String category, Bitmap image, PointF pointF) {
        this.category = category;
        this.image = image;
        this.pointF = pointF;
    }

    // getters/setters
}

视图如下:

public class PinsView extends SubsamplingScaleImageView {
    private OnPinClickListener onPinClickListener;
    private final Paint paint = new Paint();
    private List<CategoryPoint> categoryPoints;

    public PinsView(Context context) {
        this(context, null);
    }

    public PinsView(Context context, AttributeSet attr) {
        super(context, attr);
        categoryPoints = new ArrayList<>();
        initTouchListener();
    }

    public void addCategories(List<CategoryPoint> categoryPoints) {
        this.categoryPoints = categoryPoints;
        invalidate();
    }

    public void removeCategories(List<CategoryPoint> categoryPoints) {
        this.categoryPoints.removeAll(categoryPoints);
        invalidate();
    }

    public void removeAllCategories() {
        this.categoryPoints.clear();
        invalidate();
    }

    public void setOnPinClickListener(OnPinClickListener listener) {
        onPinClickListener = listener;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isReady()) {
            return;
        }
        paint.setAntiAlias(true);
        for (CategoryPoint categoryPoint: categoryPoints) {
            Bitmap pinIcon = categoryPoint.getImage();
            if (categoryPoint.getPointF() != null && categoryPoint.getImage() != null) {
                PointF point = sourceToViewCoord(categoryPoint.getPointF());
                float vX = point.x - (pinIcon.getWidth()/2);
                float vY = point.y - pinIcon.getHeight();
                canvas.drawBitmap(pinIcon, vX, vY, paint);
            }
        }
    }

    private void initTouchListener() {
        GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                if (isReady() && categoryPoints != null) {
                    PointF tappedCoordinate = new PointF(e.getX(), e.getY());
                    Bitmap clickArea = categoryPoints.get(0).getImage();
                    int clickAreaWidth = clickArea.getWidth();
                    int clickAreaHeight = clickArea.getHeight();
                    for (CategoryPoint categoryPoint : categoryPoints) {
                        PointF categoryCoordinate = sourceToViewCoord(categoryPoint.getPointF());
                        int categoryX = (int) (categoryCoordinate.x);
                        int categoryY = (int) (categoryCoordinate.y - clickAreaHeight / 2);
                        if (tappedCoordinate.x >= categoryX - clickAreaWidth / 2
                            && tappedCoordinate.x <= categoryX + clickAreaWidth / 2
                            && tappedCoordinate.y >= categoryY - clickAreaHeight / 2
                            && tappedCoordinate.y <= categoryY + clickAreaHeight / 2) {
                            onPinClickListener.onPinClick(categoryPoint);
                            break;
                        }
                    }
                }
                return true;
            }
        });
        setOnTouchListener((v, event) -> gestureDetector.onTouchEvent(event));
    }
}

片段:

pinView.setOnImageEventListener(this);
pinView.setOnPinClickListener(this);

// implementation