How to prevent onClick method on transparent portion of a PNG-loaded ImageView

前端 未结 3 498
北荒
北荒 2020-12-02 21:23

I am currently developing an Android app that displays multiple images (as ImageView\'s) stacked on top of each other. Here is how the layers are currently con

3条回答
  •  攒了一身酷
    2020-12-02 21:51

    This one sample makes ImageView's transparent area not clickable.

    ImageView:

    ImageView imgView= (ImageView) findViewById(R.id.color_blue);
    imgView.setDrawingCacheEnabled(true);
    imgView.setOnTouchListener(changeColorListener);
    

    OnTouchListener:

    private final OnTouchListener changeColorListener = new OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
            int color = bmp.getPixel((int) event.getX(), (int) event.getY());
            if (color == Color.TRANSPARENT)
                return false;
            else {
                //code to execute
                return true;
            }
        }
    };
    

提交回复
热议问题