【问题标题】:Android - How to detect transparency of the clicked area of custom shaped buttonsAndroid - 如何检测自定义形状按钮的点击区域的透明度
【发布时间】:2012-09-21 12:55:39
【问题描述】:

我有一些形状不规则的按钮,创建为 ImageButtons。 ImageButtons 的“android:src”属性是具有透明背景的 .PNG 文件。并且这些 ImageButtons 的父布局有一个自定义背景图像,它是用“android:background”属性定义的。所以活动的背景不是透明的或者只是黑色的。

我的问题是;如何检测单击按钮是在源图像的透明区域还是在源图像的可见部分?

我尝试使用onTouchListener来获取事件的坐标,并根据像素的颜色做出决定;但由于背景是彩色的,我无法理解。

非常感谢任何帮助。提前致谢!

【问题讨论】:

    标签: android button transparent shape


    【解决方案1】:

    您好,兄弟,我认为此链接可能对您有所帮助。

    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

    如果点不在所需区域中,您需要在自定义按钮中覆盖此方法以返回 false。我建议你这样做:

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

    【讨论】:

    • 您需要在自定义按钮中重写此方法以在点不在所需区域时返回 false。我建议你这样做: public static class MyButton extends ImageButton { ... @Override public boolean dispatchTouchEvent(MotionEvent event) { int iX = (int) event.getX(); int iY = (int) event.getY(); // TODO 或者使用更复杂的基于像素值的条件 if (!(iX>=0 & iY>=0 & iX
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2012-12-01
    • 2016-08-16
    相关资源
    最近更新 更多