简体   繁体   English

Android:单击具有透明背景的重叠imageViews的问题

[英]Android: clicking issue of overlapping imageViews with transparent background

I have two clickable imageViews that partially overlap each other, let's say a big one is at the bottom as base and the small one is on the top right corner of it. 我有两个可单击的imageView,它们彼此部分重叠,比方说,一个大的底部位于底部,而小一个位于其右上角。 Sample view: this . 示例视图: this The black circle is where I have problem getting big image's listener. 黑色圆圈是我在获取大图像听众时遇到的问题。

The problem is, the transparent area is invisible but still there, lead to onTouch action is not so "accurate". 问题是,透明区域是不可见的,但仍然存在,导致onTouch动作不那么“准确”。 Eg Clicking on the overlapping area, where small image is transparent and big isn't, the small image will be detected, but what I want is to get the big image detected. 例如,单击重叠区域,在该区域中小图像是透明的而大图像不是透明的,那么将检测到小图像,但是我要检测的是大图像。

Using onTouchListener to catch the colour to make transparent areas not clickable won't help neither. 使用onTouchListener捕获颜色以使透明区域不可单击将无济于事。

How can I solve this please? 请问该如何解决?

ImageView big, small;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seventh);

    big = (ImageView) findViewById(R.id.imageViewBig);
    small = (ImageView) findViewById(R.id.imageViewSmall);

    big.setOnTouchListener(this);
    small.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        switch (v.getId()) {
            case R.id.imageViewBig:
                Toast.makeText(this, "Big", Toast.LENGTH_SHORT).show();
                break;
            case R.id.imageViewSmall:
                Toast.makeText(this, "Small", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(this, "none", Toast.LENGTH_SHORT).show();
        }
    }
    return true;
}

xml: XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gamification.gamificationpagestudy.ProgressBar.SeventhActivity">

<ImageView
    android:id="@+id/imageViewBig"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:clickable="true"
    android:src="@drawable/germany" />

<ImageView
    android:id="@+id/imageViewSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/imageViewBig"
    android:layout_alignRight="@+id/imageViewBig"
    android:layout_alignTop="@+id/imageViewBig"
    android:clickable="true"
    android:src="@drawable/brazil" />

</RelativeLayout>

Thanks to Dreo for his suggestion :D 感谢Dreo的建议:D

Continue with onTouchListner to get transparent area, get as well the touched View's mid point to check whether bottom left or right area is touched, then do your action, in my case bottom left corner is my GridView's item at position 0, and bottom right corner is item at position 1: 继续使用onTouchListner来获得透明区域,并获取触摸的View的中点,以检查是否触摸了左下或右下区域,然后执行操作,在我的情况下,左下角是我的GridView的位置0,右下角的项目是项目在位置1:

if (color == Color.TRANSPARENT) {

    int touchX = (int) event.getX();
    int touchY = (int) event.getY();
    int middle_width = v.getWidth()/2;
    int middle_height = v.getHeight()/2;

    if (touchX < middle_width && touchY > middle_height) // bottom left
       Toast.makeText(GamificationMainActivity.this, gvAdapter.getItem(0), Toast.LENGTH_SHORT).show();

    else if (touchX > middle_width && touchY > middle_height) // bottom right
       Toast.makeText(GamificationMainActivity.this, gvAdapter.getItem(1), Toast.LENGTH_SHORT).show();

      return true;

   } else { //... }

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

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