Android不规则点击区域详解

 直接上代码:

1.demo代码,DemoActivity.java

 1 package com.test.activity;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.view.View.OnClickListener;
 7 import android.widget.Toast;
 8 
 9 import com.test.R;
10 
11 public class DemoActivity extends Activity implements OnClickListener {
12     
13     private Toast mToast;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate( savedInstanceState);
18         setContentView( R.layout.activity_main);
19         findViewById( R.id.menu_1).setOnClickListener( this);
20         findViewById( R.id.menu_2).setOnClickListener( this);
21         findViewById( R.id.menu_3).setOnClickListener( this);
22         findViewById( R.id.menu_4).setOnClickListener( this);
23     }
24 
25 
26     @Override
27     public void onClick(View v) {
28         if(null != mToast) {
29             mToast.cancel();
30         }
31         switch(v.getId()) {
32             case R.id.menu_1:
33                 mToast = Toast.makeText( this, "red", Toast.LENGTH_SHORT);
34                 break;
35             case R.id.menu_2:
36                 mToast = Toast.makeText( this, "yellow", Toast.LENGTH_SHORT);
37                 break;
38             case R.id.menu_3:
39                 mToast = Toast.makeText( this, "green", Toast.LENGTH_SHORT);
40                 break;
41             case R.id.menu_4:
42                 mToast = Toast.makeText( this, "blue", Toast.LENGTH_SHORT);
43                 break;
44         }
45         mToast.show();
46     }
47 }

2.自定义控件代码,MenuViewItem.java

 1 package com.test.view;
 2 
 3 import android.content.Context;
 4 import android.graphics.Bitmap;
 5 import android.graphics.Color;
 6 import android.graphics.drawable.BitmapDrawable;
 7 import android.graphics.drawable.Drawable;
 8 import android.graphics.drawable.StateListDrawable;
 9 import android.util.AttributeSet;
10 import android.view.MotionEvent;
11 import android.widget.FrameLayout;
12 
13 public class MenuViewItem extends FrameLayout {
14 
15      
16 
17     private int width = -1;
18 
19     private int height = -1;
20 
21     private Bitmap bitmap;
22 
23  
24 
25     public MenuViewItem(Context context) {
26 
27         super( context);
28 
29     }
30 
31  
32 
33     public MenuViewItem(Context context, AttributeSet attrs, int defStyle) {
34 
35         super( context, attrs, defStyle);
36 
37     }
38 
39  
40 
41     public MenuViewItem(Context context, AttributeSet attrs) {
42 
43         super( context, attrs);
44 
45     }
46 
47  
48 
49     @Override
50 
51     public boolean onTouchEvent(MotionEvent event) {
52 
53         int action = event.getAction();
54 
55         if(action != MotionEvent.ACTION_DOWN) {
56 
57             return super.onTouchEvent( event);
58 
59         }
60 
61         int x = (int)event.getX();
62 
63         int y = (int)event.getY();
64 
65         if(width == -1 || height == -1) {
66 
67             Drawable drawable = ((StateListDrawable)getBackground()).getCurrent();
68 
69             bitmap = ((BitmapDrawable)drawable).getBitmap();
70 
71             width = getWidth();
72 
73             height = getHeight();
74 
75         }
76 
77         if(null == bitmap || x < 0 || y < 0 || x >= width || y >= height) {
78 
79             return false;
80 
81         }
82 
83         int pixel = bitmap.getPixel( x, y);
84 
85         if(Color.TRANSPARENT == pixel) {
86 
87             return false;
88 
89         }
90 
91         return super.onTouchEvent( event);
92 
93     }
94 
95 }

3.xml界面代码,activity_main.xml

 1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:gravity="center"
 6     android:orientation="vertical" >
 7 
 8     <com.test.view.MenuViewItem
 9         android:id="@+id/menu_1"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:layout_gravity="center"
13         android:background="@drawable/chrome_1" />
14 
15     <com.test.view.MenuViewItem
16         android:id="@+id/menu_2"
17         android:layout_width="wrap_content"
18         android:layout_height="wrap_content"
19         android:layout_gravity="center"
20         android:background="@drawable/chrome_2" />
21 
22     <com.test.view.MenuViewItem
23         android:id="@+id/menu_3"
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         android:layout_gravity="center"
27         android:background="@drawable/chrome_3" />
28 
29     <com.test.view.MenuViewItem
30         android:id="@+id/menu_4"
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content"
33         android:layout_gravity="center"
34         android:background="@drawable/chrome_4" />
35 
36 </FrameLayout>

4.图片控件代码,chrome_1.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3 
4     <item android:drawable="@drawable/chrome_1_s" android:state_pressed="true"/>
5     <item android:drawable="@drawable/chrome_1_n"/>
6 
7 </selector>

5.图片资源:

技术分享技术分享技术分享技术分享

技术分享技术分享技术分享技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。