android手势控制
实现思路:实现这个要使用一个ViewFlipper
获取手指的XY 判断滑动X Y加还是剪 判断左滑还是右滑
这是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.android_07_10.MainActivity">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vf_main_image"
>
</ViewFlipper>
</LinearLayout>
然后是MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class MainActivity extends AppCompatActivity {
private ViewFlipper vf_main_image;
private int images[]={R.drawable.s1,R.drawable.s2,R.drawable.s3};
private GestureDetector gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vf_main_image = (ViewFlipper) findViewById(R.id.vf_main_image);
for (int i = 0; i < images.length; i++) {
ImageView iv=new ImageView(this);
iv.setImageResource(images[i]);
vf_main_image.addView(iv);
}
//实例化手势检测器
gd = new GestureDetector(this, new 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) {
if(e2.getX()-e1.getX()>200){
Toast.makeText(MainActivity.this, "右滑,上一张", Toast.LENGTH_SHORT).show();
vf_main_image.showPrevious();
//左进右出
vf_main_image.setInAnimation(MainActivity.this,R.anim.left_in);
vf_main_image.setOutAnimation(MainActivity.this,R.anim.right_out);
}
if (e1.getX()-e2.getX()>200){
Toast.makeText(MainActivity.this, "左滑,下一张", Toast.LENGTH_SHORT).show();
vf_main_image.showNext();
//左进左出
vf_main_image.setInAnimation(MainActivity.this,R.anim.right_in);
vf_main_image.setOutAnimation(MainActivity.this,R.anim.left_out);
}
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gd.onTouchEvent(event);
}
}
在res中新建一个anim文件夹
里面 可以是使用你需要滑动的效果
我这里随意定义了几个xml 用来展示
left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:duration="1000"
>
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
></translate>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
></rotate>
</set>
left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:duration="1000"
>
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
></translate>
<rotate
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
></rotate>
</set>
right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:duration="1000"
>
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
></translate>
<rotate
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
></rotate>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:duration="1000"
>
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
></translate>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
></rotate>
</set>
然后运行 就可以了 一个简单的手势控制 就实现了