Activity实现
1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.view.MotionEvent; 4 import android.view.View; 5 import android.view.Window; 6 import android.view.animation.AnimationUtils; 7 import android.widget.Gallery; 8 import android.widget.ImageSwitcher; 9 import android.widget.ImageView; 10 11 import android.widget.Gallery.LayoutParams; 12 import android.widget.Toast; 13 import android.widget.ViewSwitcher.ViewFactory; 14 15 public class Switcher extends Activity implements 16 ViewFactory { 17 private ImageSwitcher is; 18 private int Iindex=0; 19 private Gallery gallery; 20 /** 21 * 按下点的X坐标 22 */ 23 private float downX; 24 private int DuringTime=10000; 25 26 27 private Integer[] mThumbIds = {R.drawable.a,R.drawable.b, R.drawable.c, 28 R.drawable.d,R.drawable.e, R.drawable.f, R.drawable.g}; 29 30 private Integer[] mImageIds ={R.drawable.a,R.drawable.b, R.drawable.c, 31 R.drawable.d,R.drawable.e, R.drawable.f, R.drawable.g}; 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 // TODO Auto-generated method stub 36 super.onCreate(savedInstanceState); 37 requestWindowFeature(Window.FEATURE_NO_TITLE); 38 setContentView(R.layout.switcher); 39 40 is = (ImageSwitcher) findViewById(R.id.switcher); 41 is.setFactory(this); 42 43 //is.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_bottom_in));//渐入效果 44 is.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left));//系统提供渐变效果 45 is.setOutAnimation(AnimationUtils.loadAnimation(this, 46 android.R.anim.slide_out_right));//渐出效果 47 //定时执行图片切换 48 is.postDelayed(new Runnable() { 49 public void run() { 50 if(Iindex==mImageIds.length-1){ 51 Iindex=0; 52 }else{ 53 Iindex++; 54 } 55 is.setImageResource(mImageIds[Iindex]); 56 is.postDelayed(this, DuringTime); 57 } 58 }, DuringTime); 59 60 61 } 62 63 @Override 64 public View makeView() { 65 ImageView i = new ImageView(this); 66 i.setBackgroundColor(0xFF000000); 67 i.setScaleType(ImageView.ScaleType.FIT_CENTER); 68 i.setLayoutParams(new ImageSwitcher.LayoutParams( 69 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 70 i.setImageResource(mImageIds[Iindex]); 71 i.setOnTouchListener(new View.OnTouchListener() { 72 @Override 73 public boolean onTouch(View view, MotionEvent event) { 74 switch (event.getAction()) { 75 case MotionEvent.ACTION_DOWN:{ 76 //手指按下的X坐标 77 downX = event.getX(); 78 break; 79 } 80 case MotionEvent.ACTION_UP:{ 81 float lastX = event.getX(); 82 //抬起的时候的X坐标大于按下的时候就显示上一张图片 83 if(lastX > downX){ 84 if(Iindex==0){ 85 Iindex=mImageIds.length-1; 86 }else{ 87 Iindex--; 88 } 89 is.setImageResource(mImageIds[Iindex]); 90 }else if(lastX < downX){ 91 if(Iindex==mImageIds.length-1){ 92 Iindex=0; 93 }else{ 94 Iindex++; 95 } 96 is.setImageResource(mImageIds[Iindex]); 97 } 98 } 99 100 break; 101 } 102 return true; 103 } 104 105 }); 106 //点击事件换图 107 /* i.setOnClickListener(new View.OnClickListener() { 108 @Override 109 public void onClick(View view) { 110 if(Iindex==mImageIds.length-1){ 111 Iindex=0; 112 }else{ 113 Iindex++; 114 } 115 is.setImageResource(mImageIds[Iindex]); 116 //sm("you click this picture"); 117 } 118 });*/ 119 return i; 120 } 121 122 123 private void sm(String mes){ 124 Toast.makeText(this,mes,Toast.LENGTH_SHORT).show(); 125 } 126 }
布局代码
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <ImageSwitcher android:id="@+id/switcher" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:layout_alignParentTop="true" 11 android:layout_alignParentLeft="true" 12 /> 13 </RelativeLayout>
特效定义
<?xml version="1.0" encoding="utf-8"?> <!-- 上下滑入式 --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <!--<translate android:duration="200" android:fromYDelta="100%p" android:toYDelta="0" />--> <scale android:interpolator= "@android:anim/decelerate_interpolator" android:fromXScale="0.0" android:toXScale="0.5" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:startOffset="0" android:duration="1000" android:repeatCount="1" android:repeatMode="reverse" /> </set>