1.两种手势:
手势检测与添加手势(用户手势)。
2.手势实现翻页的效果,如:阅读的上一页与下一页。。。
用控件ViewFlipper 与 GestureDetector 来实现.
3.代码提示:
package com.zking.gesture;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class MainActivity extends AppCompatActivity {
private ViewFlipper main_viewflipper;
private int images[]={R.drawable.s1,R.drawable.s5,R.drawable.s10,R.drawable.s11};
private GestureDetector gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_viewflipper = (ViewFlipper) findViewById(R.id.main_viewflipper);
for (int i = 0; i < images.length; i++) {
ImageView imageView=new ImageView(this);
imageView.setImageResource(images[i]);
main_viewflipper.addView(imageView);
}
gd = new GestureDetector(this, new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
Log.i("test","按下");
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Log.i("test","按下,抬起之前");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.i("test","轻按");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.i("test","滚动");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Log.i("test","长按");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.i("test","拖动");
if(e2.getX()-e1.getX()>=200){
Log.i("test","向右滑");
main_viewflipper.showNext();
}
if(e1.getX()-e2.getX()>=200){
Log.i("test","向左滑");
main_viewflipper.showPrevious();
}
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gd.onTouchEvent(event);
}
}
3.引用动画:
vf_main_image.setInAnimation(MainActivity.this,R.anim.left_in);
vf_main_image.setOutAnimation(MainActivity.this,R.anim.right_out) ,用此代码。