http://blog.youkuaiyun.com/tsiannian/article/details/6624604
Android提供了两种常用的处理触摸事件的方式:
(1)在自定义的View对象中覆盖
public boolean onTouchEvent(MotionEvent event);
- publicbooleanonTouchEvent(MotionEventevent);
(2)为View指定触摸事件的监听器
myView.setOnTouchListener(new View.onTouchListener(View v, MotionEvent event));
- myView.setOnTouchListener(newView.onTouchListener(Viewv,MotionEventevent));
当然一个监听器可以处理多个View的触摸事件,因此多了一个View 参数。下面是一个小小的例子:我们知道一个当点击Button对象时,会调用onClickListener中的onClick函数,我们可以模拟一下这种效果。
import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.Toast; public class TouchButtonActivity extends Activity { Button button1; TouchListener tl; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button)findViewById(R.id.button1); tl = new TouchListener(); button1.setOnTouchListener(tl); } class TouchListener implements View.OnTouchListener{ private int i = 0; @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); Toast toast = Toast.makeText(getApplicationContext(), "The "+ event.toString()+"has recieved!", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER , 0, 100*i); i++; i = i%4; toast.show(); return false; } } }
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.Gravity;
- importandroid.view.MotionEvent;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.Toast;
- publicclassTouchButtonActivityextendsActivity{
- Buttonbutton1;
- TouchListenertl;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button1=(Button)findViewById(R.id.button1);
- tl=newTouchListener();
- button1.setOnTouchListener(tl);
- }
- classTouchListenerimplementsView.OnTouchListener{
- privateinti=0;
- @Override
- publicbooleanonTouch(Viewv,MotionEventevent){
- //TODOAuto-generatedmethodstub
- intaction=event.getAction();
- Toasttoast=Toast.makeText(getApplicationContext(),
- "The"+event.toString()+"hasrecieved!",Toast.LENGTH_SHORT);
- toast.setGravity(Gravity.CENTER,0,100*i);
- i++;
- i=i%4;
- toast.show();
- returnfalse;
- }
- }
- }
运行上面的代码,点击Button按钮会出现几个Toast提示框。实际上当我们点击View时,收到的是一个 事件序列。刚点击时会收到ACTION_DOWN, 触摸屏是很敏感的, 你还会收到几个ACTION_MOVE, 当手放开时会收到ACTION_UP;下面来点有意思的代码来处理不同的事件。其中手势终止时会会触发ACTION_CANCEL。
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class Dot extends View { private static final float RADIUS = 40; private float x = 40; private float y = 40; private float initialX; private float initialY; private float offsetX; private float offsetY; private Paint backgroundPaint; private Paint myPaint; public Dot(Context context, AttributeSet attrs) { super(context, attrs); backgroundPaint = new Paint(); backgroundPaint.setColor(Color.BLUE); myPaint = new Paint(); myPaint.setColor(Color.YELLOW); myPaint.setAntiAlias(true); } @Override public boolean onTouchEvent(MotionEvent event){ int action = event.getAction(); switch(action){ case MotionEvent.ACTION_DOWN: initialX = x; initialY = y; offsetX = event.getX(); offsetY = event.getY(); break; case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: x = initialX + event.getX() - offsetX; y = initialY + event.getY() - offsetY; break; } event.recycle(); return(true); } @Override public void draw(Canvas canvas){ int width = canvas.getWidth(); int height = canvas.getHeight(); canvas.drawRect(0, 0, width, height, backgroundPaint); canvas.drawCircle(x, y, RADIUS, myPaint); invalidate(); } }
- importandroid.content.Context;
- importandroid.graphics.Canvas;
- importandroid.graphics.Color;
- importandroid.graphics.Paint;
- importandroid.util.AttributeSet;
- importandroid.view.MotionEvent;
- importandroid.view.View;
- publicclassDotextendsView{
- privatestaticfinalfloatRADIUS=40;
- privatefloatx=40;
- privatefloaty=40;
- privatefloatinitialX;
- privatefloatinitialY;
- privatefloatoffsetX;
- privatefloatoffsetY;
- privatePaintbackgroundPaint;
- privatePaintmyPaint;
- publicDot(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- backgroundPaint=newPaint();
- backgroundPaint.setColor(Color.BLUE);
- myPaint=newPaint();
- myPaint.setColor(Color.YELLOW);
- myPaint.setAntiAlias(true);
- }
- @Override
- publicbooleanonTouchEvent(MotionEventevent){
- intaction=event.getAction();
- switch(action){
- caseMotionEvent.ACTION_DOWN:
- initialX=x;
- initialY=y;
- offsetX=event.getX();
- offsetY=event.getY();
- break;
- caseMotionEvent.ACTION_MOVE:
- caseMotionEvent.ACTION_UP:
- caseMotionEvent.ACTION_CANCEL:
- x=initialX+event.getX()-offsetX;
- y=initialY+event.getY()-offsetY;
- break;
- }
- event.recycle();
- return(true);
- }
- @Override
- publicvoiddraw(Canvascanvas){
- intwidth=canvas.getWidth();
- intheight=canvas.getHeight();
- canvas.drawRect(0,0,width,height,backgroundPaint);
- canvas.drawCircle(x,y,RADIUS,myPaint);
- invalidate();
- }
- }
下面是mian.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <com.tsiannian.gmail.dot.Dot
- android:id="@+id/dot"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>