首页 > 代码库 > 安卓 surfaceview 添加点击事件

安卓 surfaceview 添加点击事件

功能说明:在一个Activity页面中,点击页面布局中的MapView(继承SurfaceView),实现页面中的某个元素隐藏和显示。

贴出MapView的代码:

  1 /**  2  * @author gr  3  */  4 package com.cn.gordon.exhibition.walk.view;  5   6 import android.content.Context;  7 import android.graphics.Bitmap;  8 import android.graphics.Canvas;  9 import android.graphics.Color; 10 import android.graphics.Matrix; 11 import android.graphics.Paint; 12 import android.graphics.PorterDuff.Mode; 13 import android.util.AttributeSet; 14 import android.util.Log; 15 import android.view.GestureDetector; 16 import android.view.GestureDetector.SimpleOnGestureListener; 17 import android.view.MotionEvent; 18 import android.view.SurfaceHolder; 19 import android.view.SurfaceHolder.Callback; 20 import android.view.SurfaceView; 21 import android.view.View; 22  23 /** 24  * @author gr 25  *  26  */ 27 public class MapView extends SurfaceView implements Callback { 28  29     private Context context; 30     private Bitmap bitmap_map; 31     private Paint bitmap_paint; 32     private float move_x, move_y; 33     private int view_W, view_H; 34     private float offset = 1, offset_back = 1,myPosition_degree = 0; 35     private GestureDetector mGestureDetector; 36     private MyPosition myPosition; 37  38     public MapView(Context context, AttributeSet attrs, int defStyle) { 39         super(context, attrs, defStyle); 40         init(context); 41     } 42  43     public MapView(Context context, AttributeSet attrs) { 44         super(context, attrs); 45         init(context); 46     } 47  48     public MapView(Context context) { 49         super(context); 50         init(context); 51         setFocusable(true);//使用Key event,setFocusable(true)可以聚焦 52     } 53  54     private void init(Context context) { 55         this.context = context; 56         bitmap_paint = new Paint(); 57         bitmap_paint.setAntiAlias(true); 58         myPosition = new MyPosition(this.context); 59         mGestureDetector = new GestureDetector(this.context, 60                 new MyOnGestureListener()); 61         getHolder().addCallback(this); 62         setOnClickListener(new OnClickListener() { 63              64             @Override 65             public void onClick(View v) { 66                 // TODO Auto-generated method stub 67                 if (v!=null) { 68                      69                 } 70             } 71         }); 72         setOnTouchListener(new OnTouchListener() { 73  74             @Override 75             public boolean onTouch(View v, MotionEvent event) { 76                 if (event.getPointerCount() > 1) { 77                     return false; 78                 } 79                 mGestureDetector.onTouchEvent(event); 80                 return true; 81             } 82         }); 83     } 84  85     public void setMap(Bitmap bitmap) { 86         bitmap_map = bitmap; 87         move_x = 0; 88         move_y = 0; 89         if (view_W != 0) { 90             offset_back = offset = (float) view_W 91                     / (float) bitmap_map.getWidth(); 92         } 93  94         draw(); 95  96     } 97  98     public synchronized void doDraw(Canvas canvas) { 99         canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);100         if (bitmap_map == null) {101             return;102         }103 104         Matrix matrix = new Matrix();105         matrix.postScale(offset, offset);106         matrix.postTranslate(move_x, move_y);107         canvas.drawBitmap(bitmap_map, matrix, bitmap_paint);108         myPosition.draw(canvas, (100*offset+move_x), (100*offset+move_y), myPosition_degree);109     }110 111     @Override112     public void surfaceChanged(SurfaceHolder holder, int format, int width,113             int height) {114         // TODO Auto-generated method stub115 116     }117 118     @Override119     public void surfaceCreated(SurfaceHolder holder) {120         view_W = getWidth();121         view_H = getHeight();122         if (bitmap_map != null) {123             offset_back = offset = (float) view_W124                     / (float) bitmap_map.getWidth();125         }126         draw();127 128     }129 130     @Override131     public void surfaceDestroyed(SurfaceHolder holder) {132         // TODO Auto-generated method stub133 134     }135 136     private void draw() {137         Canvas canvas = getHolder().lockCanvas(null);138         if (canvas != null) {139             doDraw(canvas);140         }141         if (canvas != null) {142             getHolder().unlockCanvasAndPost(canvas);143         }144     }145 146     private class MyOnGestureListener extends SimpleOnGestureListener {147 148         @Override149         public boolean onSingleTapUp(MotionEvent e) {150             Log.i(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "mapview onSingleTapUp click");151             return true;152         }153 154         @Override155         public boolean onScroll(MotionEvent e1, MotionEvent e2,156                 float distanceX, float distanceY) {157             move_x = move_x - distanceX;158             move_y = move_y - distanceY;159             if (move_x > 0) {160                 move_x = 0;161             }162 163             if (move_x < (view_W - bitmap_map.getWidth() * offset)) {164                 move_x = view_W - bitmap_map.getWidth() * offset;165                 if (move_x > 0) {166                     move_x = 0;167                 }168             }169             if (move_y > 0) {170                 move_y = 0;171             }172             if (move_y < (view_H - bitmap_map.getHeight() * offset)) {173                 move_y = view_H - bitmap_map.getHeight() * offset;174                 if (move_y > 0) {175                     move_y = 0;176                 }177             }178             Log.i(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "mapview onScroll click");179             draw();180             return true;181         }182 183         @Override184         public boolean onDown(MotionEvent e) {185             temp_x = 0;186             temp_y = 0;187             Log.i(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "mapview onDown click");188             return true;189         }190 191         @Override192         public boolean onDoubleTapEvent(MotionEvent e) {193 194             offset = offset_back;195             move_x = 0;196             move_y = 0;197             Log.i(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "mapview onDoubleTapEvent click");198             draw();199             return true;200         }201 202         @Override203         public boolean onSingleTapConfirmed(MotionEvent e) {204             Log.i(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "mapview onSingleTapConfirmed click");205             if(clickLinster != null){206                 clickLinster.onClick();207             }208             return true;209         }210 211     }212     private float temp_x,temp_y;213     @Override214     public boolean onTouchEvent(MotionEvent event) {215         if(temp_x != 0 && temp_y != 0){216             float xx = Math.abs(event.getX(0) - event.getX(1))217                     - Math.abs(temp_x);218             float yy = Math.abs(event.getY(0) - event.getY(1))219                     - Math.abs(temp_y);220             float t = 0;221             t = Math.abs(xx) > Math.abs(yy)?xx:yy;222             if(t != 0){223                 if(t > 0){224                     offset += Math.abs(t) / 500;225                 } else {226                     offset -= Math.abs(t) / 500;227                 }228                 if(bitmap_map.getWidth()*offset > view_W/2 || bitmap_map.getHeight()*offset > view_H/2){229                     230                     draw();231                 }232             }233         }234         temp_x = event.getX(0) - event.getX(1);235         temp_y = event.getY(0) - event.getY(1);236         return super.onTouchEvent(event);237     }238     239     public void setDegree(float degree){240         myPosition_degree = degree;241         draw();242     }243 244 245     private OnMyClickLinster clickLinster;246     247     248     249     public void setClickLinster(OnMyClickLinster clickLinster) {250         this.clickLinster = clickLinster;251     }252 253     public interface OnMyClickLinster{254         public void onClick();255     }256 }

代码中红色标记为关键代码,主要目的是向外提供一个该MapView可供用户点击的公共接口。

然后贴出测试代码:

 1 package com.cn.gordon.exhibition.walk.activity; 2  3 import com.cn.gordon.exhibition.walk.view.MapView; 4  5 import android.app.Activity; 6 import android.graphics.BitmapFactory; 7 import android.os.Bundle; 8 import android.widget.Toast; 9 10 public class ForumArrangementActivity extends Activity {11     /** Called when the activity is first created. */12     private MapView wallpaper;13     //LinearLayout linearLayout;14     @Override15     public void onCreate(Bundle savedInstanceState) {16         super.onCreate(savedInstanceState);17         setContentView(R.layout.mymap);18         super.onCreate(savedInstanceState);19         wallpaper = (MapView)super.findViewById(R.id.mymap); 20         wallpaper.setMap(BitmapFactory.decodeResource(getResources(), R.drawable.map));21         wallpaper.setFocusable(true);22         wallpaper.setFocusableInTouchMode(true);23         wallpaper.setClickLinster(new MapView.OnMyClickLinster() {24             25             @Override26             public void onClick() {27                 Toast.makeText(ForumArrangementActivity.this, "haha ", Toast.LENGTH_SHORT).show();28             }29         });30     }31 }

当然,还有mymap的布局文件。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#FFFFFFFF" >        <RelativeLayout            android:id="@+id/temp_relative"            android:layout_width="match_parent"            android:layout_height="50dip"            android:layout_alignParentTop="true"            android:background="#FF333333" >                         <Button                android:id="@+id/title_information"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:layout_centerInParent="true"               android:background="#00FFFFFF"               android:text="@string/walk_exhibition"               android:textColor="#FFFFFFFF"               android:textSize="20sp"/>        <TextView            android:id="@+id/titlebar_information"            android:layout_width="18dip"            android:layout_height="18dip"            android:layout_toRightOf="@id/title_information"            android:background="@drawable/title_notifybar"            android:layout_marginTop="5dip"            android:text="@string/information_count"            android:textColor="#FFFFFFFF"            android:textSize="12sp"             android:gravity="center"/>            <Button                android:layout_width="80dip"                android:layout_height="50dip"                android:id="@+id/btn_menu"                android:background="@drawable/menu_btn_bg"                 />            <Button                android:layout_width="30dip"                android:layout_height="30dip"                android:id="@+id/btn_submenu_information"                android:layout_centerVertical="true"                android:layout_alignParentRight="true"                android:layout_marginRight="20dip"                android:background="@drawable/querybtn_pressed_bg" />        </RelativeLayout>                                <LinearLayout             android:layout_width="match_parent"            android:layout_height="50dip"            android:orientation="vertical"            android:id="@+id/temp_linearlayout"            android:layout_alignParentBottom="true"            android:background="#FF5c5c5c">            <LinearLayout                 android:layout_width="match_parent"                android:layout_height="25dip"                android:orientation="horizontal"                android:baselineAligned="false"                 android:id="@+id/map_layout1">                <LinearLayout                     android:layout_width="0dip"                    android:layout_height="25dip"                    android:gravity="center_vertical"                    android:layout_weight="1">                    <TextView                         android:layout_width="10dip"                        android:layout_height="10dip"                        android:layout_marginLeft="10dip"                        android:background="@drawable/walk_pointer"                        />                    <TextView                         android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="5dip"                        android:text="@string/walk_whereiam"                        android:textSize="12sp"                        android:textColor="@color/blue"/>                </LinearLayout>                <LinearLayout                     android:layout_width="0dip"                    android:layout_height="30dip"                    android:gravity="center_vertical"                    android:layout_weight="1">                        <TextView                         android:layout_width="10dip"                        android:layout_height="10dip"                        android:layout_marginLeft="10dip"                        android:background="@drawable/walk_havewalked"                        />                    <TextView                         android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="5dip"                        android:text="@string/walk_havewalked"                        android:textSize="12sp"                        android:textColor="@color/blue"/>                </LinearLayout>                <LinearLayout                     android:layout_width="0dip"                    android:layout_height="30dip"                    android:gravity="center_vertical"                    android:layout_weight="1">                        <TextView                         android:layout_width="10dip"                        android:layout_height="10dip"                        android:layout_marginLeft="10dip"                        android:background="@drawable/walk_togo"                        />                    <TextView                         android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="5dip"                        android:text="@string/walk_togo"                        android:textSize="12sp"                        android:textColor="@color/blue"/>                </LinearLayout>                <LinearLayout                     android:layout_width="0dip"                    android:layout_height="30dip"                    android:gravity="center_vertical"                    android:layout_weight="1">                        <TextView                         android:layout_width="10dip"                        android:layout_height="10dip"                        android:layout_marginLeft="10dip"                        android:background="@drawable/title_notifybar"                        />                    <TextView                         android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="5dip"                        android:text="@string/walk_information"                        android:textSize="12sp"                        android:textColor="@color/blue"/>                </LinearLayout>            </LinearLayout>            <LinearLayout                 android:layout_width="match_parent"                android:layout_height="25dip"                android:baselineAligned="false"                android:orientation="horizontal"                android:gravity="center_vertical"                android:id="@+id/map_layout2">                    <LinearLayout                     android:layout_width="0dip"                    android:layout_height="25dip"                    android:gravity="center_vertical"                    android:layout_weight="1">                    <TextView                         android:layout_width="10dip"                        android:layout_height="10dip"                        android:layout_marginLeft="10dip"                        android:background="@drawable/btn_queryresult"                        />                    <TextView                         android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="5dip"                        android:text="@string/walk_query"                        android:textSize="12sp"                        android:textColor="@color/blue"/>                </LinearLayout>                <LinearLayout                     android:layout_width="0dip"                    android:layout_height="30dip"                    android:gravity="center_vertical"                    android:layout_weight="1">                        <TextView                         android:layout_width="10dip"                        android:layout_height="10dip"                        android:layout_marginLeft="10dip"                        android:background="@drawable/walk_lots"                        />                    <TextView                         android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="5dip"                        android:text="@string/walk_lots"                        android:textSize="12sp"                        android:textColor="@color/blue"/>                </LinearLayout>                <LinearLayout                     android:layout_width="0dip"                    android:layout_height="30dip"                    android:gravity="center_vertical"                    android:layout_weight="1">                        <TextView                         android:layout_width="10dip"                        android:layout_height="10dip"                        android:layout_marginLeft="10dip"                        android:background="@drawable/walk_many"                        />                    <TextView                         android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="5dip"                        android:text="@string/walk_many"                        android:textSize="12sp"                        android:textColor="@color/blue"/>                </LinearLayout>                <LinearLayout                     android:layout_width="0dip"                    android:layout_height="30dip"                    android:gravity="center_vertical"                    android:layout_weight="1">                        <TextView                         android:layout_width="10dip"                        android:layout_height="10dip"                        android:layout_marginLeft="10dip"                        android:background="@drawable/walk_little"                        />                    <TextView                         android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="5dip"                        android:text="@string/walk_little"                        android:textSize="12sp"                        android:textColor="@color/blue"/>                </LinearLayout>            </LinearLayout>        </LinearLayout>        <com.cn.gordon.exhibition.walk.view.MapView            android:id="@+id/mymap"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_centerInParent="true"            android:layout_below="@+id/temp_relative"            android:layout_above="@+id/temp_linearlayout"            android:orientation="vertical"            android:visibility="visible" >        </com.cn.gordon.exhibition.walk.view.MapView></RelativeLayout>

surfaceview的点击事件可以实现了,在这里的代码是测试代码。

安卓 surfaceview 添加点击事件