您的位置:华清远见教育科技集团 >> Android资料 >> Android 控件动画效果的实现  
 
Android 控件动画效果的实现
分享到:

Android的控件动画效果均是基于补间动画实现的,常用的控件动画是基于ViewAnimator类进行的,其子类包含ViewFlipper、ViewSwitcher、ImageSwitcher、TextSwitcher等。下面是日历应用中关于ViewSwitcher的一个实现:

代码:ViewSwitcher的应用

public View switchViews(boolean forward, float xOffSet, float width) {
    float progress = Math.abs(xOffSet) / width;
    if (progress > 1.0f) {
        progress = 1.0f;
        }
    float inFromXValue, inToXValue;
    float outFromXValue, outToXValue;
    if (forward) {
        inFromXValue = 1.0f - progress;
        inToXValue = 0.0f;
        outFromXValue = -progress;
        outToXValue = -1.0f;
    } else {
        inFromXValue = progress - 1.0f;
        inToXValue = 0.0f;
        outFromXValue = progress;
        outToXValue = 1.0f;
    }
    TranslateAnimation inAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, inFromXValue,
        Animation.RELATIVE_TO_SELF, inToXValue,
        Animation.ABSOLUTE, 0.0f,
        Animation.ABSOLUTE, 0.0f);
    TranslateAnimation outAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, outFromXValue,
        Animation.RELATIVE_TO_SELF, outToXValue,
        Animation.ABSOLUTE, 0.0f,
        Animation.ABSOLUTE, 0.0f);
    long duration = (long) (ANIMATION_DURATION * (1.0f - progress));
    inAnimation.setDuration(duration);
    outAnimation.setDuration(duration);
    mViewSwitcher.setInAnimation(inAnimation);
    mViewSwitcher.setOutAnimation(outAnimation);
    CalendarView view = (CalendarView) mViewSwitcher.getCurrentView();
    view.cleanup();
    mViewSwitcher.showNext();
    view = (CalendarView) mViewSwitcher.getCurrentView();
    view.requestFocus();
    view.reloadEvents();
    return view;
    }

对于控件动画,对于一些常见的动画效果,并不需要开发者自行实现,下面是一个利用ViewFlipper和系统动画实现的一个实例:

代码:ViewFlipper的应用

package com.miaozl.test;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.animation.AnimationUtils;
    import android.widget.ViewFlipper;
    public class ViewAnimActivity extends Activity {
        private ViewFlipper mFlipper;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper));
            mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
            mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
            mFlipper.startFlipping();    //循环播放
        }
    }

下面是相应的布局文件:

< ?xml version="1.0" encoding="utf-8"?>
    < LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    < ViewFlipper android:id="@+id/flipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:flipInterval="2000"
        android:autoStart="true">    //自动播放
    < ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/photo2"
        android:id="@+id/one"/>
    < ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/photo3"
        android:id="@+id/two"/>
    < /ViewFlipper>
    < /LinearLayout>

除了淡入淡出效果外,Android还支持下推(push down)、上推(push up)、shrink fade、grow fade等多种动画效果。

动画的进度是通过插补器(interpolator)来控制的,目前,Android支持七种插补器效果:加速减速插补器(accelerate decelerate interpolator)、加速插补器(accelerate interpolator)、预期插补器(anticipate interpolator)、预期超调插补器(anticipate_overshoot_interpolator)、弹跳插补器(bounce_interpolator)、圆插补器(cycle_interpolator)、减速插补器(decelerate interpolator)、线性插补器(linear_interpolator)、超调插补器(overshoot_interpolator)。如果没有适合读者的,读者也可以通过实现TimeInterpolator接口自定义一个自己的插补器。

 更多相关文章

·Android中的四种补间动画
·Android开发中的人脸检测技术
·Android中如何实现图像浏览
·Android图像旋转源码分享
·Android中多媒体缩略图的生成