【问题标题】:How to handle custom view touch event only on opaque(visible) area如何仅在不透明(可见)区域处理自定义视图触摸事件
【发布时间】:2014-09-04 15:46:06
【问题描述】:

您好,我正在尝试仅在自定义视图的不透明(可见)区域检测触摸事件。例如,内部有透明区域的矩形视图和圆形视图(左下方屏幕)。但是根据Android doc,每个“视图”在屏幕上占据一个矩形区域(所以它的边界应该看起来像右边的屏幕,红色区域是透明区域)。

谁能指导我如何仅在用户单击视图的蓝色区域时检测触摸事件? 提前谢谢你。

【问题讨论】:

    标签: android android-custom-view touch-event


    【解决方案1】:

    如果您将形状绘制到Bitmap,并且在视图的单击事件中,获取该位置的颜色值,则这是可能的,如果 alpha 为 0,那么您可以不执行任何操作返回该函数,否则可以继续。

    我的一个应用程序是在一个视图中拖动多个图像,图像可以是任何形状,用户只有在触摸可见区域时才能拖动图像。所以我在这里发布我的代码块

    public boolean containsPoint(float scrnX, float scrnY) {
                // FIXME: need to correctly account for image rotation
                float[] XY = new float[] { scrnX, scrnY };
                Bitmap bitmap = getBimap();
                Matrix inv = new Matrix();
                mDrawMatrix.invert(inv);
                inv.mapPoints(XY);
                int x = Integer.valueOf((int) XY[0]);
                int y = Integer.valueOf((int) XY[1]);
                if (x < 0 || x > bitmap.getWidth() - 1 || y < 0 || y > bitmap.getHeight() - 1) {
                    // continue;
                    return false;
                }
    
                int col = bitmap.getPixel(x, y);
                if (Color.alpha(col) != Color.TRANSPARENT) {
                    return true;
                }
                return false;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多