ViewFlipper的用法
1>比如滑动手指,界面切换.就可以使用ViewFlipper来实现
ViewFlipper简单的使用:
先在layout配置文件里面添加两个按钮和一个ViewFlipper控件
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="pre" android:text="上一个" /> <Button android:id="@+id/button2" android:onClick="next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一个" /> <ViewFlipper android:id="@+id/flipper" android:layout_width="wrap_content" android:layout_height="wrap_content" ></ViewFlipper>
然后在activity中添加如下代码:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); flipper = (ViewFlipper) this.findViewById(R.id.flipper); //往flipper添加三个textView TextView tv1 = new TextView(this); tv1.setText("text1"); flipper.addView(tv1); TextView tv2 = new TextView(this); tv2.setText("text2"); flipper.addView(tv2); TextView tv3 = new TextView(this); tv3.setText("text3"); flipper.addView(tv3); } public void pre(View view) { flipper.showPrevious(); } public void next(View view) { flipper.showNext(); }
通过单击上一个/下一个进行不同view切换.
当然这只是简单的初步体验,接下来让我们继续他的学习
当我们用手指滑动屏幕的时候,让它实现view的切换.
现在来实现一下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="pre" android:text="上一个" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="next" android:text="下一个" /> </LinearLayout> <ViewFlipper android:id="@+id/vf" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview3" /> </ViewFlipper> </LinearLayout>
要实现这个功能首先要手势识别,
public class DemoActivity extends Activity { ViewFlipper vf; GestureDetector mGestureDetector; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); vf = (ViewFlipper) this.findViewById(R.id.vf); //完成手势识别器的初始化 mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener(){ // 手指滑动屏幕的时候 调用的方法 // e1 是第一触摸屏幕时候的事件 // e2 手指离开时候 的事件 // velocityX x方向的速度 // velocityY y方向的速度 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //如果垂直方向移动的距离过大 就是无效的手势 if(Math.abs(e1.getY()-e2.getY()) >100 ){ return false; } //判断向右滑动屏幕的事件 if(e2.getX()-e1.getX()>100&& Math.abs(velocityX)>100){ // 指定下一个view对象进来时候的动画效果 AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f); aa.setDuration(2000); vf.setInAnimation(aa); //设置当前view对象出去时候的动画效果 AlphaAnimation outaa = new AlphaAnimation(1.0f, 0.0f); aa.setDuration(2000); vf.setOutAnimation(outaa); //显示下一个内容 vf.showNext(); } //判断向左滑动屏幕的事件 if(e1.getX()-e2.getX()>100&& Math.abs(velocityX)>100){ // 指定下一个view对象进来时候的动画效果 AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f); aa.setDuration(2000); vf.setInAnimation(aa); //设置当前view对象出去时候的动画效果 AlphaAnimation outaa = new AlphaAnimation(1.0f, 0.0f); aa.setDuration(2000); vf.setOutAnimation(outaa); //显示上一个内容 vf.showPrevious(); } return super.onFling(e1, e2, velocityX, velocityY); } }); } //按钮的单击事件 public void pre(View view){ vf.showPrevious(); } public void next(View view){ vf.showNext(); } // 处理触摸事件的分发,如果touch事件没有被消费掉放回false,反之返回true //提高touch的灵敏性 @Override public boolean dispatchTouchEvent(MotionEvent ev) { boolean flag = mGestureDetector.onTouchEvent(ev); if(!flag){ flag = super.dispatchTouchEvent(ev); } return flag; } @Override public boolean onTouchEvent(MotionEvent event) { // 需要通过手势识别器 去识别触摸的动作 mGestureDetector.onTouchEvent(event); return true; } }
希望对大家有帮助,有误的地方请大家不吝指正...