一个简单的android图片浏览器demo(待完善)
1 package com.kevin.picturebrowser; 2 3 import java.lang.reflect.Field; 4 import java.util.ArrayList; 5 6 import android.os.Bundle; 7 import android.app.Activity; 8 import android.app.ActionBar.LayoutParams; 9 import android.view.Menu; 10 import android.view.MotionEvent; 11 import android.view.View; 12 import android.view.View.OnTouchListener; 13 import android.view.animation.AnimationUtils; 14 import android.widget.ImageSwitcher; 15 import android.widget.ImageView; 16 import android.widget.ViewSwitcher.ViewFactory; 17 18 public class MainActivity extends Activity implements ViewFactory 19 { 20 21 private ImageSwitcher imageSwitcher1; 22 private int downX,upX; 23 private ArrayList<Integer> imgPool = new ArrayList<Integer>(); //图片库 24 private int ImgPos = 0; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) 28 { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 32 InitialPicturePool(); 33 InitialImageSwitcher(); 34 BindTouchListener(); 35 36 } 37 38 39 @Override 40 public boolean onCreateOptionsMenu(Menu menu) 41 { 42 // Inflate the menu; this adds items to the action bar if it is present. 43 getMenuInflater().inflate(R.menu.main, menu); 44 return true; 45 } 46 47 private void InitialImageSwitcher() 48 { 49 imageSwitcher1 = (ImageSwitcher) findViewById(R.id.imageswitcher1); 50 imageSwitcher1.setFactory(this); 51 imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(this, 52 android.R.anim.fade_in)); 53 imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(this, 54 android.R.anim.fade_out)); 55 imageSwitcher1.setImageResource(imgPool.get(0)); 56 } 57 58 private void BindTouchListener() 59 { 60 imageSwitcher1.setOnTouchListener(new OnTouchListener() 61 { 62 63 @Override 64 public boolean onTouch(View v, MotionEvent event) 65 { 66 if (event.getAction() == MotionEvent.ACTION_DOWN) 67 { 68 downX = (int) event.getX(); //取得按下时的坐标 69 return true; 70 } 71 else if(event.getAction() == MotionEvent.ACTION_UP) 72 { 73 upX = (int) event.getX(); //取得松开时的坐标 74 if(upX - downX > 100) 75 { 76 imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation( 77 MainActivity.this, android.R.anim.slide_in_left)); 78 imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation( 79 MainActivity.this, android.R.anim.slide_out_right)); 80 imageSwitcher1.setImageResource(GetPreviousPicture()); 81 } 82 else if(downX - upX > 100) 83 { 84 imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation( 85 MainActivity.this, android.R.anim.fade_in)); 86 imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation( 87 MainActivity.this, android.R.anim.fade_out)); 88 imageSwitcher1.setImageResource(GetNextPicture()); 89 } 90 } 91 return false; 92 } 93 }); 94 } 95 96 @Override 97 public View makeView() 98 { 99 ImageView iv = new ImageView(this); 100 iv.setBackgroundColor(0xFF000000); 101 iv.setScaleType(ImageView.ScaleType.FIT_CENTER); 102 iv.setLayoutParams(new ImageSwitcher.LayoutParams( 103 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 104 return iv; 105 } 106 107 private void InitialPicturePool() 108 { 109 Field[] fields = R.drawable.class.getDeclaredFields(); 110 for(Field field : fields) 111 { 112 int index = 0; 113 try 114 { 115 index = field.getInt(R.drawable.class); 116 } 117 catch(IllegalArgumentException e) 118 { 119 e.printStackTrace(); 120 } 121 catch(IllegalAccessException e) 122 { 123 e.printStackTrace(); 124 } 125 126 imgPool.add(index); 127 } 128 } 129 130 private Integer GetNextPicture() 131 { 132 ImgPos++; 133 if(ImgPos > imgPool.size()-1) 134 ImgPos = 0; 135 136 return imgPool.get(ImgPos); 137 } 138 139 private Integer GetPreviousPicture() 140 { 141 ImgPos--; 142 if(ImgPos < 0) 143 ImgPos = imgPool.size()-1; 144 145 return imgPool.get(ImgPos); 146 } 147 }
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageSwitcher android:id="@+id/imageswitcher1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerVertical="true"> </ImageSwitcher> </RelativeLayout>