ViewFlipper的使用

  1. 效果如下:

  2. 主要代码:

    MainActivity.java

        public class MainActivity extends Activity implements OnTouchListener,        OnGestureListener, OnDoubleTapListener {    private ViewFlipper mFlipper;    private GestureDetector mGestureDetector;    private int mCurrentLayoutState;    private static final int FLING_MIN_DISTANCE = 100;    private static final int FLING_MIN_VELOCITY = 200;    TextView counttv;    Button buttonNext1, buttonNext2;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        findView();        setListener();    }    public void findView() {        mFlipper = (ViewFlipper) findViewById(R.id.details);        // 注册一个用于手势识别的类        mGestureDetector = new GestureDetector(this);        mCurrentLayoutState = 0;        // 允许长按住ViewFlipper,这样才能识别拖动等手势        mFlipper.setLongClickable(true);        counttv = (TextView) findViewById(R.id.counttv);        buttonNext1 = (Button) findViewById(R.id.Button_next1);        buttonNext2 = (Button) findViewById(R.id.Button_next2);    }    public void setListener() {        // 给mFlipper设置一个listener        mFlipper.setOnTouchListener(this);        counttv.setText("9");        buttonNext1.setOnClickListener(new View.OnClickListener() {            public void onClick(View view) {                mFlipper.showNext();                counttv.setText("7");            }        });        buttonNext2.setOnClickListener(new View.OnClickListener() {            public void onClick(View view) {                mFlipper.showNext();                counttv.setText("8");            }        });    }    /**     * 此方法在本例中未用到,可以指定跳转到某个页面     */    public void switchLayoutStateTo(int switchTo) {        while (mCurrentLayoutState != switchTo) {            if (mCurrentLayoutState > switchTo) {                mCurrentLayoutState--;                mFlipper.setInAnimation(inFromLeftAnimation());                mFlipper.setOutAnimation(outToRightAnimation());                mFlipper.showPrevious();            } else {                mCurrentLayoutState++;                mFlipper.setInAnimation(inFromRightAnimation());                mFlipper.setOutAnimation(outToLeftAnimation());                mFlipper.showNext();            }        }        ;    }    /**     * 定义从右侧进入的动画效果 * @return     */    protected Animation inFromRightAnimation() {        Animation inFromRight = new TranslateAnimation(                Animation.RELATIVE_TO_PARENT, +1.0f,                Animation.RELATIVE_TO_PARENT, 0.0f,                Animation.RELATIVE_TO_PARENT, 0.0f,                Animation.RELATIVE_TO_PARENT, 0.0f);        inFromRight.setDuration(500);        inFromRight.setInterpolator(new AccelerateInterpolator());        return inFromRight;    }    /**     * 定义从左侧退出的动画效果 * @return     */    protected Animation outToLeftAnimation() {        Animation outtoLeft = new TranslateAnimation(                Animation.RELATIVE_TO_PARENT, 0.0f,                Animation.RELATIVE_TO_PARENT, -1.0f,                Animation.RELATIVE_TO_PARENT, 0.0f,                Animation.RELATIVE_TO_PARENT, 0.0f);        outtoLeft.setDuration(500);        outtoLeft.setInterpolator(new AccelerateInterpolator());        return outtoLeft;    }    /**     * 定义从左侧进入的动画效果 * @return     */    protected Animation inFromLeftAnimation() {        Animation inFromLeft = new TranslateAnimation(                Animation.RELATIVE_TO_PARENT, -1.0f,                Animation.RELATIVE_TO_PARENT, 0.0f,                Animation.RELATIVE_TO_PARENT, 0.0f,                Animation.RELATIVE_TO_PARENT, 0.0f);        inFromLeft.setDuration(500);        inFromLeft.setInterpolator(new AccelerateInterpolator());        return inFromLeft;    }    /**     * 定义从右侧退出时的动画效果 * @return     */    protected Animation outToRightAnimation() {        Animation outtoRight = new TranslateAnimation(                Animation.RELATIVE_TO_PARENT, 0.0f,                Animation.RELATIVE_TO_PARENT, +1.0f,                Animation.RELATIVE_TO_PARENT, 0.0f,                Animation.RELATIVE_TO_PARENT, 0.0f);        outtoRight.setDuration(500);        outtoRight.setInterpolator(new AccelerateInterpolator());        return outtoRight;    }    public boolean onDown(MotionEvent e) {        return false;    }    /*     * * 用户按下触摸屏、快速移动后松开即触发这个事件 * e1:第1个ACTION_DOWN MotionEvent *     * e2:最后一个ACTION_MOVE MotionEvent * velocityX:X轴上的移动速度,像素/秒 *     * velocityY:Y轴上的移动速度,像素/秒 * 触发条件 : *     * X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒     */    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,                           float velocityY) {        if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {            // 当像左侧滑动的时候 //设置View进入屏幕时候使用的动画            mFlipper.setInAnimation(inFromRightAnimation());            // 设置View退出屏幕时候使用的动画            mFlipper.setOutAnimation(outToLeftAnimation());            mFlipper.showNext();        } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {            // 当像右侧滑动的时候            mFlipper.setInAnimation(inFromLeftAnimation());            mFlipper.setOutAnimation(outToRightAnimation());            mFlipper.showPrevious();        }        return false;    }    public void onLongPress(MotionEvent e) {    }    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,                            float distanceY) {        return false;    }    public void onShowPress(MotionEvent e) {    }    public boolean onSingleTapUp(MotionEvent e) {        return false;    }    public boolean onTouch(View v, MotionEvent event) {        // 一定要将触屏事件交给手势识别类去处理(自己处理会很麻烦的)        return mGestureDetector.onTouchEvent(event);    }    public boolean onDoubleTap(MotionEvent e) {        return false;    }    public boolean onDoubleTapEvent(MotionEvent e) {        return false;    }    public boolean onSingleTapConfirmed(MotionEvent e) {        return false;    }}

    main.xml

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:orientation="vertical">    <FrameLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:orientation="vertical">        <TextView            android:id="@+id/counttv"            android:layout_width="50dip"            android:layout_height="wrap_content"            android:layout_gravity="right"            android:background="@drawable/a4" />    </FrameLayout>    <ViewFlipper        android:id="@+id/details"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:flipInterval="1000"        android:inAnimation="@anim/push_left_in"        android:outAnimation="@anim/push_left_out"        android:persistentDrawingCache="animation">        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical">            <Button                android:id="@+id/Button_next1"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:text="Next1" />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dip"                android:singleLine="true"                android:text="第一个View"                android:textSize="20dip" />        </LinearLayout>        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical">            <Button                android:id="@+id/Button_next2"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:text="Next2" />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dip"                android:singleLine="true"                android:text="第二个View"                android:textSize="20dip" />        </LinearLayout>    </ViewFlipper></LinearLayout>  

    由以上代码可以看出,ViewFlipper中有几个直接子View,就有几个左右页面.

  3. 方法:

    1. mFlipper.showNext();跳转到下一个View页面
  4. 源码:

    http://download.csdn.net/detail/lingwu7/9420393

更多相关文章

  1. Android组件 文字标签(TextView)
  2. java.lang.IllegalStateException: No host
  3. 给Android标题栏加上返回按钮
  4. ListView实现滚动动画
  5. android属性动画 —— ValueAnimator和ObjectAnimator的例子
  6. Android(安卓)自定义颜色ProgressBar
  7. Android(安卓)控件点击效果纯色和水波纹
  8. android view 动画详解
  9. 自定义开关按钮

随机推荐

  1. Android(安卓)Sensor详解(1)简介与架构
  2. 出版技术书籍的经历
  3. 使用ARouter进行Android模块化开发
  4. Android(安卓)studio打开机智云APP(自动
  5. Android中获取手机IMEI,IMSI, MAC(Android(
  6. Android应用发布后的统计——百度移动统
  7. Android通过Okhttp3实现socket长连接
  8. APP切图那点事儿–详细介绍android和ios
  9. Android(安卓)画板(简单的自定义控件)
  10. Android(安卓)monkey介绍