天天看点

Android ViewFlipper触摸动画

 今天给大家介绍一下如何实现androd主页面的左右拖动效果。实现起来很简单,就是使用viewflipper来将您要来回拖动的view装在一起,然后与gesturedetector手势识别类来联动,确定要显示哪个view,加上一点点动画效果即可。比如当手指向左快速滑动时跳转到上一个view,手指向右快速滑动时跳转到下一个view,本例中使用图片作为各个view的页面,实现左右快速滑动显示不同的图片。

  

  首先来看看我们的layout,如下所示:

  <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

        <viewflipper android:id="@+id/flipper" android:layout_below="@+id/cockpitlayout" android:layout_height="fill_parent" android:layout_width="fill_parent">

                <include android:id="@+id/firstlayout" layout="@layout/first">

                <include android:id="@+id/secondlayout" layout="@layout/second">

                <include android:id="@+id/thirdlayout" layout="@layout/third">

                <include android:id="@+id/fourthlayout" layout="@layout/fourth">

        </include></include></include></include></viewflipper>

</linearlayout>

  如上所示,在viewflipper中放置多个layout(接下来会在不同的layout中来回滑动),viewflipper在同一个页面就显示其中一个layout。viewflipper中的四个layout很简单,我们就放置一张图片,如下所示:

<linearlayout android:gravity="center_vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

  <imageview android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/v1">

</imageview></linearlayout>

  接下来我们来看看activity,我们的activity需要实现两个接口ongesturelistener,ontouchlistener。具体的代码如下所示,代码中都有相应的注释,这里就不再详述。

package com.ideasandroid.demo;

import android.app.activity;

import android.os.bundle;

import android.view.gesturedetector;

import android.view.motionevent;

import android.view.view;

import android.view.gesturedetector.ongesturelistener;

import android.view.view.ontouchlistener;

import android.view.animation.accelerateinterpolator;

import android.view.animation.animation;

import android.view.animation.translateanimation;

import android.widget.viewflipper;

public class viewflipperdemo extends activity implements ongesturelistener,ontouchlistener{

        private viewflipper mflipper;

        gesturedetector mgesturedetector;

        private int mcurrentlayoutstate;

        private static final int fling_min_distance = 100;

        private static final int fling_min_velocity = 200;

        @override

        public void oncreate(bundle savedinstancestate) {

                super.oncreate(savedinstancestate);

                setcontentview(r.layout.main);

                mflipper = (viewflipper) findviewbyid(r.id.flipper);

                //注册一个用于手势识别的类

                mgesturedetector = new gesturedetector(this);

                //给mflipper设置一个listener

                mflipper.setontouchlistener(this);

                mcurrentlayoutstate = 0;

                //允许长按住viewflipper,这样才能识别拖动等手势

                mflipper.setlongclickable(true);

        }

        /**

         * 此方法在本例中未用到,可以指定跳转到某个页面

         * @param switchto

         */

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

                infromright.setduration(500);

                infromright.setinterpolator(new accelerateinterpolator());

                return infromright;

         * 定义从左侧退出的动画效果

        protected animation outtoleftanimation() {

                animation outtoleft = new translateanimation(

                                animation.relative_to_parent, -1.0f,

                outtoleft.setduration(500);

                outtoleft.setinterpolator(new accelerateinterpolator());

                return outtoleft;

         * 定义从左侧进入的动画效果

        protected animation infromleftanimation() {

                animation infromleft = new translateanimation(

                infromleft.setduration(500);

                infromleft.setinterpolator(new accelerateinterpolator());

                return infromleft;

         * 定义从右侧退出时的动画效果

        protected animation outtorightanimation() {

                animation outtoright = new translateanimation(

                outtoright.setduration(500);

                outtoright.setinterpolator(new accelerateinterpolator());

                return outtoright;

        public boolean ondown(motionevent e) {

                // todo auto-generated method stub

                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

                    // 当像右侧滑动的时候

                        mflipper.setinanimation(infromleftanimation());

                        mflipper.setoutanimation(outtorightanimation());

                        mflipper.showprevious();

            }

        public void onlongpress(motionevent e) {

        public boolean onscroll(motionevent e1, motionevent e2, float distancex,

                        float distancey) {

        public void onshowpress(motionevent e) {

        public boolean onsingletapup(motionevent e) {

        public boolean ontouch(view v, motionevent event) {

                // 一定要将触屏事件交给手势识别类去处理(自己处理会很麻烦的)

                return mgesturedetector.ontouchevent(event);

}

继续阅读