当前位置:操作系统 > 安卓/Android >>

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; 
   

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,