Android开发学习
图片左右滑动效果,第三种:自动切换图片,结束后跳到下一个Activity,使用控件ViewFlipper
新建xml布局文件:activity_main.xml,代码内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/ViewFlipper1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY" >
</ViewFlipper>
</LinearLayout>
使用线性布局LinearLayout,资源文件中的图片index1.jpg,index2.jpg,index3.jpg,index4.jpg
实现ThreeActivity.java代码:
package com.zhengdecai.slidingeffect;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
/**
* 图片左右滑动效果-自动切换图片到最后一张,结束后跳到下一个Activity
*
* @author 郑德才
*
*/
public class ThreeActivity extends Activity implements OnGestureListener {
private ViewFlipper flipper;
private GestureDetector detector;
private Handler nextHandler;
private int i = 1;
private int count = 4; //图片切换数量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detector = new GestureDetector(this);
flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);
flipper.addView(addTextView(R.drawable.index1));
flipper.addView(addTextView(R.drawable.index2));
flipper.addView(addTextView(R.drawable.index3));
flipper.addView(addTextView(R.drawable.index4));
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
nextHandler.sendMessage(new Message());
}
}, 2000, 2000);
nextHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if (i == count) {
Intent mainIntent = new Intent();
mainIntent.setClass(ThreeActivity.this, FourActivity.class); //下一个Activity
startActivity(mainIntent);
ThreeActivity.this.finish();
} else {
flipper.showNext();
}
i++;
}
};
}
/**
* 同时判断点击第三张图片时进入首页
* */
private View addTextView(int id) {
ImageView iv = new ImageView(this);
iv.setImageResource(id);
return iv;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return this.detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > 100) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
this.flipper.showNext();
return true;
} else if (e1.getX() - e2.getX() < -100) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
this.flipper.showPrevious();
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
运行效果图:
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。