package liu;
import com.example.demo_0330.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitcher_Activity extends Activity implements ViewFactory,
OnTouchListener {
private ImageSwitcher imageswitcher;
private int index;
private int[] images = { R.drawable.mingren, R.drawable.xiaoying,
R.drawable.zuozhu, R.drawable.xuanfuqiu };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageswitcher_activity);
imageswitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageswitcher.setFactory(this);
imageswitcher.setOnTouchListener(this);
}
// 适配器
public View makeView() {
ImageView im = new ImageView(this);
im.setImageResource(images[0]);
return im;
}
float startX=0.0f;
float netX=0.0f;
// 滑屏事件
public boolean onTouch(View v, MotionEvent event) {
int action=event.getAction();//获取当前事件
//action==MotionEvent.ACTION_DOWN 表示触碰获得焦点
if (action==MotionEvent.ACTION_DOWN) {
startX=event.getX();
return true;
}
if (action==MotionEvent.ACTION_UP) {
netX=event.getX();
if (startX-netX>20) {
index=index+1<images.length?++index:0;
//动画效果
imageswitcher.setInAnimation(this, android.R.anim.fade_in);
imageswitcher.setOutAnimation(this, android.R.anim.fade_out);
}else if(netX-startX>20){
index=index-1>=0?--index:images.length-1;
//动画效果
imageswitcher.setInAnimation(this, android.R.anim.fade_in);
imageswitcher.setOutAnimation(this, android.R.anim.fade_out);
}
imageswitcher.setImageResource(images[index]);
}
return false;
}
}