package wk.demo.widget
;
import android.content.Context
;
import android.util.AttributeSet
;
import android.view.GestureDetector
;
import android.view.MotionEvent
;
import android.view.ScaleGestureDetector
;
import android.view.View
;
public class GestureView extends View {
private final int minVelocity = 5
;
public MyGestureCallBack myGestureCallBack
;
private GestureDetector gestureDetector
;
private ScaleGestureDetector scaleGestureDetector = null
;
public GestureView(Context context) {
super(context
);
initView(context
);
}
public GestureView(Context context, AttributeSet attrs) {
super(context, attrs
);
initView(context
);
}
private void initView(Context context){
gestureDetector = new
GestureDetector(context, onGestureListener
);
gestureDetector.
setOnDoubleTapListener(onDoubleTapListener
);
scaleGestureDetector = new
ScaleGestureDetector(context, onScaleGestureListener
);
}
public void setCallBack(MyGestureCallBack myGestureCallBack) {
this.myGestureCallBack = myGestureCallBack
;
}
private ScaleGestureDetector.OnScaleGestureListener onScaleGestureListener = new ScaleGestureDetector.OnScaleGestureListener() {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scale=detector.
getScaleFactor() ;
if(scale>1){
myGestureCallBack.
zoomToMax(scale
);
}else{
myGestureCallBack.
zoomToMin(scale
);
}
return false
;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
// 一定要返回true才会进入
onScale()这个函数
return true
;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
myGestureCallBack.
zoomStop();
}
};
@Override
public boolean performClick() {
return super.
performClick();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 手势事件交给 Detector接管
gestureDetector.
onTouchEvent(event
);
scaleGestureDetector.
onTouchEvent(event
);
performClick();
return true
;
}
private GestureDetector.OnDoubleTapListener onDoubleTapListener = new GestureDetector.OnDoubleTapListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
myGestureCallBack.
onClick();
return false
;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
myGestureCallBack.
doubleClick();
return false
;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// 一般可不用
return false
;
}
};
private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.OnGestureListener(){
@Override
public boolean onDown(MotionEvent e) {
// 一般可不用
return true
;
}
@Override
public void onShowPress(MotionEvent e) {
// 一般可不用
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// 一般不用
return false
;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (distanceX < -minVelocity) {
myGestureCallBack.
scrollToRight( );
} else if (distanceX > minVelocity) {
myGestureCallBack.
scrollToLeft( );
} else if (distanceY < -minVelocity) {
myGestureCallBack.
scrollToBottom( );
} else if (distanceY > minVelocity) {
myGestureCallBack.
scrollToTop( );
}
return true
;
}
@Override
public void onLongPress(MotionEvent e) {
myGestureCallBack.
onLongPress();
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > minVelocity && Math.abs(velocityX) > minVelocity) {
myGestureCallBack.
scrollToLeft();
} else if ((e2.getX() - e1.getX() > minVelocity && Math.abs(velocityX) > minVelocity)) {
myGestureCallBack.
scrollToRight();
} else if (e1.getY() - e2.getY() > minVelocity && Math.abs(velocityY) > minVelocity) {
myGestureCallBack.
scrollToTop();
} else if ((e2.getY() - e1.getY() > minVelocity && Math.abs(velocityY) > minVelocity)) {
myGestureCallBack.
scrollToBottom( );
}
return true
;
}
};
public interface MyGestureCallBack {
void
scrollToLeft( );
void
scrollToRight( );
void
scrollToTop( );
void
scrollToBottom( );
void
zoomToMax( float scale
);
void
zoomToMin( float scale
);
void
zoomStop( );
void
doubleClick( );
void
onClick();
void
onLongPress();
}
}
使用:
gestureView =
findViewById(R.id.gestureView
) ;
gestureView.
setCallBack(myGestureCallback
);
本文转载自网络,转载仅为分享干货知识,如有侵权欢迎联系云豹科技进行删除处理