以前都是迷茫过日子,沉迷游戏,也是被腾讯害了我的前半生。谁都有人生最灰暗的一段日子,索性后来被一耳光打醒,才开始懂得珍惜时间,才会觉得时间是最贵重的,得拼命地挤。我的github13年申请,前面是空白,最近就开始绿了,我相信坚持下去,会种出一片草坪。
走过路过千万不要错过,瞧一瞧看一看了,Android手势操作大甩卖。这里有按下,抬起,点击,长按,左滑,右滑,上滑,下滑,放大缩小,应有尽有。10块钱打包带走,你还在犹豫什么。
代码传送门
来一把事件操作大合唱:
前提准备:
GestureDetector 操作手势识别器
ScaleGestureDetector 缩放手势识别器
在onTouchEvent中,分别通过gestureDetector.onTouchEvent(event)
scaleGestureDetector.onTouchEvent(event),接手手势事件
分别通过OnGestureListener和SimpleOnScaleGestureListener处理手势事件。
事件监听器:
class MyGestureDetectorListener implements GestureDetector.OnGestureListener{
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@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) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
class MyScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
return super.onScale(detector);
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return super.onScaleBegin(detector);
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
super.onScaleEnd(detector);
}
}
实现过程:
1.创建GestureFrame继承RelativeLayout,创建手势识别器
private void init() {
gestureDetector = new GestureDetector(getContext(),new MyGestureDetectorListener());
scaleGestureDetector = new ScaleGestureDetector(getContext(),new MyScaleListener());
}
2.在onTouchEvent中获取按下,抬起事件;判断起始点横坐标差值,判断是左滑动还是右滑动。
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX = event.getX();
callBack.callBack("按下");
break;
case MotionEvent.ACTION_UP:
endX = event.getX();
callBack.callBack("抬起");
if(endX - startX > 200){
callBack.rightEvent();
}else if(startX - endX > 200){
callBack.leftEvent();
}
break;
}
//将触摸事件传递给手势识别器处理
gestureDetector.onTouchEvent(event);
scaleGestureDetector.onTouchEvent(event);
return true;
}
3.onSingleTapUp()回调单击事件;onScroll()回调上下左右拖动事件;onLongPress()长按事件;onScale()回调缩放事件,detector.getScaleFactor()获取缩放比例;
class MyGestureDetectorListener implements GestureDetector.OnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
callBack.click(e.getX(),e.getY());
callBack.callBack("单击");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
curHeight += distanceY;
callBack.callBackScale("上下滑动 " + curHeight);
//distanceX 左右滑动值
return false;
}
@Override
public void onLongPress(MotionEvent e) {
callBack.callBack("长按");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
class MyScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
callBack.callBackScale("缩放比例: " + detector.getScaleFactor());
return super.onScale(detector);
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return super.onScaleBegin(detector);
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
super.onScaleEnd(detector);
}
}
GestureFrame完整代码:
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.RelativeLayout;
public class GestureFrame extends RelativeLayout{
private GestureDetector gestureDetector;
private ScaleGestureDetector scaleGestureDetector;
private IcallBack callBack;
private float startX;
private float endX;
private int curHeight;
public GestureFrame(Context context) {
super(context);
init();
}
public GestureFrame(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
gestureDetector = new GestureDetector(getContext(),new MyGestureDetectorListener());
scaleGestureDetector = new ScaleGestureDetector(getContext(),new MyScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX = event.getX();
callBack.callBack("按下");
break;
case MotionEvent.ACTION_UP:
endX = event.getX();
callBack.callBack("抬起");
if(endX - startX > 200){
callBack.rightEvent();
}else if(startX - endX > 200){
callBack.leftEvent();
}
break;
}
//将触摸事件传递给手势识别器处理
gestureDetector.onTouchEvent(event);
scaleGestureDetector.onTouchEvent(event);
return true;
}
class MyGestureDetectorListener implements GestureDetector.OnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
callBack.click(e.getX(),e.getY());
callBack.callBack("单击");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
curHeight += distanceY;
callBack.callBackScale("上下滑动 " + curHeight);
//distanceX 左右滑动值
return false;
}
@Override
public void onLongPress(MotionEvent e) {
callBack.callBack("长按");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
class MyScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
callBack.callBackScale("缩放比例: " + detector.getScaleFactor());
return super.onScale(detector);
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return super.onScaleBegin(detector);
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
super.onScaleEnd(detector);
}
}
public interface IcallBack{
void callBack(String string);
void callBackScale(String scale);
void leftEvent();
void rightEvent();
void click(float x,float y);
}
public void setCallback(IcallBack callback){
this.callBack = callback;
}
FocusBoxView完整代码:
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class FocusBoxView extends AppCompatImageView {
public FocusBoxView(Context context) {
super(context);
}
public FocusBoxView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void startFocus(float x,float y){
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
layoutParams.leftMargin = (int) (x - getMeasuredWidth() / 2);
layoutParams.topMargin = (int) (y - getMeasuredHeight() / 2);
setLayoutParams(layoutParams);
setVisibility(VISIBLE);
}
}
Mainactivity回调设置值,显示事件,左右滑动动画
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private GestureFrame gestureFrame;
private StringBuilder stringBuilder;
private TextView tvMsg;
private FocusBoxView ivClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureFrame = (GestureFrame) findViewById(R.id.gesture_frame);
textView = (TextView) findViewById(R.id.tv);
tvMsg = (TextView) findViewById(R.id.tv_Msg);
ivClick = (FocusBoxView) findViewById(R.id.iv_click);
stringBuilder = new StringBuilder();
gestureFrame.setCallback(new GestureFrame.IcallBack() {
@Override
public void callBack(String string) {
stringBuilder.append(string + " ");
textView.setText(stringBuilder.toString());
}
@Override
public void callBackScale(String scale) {
tvMsg.setText(scale);
}
@Override
public void leftEvent() {
TranslateAnimation animation = new TranslateAnimation(0,-300,0,0);
animation.setDuration(300);
tvMsg.startAnimation(animation);
tvMsg.setText("左滑");
}
@Override
public void rightEvent() {
TranslateAnimation animation = new TranslateAnimation(0,300,0,0);
animation.setDuration(300);
tvMsg.startAnimation(animation);
tvMsg.setText("右滑");
}
@Override
public void click(float x, float y) {
ivClick.startFocus(x,y);
}
});
}
}
4.activity_main.xml布局:
上下滑动需要加一个标志再触发,比如点击才能触发,不然会和左右滑动、缩放一起触发。界面显示效果没有去弄美观一些,还请见谅。程序猿持续进化,博客持续更新,敬请期待。