Android学习笔记之 ViewFlipper UI的简单用法

这是一个左右滑动的效果,这里以切换图片为例。

  1 public class ViewFlipper_Main extends Activity implements OnGestureListener {

  2 

  3     private ViewFlipper vFlipper;

  4     private GestureDetector gDetector;

  5 

  6     @Override

  7     protected void onCreate(Bundle savedInstanceState) {

  8         super.onCreate(savedInstanceState);

  9         setContentView(R.layout.viewflipper_activity);

 10 

 11         vFlipper = (ViewFlipper) this.findViewById(R.id.vFlipper);

 12         gDetector = new GestureDetector(this);

 13 

 14         Bitmap bitmap = BitmapFactory.decodeResource(getResources(),

 15                 R.drawable.test1);

 16         Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),

 17                 R.drawable.test2);

 18         Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(),

 19                 R.drawable.test3);

 20 

 21         vFlipper.addView(addImgView(bitmap));

 22         vFlipper.addView(addImgView(bitmap1));

 23         vFlipper.addView(addImgView(bitmap2));

 24 

 25     }

 26 

 27     public View addImgView(Bitmap bm) {

 28         ImageView imageView = new ImageView(this);

 29         imageView.setImageBitmap(bm);

 30         return imageView;

 31     }

 32 

 33 

 34     @Override

 35     public boolean onCreateOptionsMenu(Menu menu) {

 36         // Inflate the menu; this adds items to the action bar if it is present.

 37         getMenuInflater().inflate(R.menu.view_flipper__main, menu);

 38         return true;

 39     }

 40 

 41     @Override

 42     public boolean onTouchEvent(MotionEvent event) {

 43         // TODO Auto-generated method stub

 44         // return super.onTouchEvent(event);

 45         return this.gDetector.onTouchEvent(event);

 46     }

 47 

 48     @Override

 49     public boolean onDown(MotionEvent e) {

 50         // TODO Auto-generated method stub

 51         return false;

 52     }

 53 

 54     @Override

 55     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

 56             float velocityY) {

 57         // TODO Auto-generated method stub

 58         if (e1.getX() - e2.getX() > 120) {

 59             this.vFlipper.setInAnimation(AnimationUtils.loadAnimation(this,

 60                     R.anim.push_left_in));

 61             this.vFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,

 62                     R.anim.push_left_out));

 63             this.vFlipper.showNext();

 64             return true;

 65         } else if (e1.getX() - e2.getX() < -120) {

 66             this.vFlipper.setInAnimation(AnimationUtils.loadAnimation(this,

 67                     R.anim.push_right_in));

 68             this.vFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,

 69                     R.anim.push_right_out));

 70             this.vFlipper.showPrevious();

 71             return true;

 72         }

 73         return true;

 74 

 75     }

 76 

 77     @Override

 78     public void onLongPress(MotionEvent e) {

 79         // TODO Auto-generated method stub

 80 

 81     }

 82 

 83     @Override

 84     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

 85             float distanceY) {

 86         // TODO Auto-generated method stub

 87         return false;

 88     }

 89 

 90     @Override

 91     public void onShowPress(MotionEvent e) {

 92         // TODO Auto-generated method stub

 93 

 94     }

 95 

 96     @Override

 97     public boolean onSingleTapUp(MotionEvent e) {

 98         // TODO Auto-generated method stub

 99         return false;

100     }

101 

102 }
View Code

 

你可能感兴趣的:(viewflipper)