张慧的博客

张慧的博客

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  45 Posts :: 0 Stories :: 24 Comments :: 0 Trackbacks

经常用到多个透明图片层叠,但又需要获取不同图片的点击事件,本文实现图片透明区域穿透点击事件。

效果图:

 欢迎转载请说明转自:http://blog.csdn.net/aminfo/article/details/7872748

一、先上图片,这2张图片尺寸是一样的,放到drawable目录下:

图1:transparent.png

 

图2:transparent2.png

 

二、上布局文件,test.xml

<?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>

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;
            }
        });
    }
}


posted on 2012-08-16 22:28 张慧 阅读(3364) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: