package com.example.myviewfilpper;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
private ViewFlipper flipper;
private int[] icon = { R.drawable.feiyiyuan, R.drawable.haiyangguan, R.drawable.hezhao };
private float startX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flipper = (ViewFlipper) findViewById(R.id.viewfilpper);
for (int i = 0; i < icon.length; i++) {
flipper.addView(getImage(icon[i]));
}
/*
* flipper.setInAnimation(this,R.anim.left_in);
* flipper.setOutAnimation(this,R.anim.left_out);
* flipper.setFlipInterval(3000);
* flipper.startFlipping();
*/
Toast.makeText(this, "哈哈", 1).show();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
startX = event.getX();
}
case MotionEvent.ACTION_HOVER_MOVE: {
// 向右滑动看一页
if (event.getX()-startX > 50) {
Toast.makeText(this, "呵呵", 1).show();
flipper.setInAnimation(this, R.anim.left_in);
flipper.setOutAnimation(this, R.anim.left_out);
flipper.showPrevious();
}
// 向左滑动看一页
if (startX-event.getX() > 50) {
flipper.setInAnimation(this, R.anim.right_in);
flipper.setOutAnimation(this, R.anim.right_out);
flipper.showNext();
}
break;
}
case MotionEvent.ACTION_UP: {
break;
}
}
return super.onTouchEvent(event);
}
private ImageView getImage(int resId) {
ImageView image = new ImageView(this);
// 这样写图片的大小完全取决于图片的大小,用setBackgroundResource是全屏显示,但是图片和屏幕大小不一样时,会按照xy方向拉升或者缩小
//image.setImageResource(resId);
image.setBackgroundResource(resId);
return image;
}
}