本文实例讲述了Android ViewFlipper用法。分享给大家供大家参考,具体如下:
这里实现的效果是当手动滑动手机屏幕时会一个一个地显示图片,一次显示一张图片
package com.my.viewflippertest; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.widget.TextView; import android.widget.ViewFlipper; public class ViewFlipperTestActivity extends Activity implements OnGestureListener { private ViewFlipper vf;//声明一个ViewFlipper对象 private GestureDetector gd; private int[] d={//定义一个图片集合 R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.h, R.drawable.i, R.drawable.g }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gd=new GestureDetector(this); vf=(ViewFlipper)this.findViewById(R.id.vf);//初始化对象 for(int i=0;i<d.length;i++){ vf.addView(addTextView(d[i]));//添加界面 } } public TextView addTextView(int a){//把图片加入TextView中 TextView tv=new TextView(this); tv.setBackgroundResource(a); return tv; } //触发接触事件 public boolean onTouchEvent(MotionEvent event){ return this.gd.onTouchEvent(event); } //接触事件发生后显示下一张图片 public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){ this.vf.showNext(); return true; } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } }
关键字词: