//以下的代码实现了翻页操作的功能
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Alan on 2016/05/17 0017.
*/
public class TwoActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
private static final String TAG = "TwoLog";
GestureDetector gestureDetector;
ViewFlipper flipper;
//定义一个动画数组
Animation[] animation = new Animation[4];
final int FLIP = 50;
Handler h = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
init(this);
}
private void init(Context mContext) {
gestureDetector = new GestureDetector(this,this);
flipper = (ViewFlipper)findViewById(R.id.flipper);
//添加图片
flipper.addView(setImages(R.drawable.a001));
flipper.addView(setImages(R.drawable.a002));
flipper.addView(setImages(R.drawable.a003));
flipper.addView(setImages(R.drawable.a004));
flipper.addView(setImages(R.drawable.a005));
//创建动画
animation[0] = AnimationUtils.loadAnimation(mContext,R.anim.left_in);
animation[1] = AnimationUtils.loadAnimation(mContext,R.anim.left_out);
animation[2] = AnimationUtils.loadAnimation(mContext,R.anim.reghit_in);
animation[3] = AnimationUtils.loadAnimation(mContext,R.anim.reghit_out);
//消息接受,通过时钟发送的消息进行UI界面更改
h = new Handler(){
public void handleMessage(Message msg){
flipper.setInAnimation(animation[0]);
flipper.setOutAnimation(animation[1]);
flipper.showNext();
}
};
//添加了一个时钟 实现自动翻页的效果
Timer time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
h.sendEmptyMessage(0);
}
}, 1000, 4000);
}
private View setImages(int id){
ImageView im = new ImageView(this);
im.setImageResource(id);
im.setScaleType(ImageView.ScaleType.CENTER);
return im;
}
//绑定触摸屏事件提交给GestureDetector处理
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
@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) {
Log.i(TAG,"e1:"+e1.getX()+" e2:"+e2.getX());
//如果开始点击的x坐标减去最后放开的x坐标大于FLIP 则返回上一页
if(e1.getX() - e2.getX() > FLIP){
flipper.setInAnimation(animation[0]);
flipper.setOutAnimation(animation[1]);
flipper.showPrevious();
return true;
}else if(e2.getX() - e1.getX() > FLIP){
flipper.setInAnimation(animation[2]);
flipper.setOutAnimation(animation[3]);
flipper.showNext();
return true;
}
return false;
}
}
//left_in.xml动画文件源码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="700"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%"></translate>
</set>
//left_out.xml动画文件源码
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="700" android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="-100%" android:toYDelta="0%"></translate> </set>//reghit_in.xml动画文件源码
<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”>
<translate
android:duration=”700″
android:fromXDelta=”-100%”
android:fromYDelta=”0%”
android:toXDelta=”0%”
android:toYDelta=”0%”></translate>
</set>//reghit_out.xml动画文件源码
<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”>
<translate
android:duration=”700″
android:fromXDelta=”0%”
android:fromYDelta=”0%”
android:toXDelta=”100%”
android:toYDelta=”0%”></translate>
</set>//main2.xml布局效果源码 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <ViewFlipper android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/flipper"></ViewFlipper> </RelativeLayout> </android.support.design.widget.CoordinatorLayout>
源码链接:http://pan.baidu.com/s/1kUIG2CR 密码:reul