Android开发学习
图片左右滑动效果,第二种:自动切换图片,使用控件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
实现TwoActivity.java代码:
package com.zhengdecai.slidingeffect;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
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;
/**
* 图片左右滑动效果-自动切换图片、循环播放
*
* @author 郑德才
*
*/
public class TwoActivity extends Activity implements OnGestureListener {
private ViewFlipper flipper;
private GestureDetector detector;
private Handler nextHandler;
@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);
flipper.showNext();
}
};
}
/**
* 添加图片资源到ViewFlipper中
* */
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;
}
}
运行效果图:
评论列表:
谢谢!
恋字坊http://www.lianzifang.com/
我来了,既然来了我就得说几句!只说几句而已!如果我不说几句!就对不起人了,既然我要说几句!那么肯定是要说话的~所以我决定要说几句了。