如何在 PNG 加载的 ImageView 的透明部分防止 onClick 方法 [英] How to prevent onClick method on transparent portion of a PNG-loaded ImageView

查看:9
本文介绍了如何在 PNG 加载的 ImageView 的透明部分防止 onClick 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 Android 应用程序,该应用程序显示多个图像(如 ImageView 的)堆叠在一起.以下是当前层的配置方式:

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 configured:

  • 背景层:缩放整个屏幕,必须是可点击的
  • 前景层:缩放整个屏幕,必须是可点击的,包含透明度,允许用户看到一些背景层

我面临的问题是前景层.我将 onClick() 方法分配给图像视图,但是无论它们是否击中可见的图像部分以及包含透明度的部分,都会调用该方法.我只希望在用户单击该图像视图的不透明部分时调用前景 ImageView onClick() 方法.

The problem I face is with the foreground layer. I am assigning the onClick() method to the imageview, but the method is being called whether they hit the portion of the image which is visible as well as the part which contains transparency. I only want the foreground ImageView onClick() method to be called when the user clicks a portion of that imageview that is not transparent.

这就是场景的样子:

对角线表示前景图像的透明部分.如果用户触摸这个空间,我希望它访问背景图像而不是前景图像.感谢您提供的任何帮助.

The diagonal lines represent the transparent portion of the Foreground image. If a user touches this space, I want it to access the Background image instead of the Foreground image. Thank you for any assistance you can provide.

这是我实施的解决方案(感谢下面的回答):

//ontouchlistener - gets X and Y from event
private void setClick(View view)
{
    view.setOnTouchListener(new View.OnTouchListener() 
    {
        public boolean onTouch(View v, MotionEvent event) 
        {
            int imageId = getImageId((int)event.getX(), (int)event.getY());
            if (imageId >= 0)
                performActions(imageId);
            return false;
        }
    });
}

//get the ID of the first imageview (starting from foreground, 
//working backwards) which contains a non-transparent pixel
private int getImageId(int x, int y)
{
    ViewGroup parent = (ViewGroup) findViewById(R.id.relative_layout);
    for (int a = parent.getChildCount()-1; a >= 0; a--)
    {
        if (parent.getChildAt(a) instanceof ImageView)
            if (!checkPixelTransparent((ImageView)parent.getChildAt(a), x, y))
                return parent.getChildAt(a).getId();
    }
    return -1;
}

//get bitmap from imageview, get pixel from x, y coord
//check if pixel is transparent
private boolean checkPixelTransparent(ImageView iv, int x, int y)
{
    Bitmap bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();
    if (Color.alpha(bitmap.getPixel(x, y)) == 0)
        return true;
    else
        return false;
}

推荐答案

这个一个示例 使 ImageView 的透明区域不可点击.

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

图像视图:

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;
        }
    }
};

这篇关于如何在 PNG 加载的 ImageView 的透明部分防止 onClick 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆