layout
1 26 7 12 13 27 2819 20 26 32 33 34
Activity
1 package cn.itcast.image; 2 3 import java.io.File; 4 5 import android.app.Activity; 6 import android.app.ProgressDialog; 7 import android.net.Uri; 8 import android.os.Bundle; 9 import android.os.Environment; 10 import android.os.Handler; 11 import android.os.Message; 12 import android.view.GestureDetector; 13 import android.view.MotionEvent; 14 import android.view.View; 15 import android.view.animation.Animation; 16 import android.view.animation.AnimationUtils; 17 import android.widget.ImageView; 18 import android.widget.TextView; 19 import android.widget.ViewFlipper; 20 21 public class DemoActivity extends Activity { 22 private ViewFlipper vf; 23 private GestureDetector mGestureDetector; 24 private ProgressDialog pd; 25 private Handler handler = new Handler(){ 26 public void handleMessage(android.os.Message msg) { 27 ImageView iv = (ImageView) msg.obj; 28 vf.addView(iv); 29 }; 30 }; 31 @Override 32 public void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.main); 35 vf = (ViewFlipper) findViewById(R.id.vf); 36 pd = new ProgressDialog(this); 37 pd.setMessage("正在加载图片信息"); 38 // 创建手势识别器. 39 mGestureDetector = new GestureDetector( 40 new GestureDetector.SimpleOnGestureListener() { 41 42 // 手指在屏幕上滑动的时候 调用的方法. 43 // e1 手指第一次接触到屏幕的时候 对应的事件 44 // e2 手指离开屏幕的时候 对应的事件 45 // velocityx 水平方向的移动速度 46 @Override 47 public boolean onFling(MotionEvent e1, MotionEvent e2, 48 float velocityX, float velocityY) { 49 if (Math.abs(e1.getY() - e2.getY()) > 100) { 50 System.out.println("垂直方向移动过大"); 51 return true; 52 } 53 54 if (Math.abs(velocityX) < 100) { 55 System.out.println("滑动的太慢了..."); 56 return true; 57 } 58 if (e2.getX() - e1.getX() > 100) { 59 Animation inAnimation = AnimationUtils.loadAnimation(DemoActivity.this, R.anim.alpha_in); 60 Animation outAnimation = AnimationUtils.loadAnimation(DemoActivity.this, R.anim.alpha_out); 61 vf.setInAnimation(inAnimation); 62 vf.setOutAnimation(outAnimation); 63 vf.showPrevious(); 64 return true; 65 } 66 if (e1.getX() - e2.getX() > 100) { 67 Animation inAnimation = AnimationUtils.loadAnimation(DemoActivity.this, R.anim.tran_in); 68 Animation outAnimation = AnimationUtils.loadAnimation(DemoActivity.this, R.anim.tran_out); 69 vf.setInAnimation(inAnimation); 70 vf.setOutAnimation(outAnimation); 71 vf.showNext(); 72 return true; 73 } 74 return super.onFling(e1, e2, velocityX, velocityY); 75 } 76 77 }); 78 pd.show(); 79 new Thread() { 80 public void run() { 81 File file = Environment.getExternalStorageDirectory(); 82 83 File[] files = file.listFiles(); 84 for (File f : files) { 85 if (f.isFile() && f.getName().endsWith("jpg")) { 86 ImageView iv = new ImageView(DemoActivity.this); 87 iv.setImageURI(Uri.fromFile(f)); 88 Message msg = Message.obtain(); 89 msg.obj = iv; 90 handler.sendMessage(msg); 91 } 92 } 93 pd.dismiss(); 94 }; 95 }.start(); 96 97 } 98 99 @Override100 public boolean onTouchEvent(MotionEvent event) {101 102 mGestureDetector.onTouchEvent(event);// 使用手势识别器 识别当前的手势103 104 return super.onTouchEvent(event);105 }106 107 public void next(View view) {108 vf.showNext();109 }110 111 public void pre(View view) {112 vf.showPrevious();113 }114 }
动画效果的.xml文件
alpha_in.xml
1 26 7
1 26 7
1 29
tran_out.xml
1 29