简体   繁体   English

Android-如何检测自定义形状按钮的单击区域的透明度

[英]Android - How to detect transparency of the clicked area of custom shaped buttons

I have some irregular shaped buttons, created as ImageButtons. 我有一些不规则形状的按钮,创建为ImageButtons。 The "android:src" attribute of the ImageButtons are .PNG files with transparent backgrounds. ImageButton的“ android:src”属性是具有透明背景的.PNG文件。 And the parent layout of these ImageButtons, has a custom background image, which is defined with "android:background" attribute. 这些ImageButton的父级布局具有自定义背景图片,该图片由“ android:background”属性定义。 So the background of the activity is not transparent or just black. 因此,活动的背景不是透明的,或者仅仅是黑色的。

My question is; 我的问题是; how can I detect if a click on a button is on the transparent area of the source image, or on the visible part of the source image? 如何检测单击按钮是在源图像的透明区域还是在源图像的可见部分?

I tried using onTouchListener to get the coordinates of the event and make a decision according to the color of the pixel; 我尝试使用onTouchListener来获取事件的坐标并根据像素的颜色做出决定; but since the background is colorful, I couldn't get to a point. 但是由于背景是彩色的,所以我讲不清楚。

Any help is very much appreciated. 很感谢任何形式的帮助。 Thanks in advance! 提前致谢!

Hi bro i think this link may help you. 嗨,兄弟,我认为此链接可能会对您有所帮助。

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/view/View.java#View.dispatchTouchEvent%28android.view.MotionEvent%29 http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/view/View.java#View.dispatchTouchEvent%28android.view.MotionEvent%29

You need to override this method in your custom button to return false if point is not in the desired area. 如果点不在所需区域中,则需要在自定义按钮中覆盖此方法以返回false。 I suggest you go about it like this: 我建议您这样处理:

public static class MyButton extends ImageButton {
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int iX = (int) event.getX();
        int iY = (int) event.getY();

        // TODO Or use a more sophisticated, pixel value-based condition
        if (!(iX >= 0 & iY >= 0 & iX < TheBitmap.getWidth() & iY < TheBitmap.getHeight())) {
            return false;
        }
        return super.dispatchTouchEvent(event)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM