Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android (安卓布局)将按钮放置在不规则形状图像的顶部或获取图像特定区域的触摸检测(可触摸区域)的解决方案?_Android_Android Layout_Button_Touch - Fatal编程技术网

Android (安卓布局)将按钮放置在不规则形状图像的顶部或获取图像特定区域的触摸检测(可触摸区域)的解决方案?

Android (安卓布局)将按钮放置在不规则形状图像的顶部或获取图像特定区域的触摸检测(可触摸区域)的解决方案?,android,android-layout,button,touch,Android,Android Layout,Button,Touch,在我的Android应用程序中,我在全球有三个不规则形状,如下所示: <ImageView android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/glob2" android:layout_marginBo

在我的Android应用程序中,我在全球有三个不规则形状,如下所示:

  <ImageView
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/glob2"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="80.5dp"
        android:contentDescription="@string/profile_picture_description"
        android:src="@drawable/pick_matches_background" />

所以我把这整个图像放在了布局中。
地球仪只是一个图像。这里并没有地球仪的角色。
现在我想在地球上每个不规则形状的图像顶部放置一个按钮

按钮始终是矩形的,但
我想获得不规则形状的触摸事件,而不是矩形区域的触摸事件。
那么我如何在这三个不规则形状上放置三个按钮,或者有什么解决方案?所以每个按钮的不同事件可以在活动中处理

我不知道如何完成这项任务。请在这方面指导我

有什么建议吗


任何帮助都将不胜感激。

过去,
标志窗口被遮住了
标志对我很有效,但可能需要一些工作才能正确。这可能值得一试,但老实说,我仍然不确定它究竟是如何决定什么是模糊的,什么不是模糊的,所以它可能不适用于这里

static OnTouchListener listenerMotionEvent = new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if ( (motionEvent.getFlags() | MotionEvent.FLAG_WINDOW_IS_OBSCURED) != MotionEvent.FLAG_WINDOW_IS_OBSCURED ) {
            return false;
            // View is not obscured so we pass on the event
        }

        // Process the motion event here and return true if you consume it
};

我还没有测试过这个,但我认为它会起作用。我相信你必须调整它并进行更正

创建一个背景图像的副本,其中三个按钮是三种不同的颜色,没有锯齿,所有其他像素都是纯黑色。它需要是一个PNG,这样就不会有JPG压缩工件。这将是你的面具。您可以只使用纯红色、绿色和蓝色,因为您只有三个按钮。只要纵横比相同,它的分辨率可能低于原始图像

子类ImageView并使用子类来绘制背景。传递子类版本的图像以位图形式查看遮罩图像

class TouchMaskImageView extends ImageView {
    ...constructors

    public void setTouchMask(Bitmap mask){
        this.mMask = mask;
    }

    public interface OnTouchedListener{
        public void onTouched(MotionEvent event, int colorTouched);
        public void onTouchCanceled();
    }

    public void setOnTouchedListener(OnTouchedListener listener){
        this.mOnTouchedListener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        if (mOnTouchedListener  != null && mMask != null ) {
            if (x >=0 && x < (float)getWidth() &&
                y >=0 && y < (float)getHeight()) {
                //Next two lines will be more complicated if 
                  // you aren't using scaleType fitXY
                int maskX = (int) x * mMask.getWidth() / getWidth();
                int maskY = (int) y * mMask.getWidth() / getHeight();
                int maskColor = mask.getPixel(maskX, maskY);
                mOnTouchedListener.onTouched(event, maskColor);
            } else if (event.getAction()==MotionEvent.UP){
                //released finger outside image view
                mOnTouchedListener.onTouchCanceled();
            }
        }
    }
}

这有点粗糙,因为您需要图像的四个不同副本来表示四种可能的状态。如果您想节省一些内存并加快运行速度,可以尝试为叠加在正确位置的三个按钮设置单独的图像视图,并针对图像更改设置相应的布局。但这将是一个非常具有挑战性的问题,考虑到您必须使其完全符合任何屏幕大小。

将在Photoshop中对图像进行切片,然后将该图像放置在webview帮助中?

从上述答案中,我得到了提示,最后我得到了如下解决方案:

我所做的是:

在xml文件中使用RelativeLayout

使用ImageView,如下所示:

  <ImageView
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/glob2"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="80.5dp"
        android:contentDescription="@string/profile_picture_description"
        android:src="@drawable/pick_matches_background" />
在onCreate方法中

 // initialized image view
img = (ImageView) findViewById(R.id.btn1);
img.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

       // call your method here

  }
});

StateListDrawable sdCreateTrip = (StateListDrawable) img
            .getDrawable();

btmp = ((BitmapDrawable) sdCreateTrip.getCurrent())
            .getBitmap();

您现在可以开始了。

获取触摸位置@twntee:请看我更新的问题。这将帮助您:您还可以将布局划分为虚拟段/弧,并处理触摸事件。你看到了吗?你所有的愿望:),我很高兴至少我能帮上点忙:)哥们,这似乎是一个非常高级的概念。我只是一个天真的开发人员。谢谢你的回答。这个线程解决了我的问题。这个解决方案更简单,但你仍然需要做一些类似于我的第二块代码的事情,让它像一个按钮一样工作(当按下按钮时,按钮图像会发生变化,您可以在松开按钮前将手指拉下以取消按下按钮)。是否要为每个按钮使用单独的图像视图?是的。我使用的是单独的图像视图。看起来边距使用的是硬编码值,这在不同的屏幕大小上如何工作?
 // initialized image view
img = (ImageView) findViewById(R.id.btn1);
img.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

       // call your method here

  }
});

StateListDrawable sdCreateTrip = (StateListDrawable) img
            .getDrawable();

btmp = ((BitmapDrawable) sdCreateTrip.getCurrent())
            .getBitmap();