ImageView有两个图像和两个onClickListeners?

时间:2016-12-20 14:20:18

标签: android imageview onclicklistener

我有imageview,里面有两张图片:

    Resources r = getResources();
    Drawable[] layers = new Drawable[2];
    layers[0] = r.getDrawable(R.drawable.image1);
    layers[1] = r.getDrawable(R.drawable.image2);
    LayerDrawable layerDrawable = new LayerDrawable(layers);
    imageView.setImageDrawable(layerDrawable);

现在,我想在此onClickListener的每个图片上添加imageView。我怎么解决这个问题?此外,他们不重叠。

谢谢。

---编辑---

现在,在我的情况下,我有两个这样的三角形:

Imageview with two images

我想检测左下角和右上三角形的三角形点击。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

请勿使用OnClickListener,请使用OnTouchListener。测量的位置 触摸事件并使用它来确定点击发生的位置。

imageButton.setOnTouchListener(new OnTouchListener() {
    long lastPress = 0L
    final long CLICK_DURATION = 500L
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            lastPress = System.currrentTimeMillis();
            return true;
        } if(event.getAction() == MotionEvent.ACTION_UP){

            //The time between pressing and releasing has to be below a
            //certain treshold to be a valid click.
            if (System.currentTimeMillis() - lastPress < CLICK_DURATION) {
                int x = (int) event.getX();
                int y = (int) event.getY();

                // Determine which triangle got clicked and then execute
                // the code that would go in an onClickListener.
            }
            return true;
        }
        return false;
    }
}