代码实现:
package com.example.g160628_android_20_gesture;
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 viewFlipper;
private int image[]={R.drawable.b,R.drawable.c,R.drawable.d};
private GestureDetector gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.vf_main_ViewFlipper);
//循环添加图片
for (int i = 0; i < image.length; i++) {
ImageView img = new ImageView(this);
img.setImageResource(image[i]);
viewFlipper.addView(img);
}
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();
viewFlipper.showPrevious();
//左进右出(实现特效)外部资源
viewFlipper.setInAnimation(MainActivity.this,R.anim.right_out);
viewFlipper.setOutAnimation(MainActivity.this,R.anim.left_in);
}
if(e1.getX()-e2.getX()>200){
Toast.makeText(MainActivity.this, "左滑,下一张", Toast.LENGTH_SHORT).show();
viewFlipper.showNext();
//左出右进(实现特效)外部资源
viewFlipper.setOutAnimation(MainActivity.this,R.anim.left_out);
viewFlipper.setInAnimation(MainActivity.this,R.anim.right_in);
}
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gd.onTouchEvent(event);
}
}
引用的外部资源有四个特效
在res文件夹下新建文件夹anim(在anim文件夹内写外部资源)
实现图片上一张外部资源:
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>
right_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>
实现图片下一张外部资源:
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:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="-360"></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"
android:toXDelta="0">
</translate>
<rotate
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="-360"></rotate>
</set>