subasa
subasa

Reputation: 289

Ignore click on transparent part of the ImageView

I have 2 ImageViews which overlap each other, both contain an image with some transparent parts (PNG). When I click in the transparent part of the imageView, the eventListener is called for this imageView. I would like the call the listener only when a non-transparent part is clicked! This way it becomes possible to click 'through' the imageView and possibly call the listener of the imageView which is behind.

Upvotes: 3

Views: 1702

Answers (1)

Hiren Patel
Hiren Patel

Reputation: 52810

Take imageview and bind it with view, set drawing cache enable true ImageView:

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

OnTouchListener of Imageview:

private 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 {
            //click portion without transparent color
            return true;
        }
    }
};

Upvotes: 3

Related Questions