自定义一个view,并实现最简单的手势识别功能(上)

自定义一个view,并实现最简单的手势识别功能,并通过输出log进行简单分析。


package com.example.mygesturedetector;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MyView extends View {
	private String TAG = "MYVIEW";

	private GestureDetector myGestrueDetector;

	public MyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}
	public MyView(Context context,AttributeSet attrs) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}
	private void init(Context context) {
		// TODO Auto-generated method stub
		
		this.myGestrueDetector = new GestureDetector(context,
				new MyOnGestureListener());
		
		this.setOnTouchListener(new MyOnTouchListener());
		
		this.setLongClickable(true);
		
		invalidate();
		
	}
	class MyOnGestureListener implements OnGestureListener {
		@Override
		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
			Log.v(TAG, "onDown()-------------------pass");
			return false;
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			// TODO Auto-generated method stub
			Log.v(TAG, "onFling()-------------------pass");
			return false;
		}

		@Override
		public void onLongPress(MotionEvent e) {
			Log.v(TAG, "onLongPress()-------------------pass");
			// TODO Auto-generated method stub

		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			Log.v(TAG, "onScroll()-------------------pass");
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void onShowPress(MotionEvent e) {
			Log.v(TAG, "onShowPress()-------------------pass");
			// TODO Auto-generated method stub

		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			Log.v(TAG, "onSingleTapUp()-------------------pass");
			// TODO Auto-generated method stub
			return false;
		}

	}	
	class MyOnTouchListener implements OnTouchListener{

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.v(TAG, "onTouch()-------------------pass");
			// TODO Auto-generated method stub
			return myGestrueDetector.onTouchEvent(event);
		}
		
	}	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawARGB(255, 255, 255, 0);
	}

}

其中xml布局文件如下:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity">


<TextView

android:id="@+id/tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world"/>

<com.example.mygesturedetector.MyView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/myview"

android:layout_below="@id/tv"

/>


</RelativeLayout>


Activity的代码如下:

packagecom.example.mygesturedetector;


importandroid.os.Bundle;

importandroid.app.Activity;

importandroid.view.Menu;


publicclass MainActivity extends Activity {


@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}


@Override

publicboolean onCreateOptionsMenu(Menu menu) {

//Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main,menu);

returntrue;

}


}

最后输出log如下:

点击屏幕

04-1113:48:18.572: V/MYVIEW(5846): onTouch()-------------------pass

04-1113:48:18.572: V/MYVIEW(5846): onDown()-------------------pass

04-1113:48:18.616: V/MYVIEW(5846): onTouch()-------------------pass

04-1113:48:18.616: V/MYVIEW(5846): onSingleTapUp()-------------------pass


小幅度滑动屏幕

04-1113:50:22.711: V/MYVIEW(6129): onDown()-------------------pass

04-1113:50:22.736: V/MYVIEW(6129): onTouch()-------------------pass

04-1113:50:22.736: V/MYVIEW(6129): onScroll()-------------------pass

04-1113:50:22.746: V/MYVIEW(6129): onTouch()-------------------pass

04-1113:50:22.746: V/MYVIEW(6129): onScroll()-------------------pass

04-1113:50:22.766: V/MYVIEW(6129): onTouch()-------------------pass

04-1113:50:22.766: V/MYVIEW(6129): onScroll()-------------------pass


大幅度滑动屏幕

04-1113:50:22.711: V/MYVIEW(6129): onTouch()-------------------pass

04-1113:50:22.711: V/MYVIEW(6129): onDown()-------------------pass

04-1113:50:22.736: V/MYVIEW(6129): onTouch()-------------------pass

04-1113:50:22.736: V/MYVIEW(6129): onScroll()-------------------pass

04-1113:50:22.746: V/MYVIEW(6129): onTouch()-------------------pass

04-1113:50:22.746: V/MYVIEW(6129): onScroll()-------------------pass

04-1113:50:22.766: V/MYVIEW(6129): onTouch()-------------------pass

04-1113:50:22.766: V/MYVIEW(6129): onScroll()-------------------pass

04-1113:50:22.777: V/MYVIEW(6129): onTouch()-------------------pass

04-1113:50:22.777: V/MYVIEW(6129): onFling()-------------------pass


长按屏幕

04-1114:05:01.006: V/MYVIEW(6349): onTouch()-------------------pass

04-1114:05:01.006: V/MYVIEW(6349): onDown()-------------------pass

04-1114:05:01.127: V/MYVIEW(6349): onShowPress()-------------------pass

04-1114:05:01.626: V/MYVIEW(6349): onLongPress()-------------------pass

04-1114:05:04.690: V/MYVIEW(6349): onTouch()-------------------pass


截图为:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值