Android UI 之 ViewFlipper 和 GestureDetector

简介

ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。一般不直接使用ViewAnimator而是使用它的两个子类ViewFlipper和ViewSwitcher。
ViewFlipper可以用来指定FrameLayout内多个View之间的切换效果(可以指定切换动画,切换的间隔等),可以一次性指定效果,也可以在每一个切换时指定切换效果。
ViewSwitcher顾名思义Switcher特指在两个View之间切换。可以通过该类指定一个ViewSwitcher.ViewFactory工程类来创建这两个View。该类也具有两个子类ImageSwitcher、TextSwitcher分别用于图片和文本切换

使用

activity_test.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context="ssss.com.jz.uitest.TestActivity">

    <Button
        android:id="@+id/btn"
        android:text="@android:string/emptyPhoneNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ViewFlipper
        android:layout_below="@+id/btn"
        android:id="@+id/flipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ViewFlipper>

</RelativeLayout>

pager.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="50sp"
        />
</FrameLayout>

注意onTouchEvent的使用,不然GestureDetector不会起作用
FlipTestAcitivity.java

public class FlipTestAcitivity extends Activity {
    private ViewFlipper mViewFlipper;
    private GestureDetector mGestrureDetetor;

    private static class AnimationUtils {
        private static Animation mInFromLeftAnimation;
        private static Animation mOutToRightAnimation;
        private static Animation mInFromRightAnimation;
        private static Animation mOutToLeftAnimation;

        static {
            mInFromLeftAnimation = 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);

            mInFromLeftAnimation.setDuration(350);
            mInFromLeftAnimation.setInterpolator(new AccelerateInterpolator());

            mOutToLeftAnimation = 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);

            mOutToLeftAnimation.setDuration(350);
            mOutToLeftAnimation.setInterpolator(new AccelerateInterpolator());

            mOutToRightAnimation = 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);

            mOutToRightAnimation.setDuration(350);
            mOutToRightAnimation.setInterpolator(new AccelerateInterpolator());

            mInFromRightAnimation = 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);

            mInFromRightAnimation.setDuration(359);
            mInFromRightAnimation.setInterpolator(new AccelerateInterpolator());
        }

        private static Animation getInFromLeftAnimation() {
            return mInFromLeftAnimation;
        }

        private static Animation getOutToLeftAnimation() {
            return mOutToLeftAnimation;
        }

        private static Animation getOutToRightAnimation() {
            return mOutToRightAnimation;
        }

        private static Animation getInFromRightAnimation() {
            return mInFromRightAnimation;
        }
    }

    private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        private static final int FLIP_PIXEL_THRESHOLD = 200;

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Toast.makeText(FlipTestAcitivity.this, "onDoubleTap", Toast.LENGTH_SHORT).show();
            return super.onDoubleTap(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (e2.getX() > (e1.getX() + FLIP_PIXEL_THRESHOLD)) {
                mViewFlipper.setInAnimation(AnimationUtils.getInFromLeftAnimation());
                mViewFlipper.setOutAnimation(AnimationUtils.getOutToRightAnimation());

                mViewFlipper.showPrevious();
                return false;
            }

            // going forwards: pushing stuff to the left
            if (e2.getX() < (e1.getX() - FLIP_PIXEL_THRESHOLD)) {
                mViewFlipper.setInAnimation(AnimationUtils.getInFromRightAnimation());
                mViewFlipper.setOutAnimation(AnimationUtils.getOutToLeftAnimation());

                mViewFlipper.showNext();
                return false;
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        mGestrureDetetor = new GestureDetector(this, new MyGestureListener());

        mViewFlipper = (ViewFlipper)findViewById(R.id.flipper);
        mViewFlipper.setPersistentDrawingCache(ViewGroup.PERSISTENT_ALL_CACHES);


        addNewView("I'm 1");
        addNewView("I'm 2");
        addNewView("I'm 3");
        addNewView("I'm 4");

        ((Button)findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(FlipTestAcitivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void addNewView(String text) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.pager, null);
        TextView textView = (TextView)view.findViewById(R.id.text);
        textView.setText(text);
        mViewFlipper.addView(view);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(mGestrureDetetor != null) {
            return mGestrureDetetor.onTouchEvent(event);
        }
        return super.onTouchEvent(event);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值