Android 多個ImageView圖片層疊透明區域點擊事件穿透

經常用到多個透明圖片層疊,但又需要獲取不同圖片的點擊事件,本文實現圖片透明區域穿透點擊事件。
效果圖:

 一、先上圖片,這2張圖片尺寸是一樣的,放到drawable目錄下:
圖1:transparent.png

 
圖2:transparent2.png

 
二、上佈局文件,test.xml:
[html] 
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:orientation="vertical" 
   android:gravity="center" 
   android:id="@+id/mainLayout"> 
    
    <ImageView android:id="@+id/ImageView01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/transparent"/> 
     
    <ImageView android:id="@+id/ImageView02" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/transparent2"/>     
         
</FrameLayout> 

 
三、上Activity文件,MainActivity.java
 
[java] 
package org.shuxiang.test; 
 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.Window; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.widget.ImageView; 
 
public class MainActivity extends Activity 

    private ImageView iv1; 
    private ImageView iv2; 
    private Bitmap bitmap1, bitmap2; 
    private boolean iv1Transparent = false; 
    private boolean iv2Transparent = false; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.test); 
 
        iv1 = (ImageView) findViewById(R.id.ImageView01); 
        iv2 = (ImageView) findViewById(R.id.ImageView02); 
        bitmap1 = ((BitmapDrawable) (iv1.getDrawable())).getBitmap(); 
        bitmap2 = ((BitmapDrawable) (iv2.getDrawable())).getBitmap(); 
         
        iv1.setOnClickListener(new OnClickListener(){ 
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                if(iv1Transparent) 
                { 
                    Log.i("test", "圖1透明區域"); 
                } 
                else 
                { 
                    Log.i("test", "圖1點擊");                   
                } 
            } 
             
        }); 
         
        iv1.setOnTouchListener(new OnTouchListener() 
        { 
            @Override 
            public boolean onTouch(View arg0, MotionEvent arg1)  
            { 
                // TODO Auto-generated method stub 
                if(bitmap1.getPixel((int)(arg1.getX()),((int)arg1.getY()))==0) 
                { 
                    Log.i("test", "圖1透明區域"); 
                    iv1Transparent = true;  //透明區域設置true                     
                } 
                else 
                { 
                    Log.i("test", "圖1實體區域"); 
                    iv1Transparent = false; 
                } 
                return false; 
            }            
        }); 
         
        iv2.setOnClickListener(new OnClickListener() 
        { 
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                if(iv2Transparent) 
                { 
                    Log.i("test", "圖2透明區域"); 
                } 
                else 
                { 
                    Log.i("test", "圖2點擊");                   
                } 
            } 
             
        }); 
         
        iv2.setOnTouchListener(new OnTouchListener() 
        { 
            @Override 
            public boolean onTouch(View v, MotionEvent event) { 
                // TODO Auto-generated method stub 
                if(bitmap2.getPixel((int)(event.getX()),((int)event.getY()))==0) 
                { 
                    Log.i("test", "圖2透明區域"); 
                    iv2Transparent = true;  //透明區域設置true 
                    iv1.dispatchTouchEvent(event); 
                } 
                else 
                { 
                    Log.i("test", "圖2實體區域"); 
                    iv2Transparent = false; 
                } 
                return false; 
            } 
        }); 
    } 

 

發佈留言