自定义View实现手势监听

下面是我自定义的view,主要是实现左右滑动

要实现手势监听,首先要实现OnGestureListener接口

其次是实例化一个GestureDetector:mDetector = new GestureDetector(context, this);

如果只是简单的实现OnGestureListener的方法,是不能实现监听效果,

我们要将onDown , onScroll的返回值改为true,自定义view的clickable属性为true。

除此之外,我们要重写自定义view的onTouchEvent方法,onTouchEvent的返回值改为

mDetector.onTouchEvent(event);即可。



package com.uwatch.condor.view;



import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
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 com.uwatch.condor.bll.PedometerBLL;
import com.uwatch.condor.util.DateRange;


public class SportView extends View implements OnGestureListener{
private static String TAG = "SportView";
private static int MSG_SCROLL = 0x123;
private Context mContext;
private DateRange mDateRange;
private OnSportViewListener mOnSportViewListener; 
private GestureDetector mDetector;
private PedometerBLL mPedoBll;
private static int sCurrPos = 0;


private Handler handler = new Handler(){


@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);


}
};


private String[] times = {"00:00","04:00","08:00","12:00","16:00","20:00","24:00"};


public SportView(Context context) {
super(context);
init(context);
}


public SportView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}


public SportView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}


private void init(Context context) {
this.mContext = context;
mDetector = new GestureDetector(context, this);
}


public void setDateRange(DateRange dt){
this.mDateRange = dt;
invalidate();
}


public void setOnSportViewListener(OnSportViewListener listener){
this.mOnSportViewListener = listener;
}


public void setPedoBll(PedometerBLL bll){
this.mPedoBll = bll;
invalidate();
}


@Override
protected void onDraw(Canvas canvas) {
if(getWidth()>0 && getHeight()>0){


}
super.onDraw(canvas);
}


public void drawXY(){


}


@Override
public boolean onTouchEvent(MotionEvent event) {
return mDetector.onTouchEvent(event);
}


@Override
public boolean onDown(MotionEvent e) {
Log.e(TAG,"onDown");
return true;
}


@Override
public void onShowPress(MotionEvent e) {
Log.e(TAG,"onShowPress");
}


@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.e(TAG,"onSingleTapUp");
return false;
}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.e(TAG,"onScroll");
return true;
}


@Override
public void onLongPress(MotionEvent e) {
Log.e(TAG,"onLongPress");
}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.e(TAG,"onFling");
if(e1.getX() startScroll(1);
}else if(e1.getX()>e2.getX()){
startScroll(-1);
}
return false;
}


public void startScroll(int direction){
sCurrPos = sCurrPos + direction;
mOnSportViewListener.scrollCompleted(true, mDateRange.GetRangeDate(mDateRange.getCurrDate(), sCurrPos));
if(direction == 1){//right


}else if(direction == -1){///left


}
}


public interface OnSportViewListener{
public void scrollCompleted(boolean completed,String date);
}
}

你可能感兴趣的:(Android)