动画的分类
无    2016-03-14 18:50:05    491    0    0
xianglijiaxing

1、Drawable Animation:  帧动画   

        AnimationDrawable​

 设置为view 的background 属性;

The timebase is android.os.SystemClock#uptimeMillis.

 

Drawable.Callback

 

 

两种实现实现方式:


①代码中:

animationDrawable.addFrame(@NonNull Drawable frame, int duration);

 

②在res/drawable/目录下创建:

<?xml version="1.0" encoding="utf-8"?>
<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">

    <item android:drawable="@drawable/a04" android:duration="200"></item>
    <item android:drawable="@drawable/a03" android:duration="200"></item>
    <item android:drawable="@drawable/a02" android:duration="200"></item>
    <item android:drawable="@drawable/a01" android:duration="200"></item>
    <item android:drawable="@drawable/a06" android:duration="200"></item>
</animation-list>

oneshot 动画播放次数,ture表示只播放一次,false表示循环播放

duration 动画播放时间(每帧)

 

AnimationDrawable.AnimationState 存储配置的动画帧和动画相关其他属性;​


2、View Animation:  视图动画

 2.1、分类

    TranslateAnimation​

    RotateAnimation

    ScaleAnimation

    AlphaAnimation

 

    AnimationSet​

    ①代码实现

    ②在res/anim/目录下创建:

 

 2.2、LayoutAnimation 作用于ViewGroup.

    特殊使用场景:

        例如:listView的条目逐行显示

示例一:

 简单使用

布局文件中使用

android:animateLayoutChanges="true"​

 

示例二“

    LayoutAnimationController:

        ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 1f, 0f, 1f);
        scaleAnimation.setDuration(1000);
        scaleAnimation.setInterpolator(new LinearInterpolator());
//        Interpolator
        LayoutAnimationController controller = new LayoutAnimationController(scaleAnimation);
        //new LayoutAnimationController(scaleAnimation,0.5f);  指定offset
        root.setLayoutAnimation(controller);​


 


 

anim/ 下创建布局动画 ,以layoutAnimation为根部局:

<layoutAnimation 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:animationOrder="normal|reverse|random"
    android:delay="延时时间"
    android:animation="指定动画">
</layoutAnimation>
 ​

 

 

2.3、Activity的切换效果:

    public void overridePendingTransition (int enterAnim, int exitAnim)

        注意:必须在StartActivity或者finish()之后调用。

 

2.4、为Fragment添加动画效果:

    通过FragmentTransaction中的setCustomeAnimations()来添加切换动画。

        

2.5、View动画的监听:

   方法:  setAnimationListener();       接口:AnimationListener

 

2.6、自定义View动画

    public class CustomAnimation extends Animation {

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        
        
    }
}

    

 

 

3、Property Animation:     属性动画

    前提条件:对象的属性必须要有get和set方法;

     默认时间间隔:300ms

      默认帧率:10ms/帧

 API11以下使用属性动画:NineOldAndroids    http://nineoldandroids.com/ 

 

    ValueAnimator​
    ObjectAnimator

    AnimatorSet    // 编排动画执行顺序

 

  playTogether();​
animatorSet.playSequentially(animators)​
animSet.play(rotate).with(fadeInOut).after(moveIn);  ​

AnimatorSet的几个控制动画顺序的函数
play(Animator anima) –> 设置一个参考动画anima

with(Animator anima) –> 跟参考动画同时执行anima

before(Animator anima) –> 在参考动画之前执行anima

after(Animator anima) –> 在参考动画之后执行anima

after(long delay) –> 在参考动画之后延迟delay毫秒,才执行参考动画之后的动画

 

 

 

    ①代码实现

 

    mValuewAnimator = ObjectAnimator.ofInt(mTextView, "backgroundColor", /*Red*/0xFFFF8080, /*Blue*/0xFF8080FF);
        mValuewAnimator.setDuration(2000);
        mValuewAnimator.setEvaluator(new ArgbEvaluator());
        mValuewAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mValuewAnimator.setRepeatMode(ValueAnimator.REVERSE);
        mValuewAnimator.start();    

    ②在res/animator/目录下创建:

        

        AnimatorInflater.loadAnimator()​;

        AnimatorInflater : setTarget();

 

3.2、 属性动画的差值器和估值器:


        public void setEvaluator (TypeEvaluator value);

         

        public void setInterpolator (TimeInterpolator value);

        

https://developer.android.com/reference/android/view/animation/BaseInterpolator.html

 

 

    自定义差值器和估值器:

        自定义差值器需要实现Interpolator或者TimeInterpolator接口

        自定义估值器需要实现Evaluator接口

 

       3.3、属性动画的监听

 

        方法:addUpdateListener();​        接口:AnimatorUpdateListener;

                   使用animation.getAnimatedFraction();获取百分比,然后使用估值器进行估值。​

       

        方法:addListener​()                     接口:AnimatorListener;       

                                                            AnimatorListenerAdapter 实现了Animator.AnimatorListener​接口,均为空实现

        

    3.4 组合动画

ObjectAnimator moveIn = ObjectAnimator.ofFloat(textview, "translationX", -500f, 0f);  
ObjectAnimator rotate = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f);  
ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);  
AnimatorSet animSet = new AnimatorSet();  
animSet.play(rotate).with(fadeInOut).after(moveIn);  
animSet.setDuration(5000);  
animSet.start();

 

3.5、对任意属性做动画

 

    例如:Button extends TextView

        其宽度width的get和set方法如下:

    @ViewDebug.ExportedProperty(category = "layout")
    public final int getWidth() {
        return mRight - mLeft;
    }
    @android.view.RemotableViewMethod
    public void setWidth(int pixels) {
        mMaxWidth = mMinWidth = pixels;
        mMaxWidthMode = mMinWidthMode = PIXELS;

        requestLayout();
        invalidate();
    }

   发现setWidth并不是对Button的宽度进行设置!但是我们可以通过getLayoutParams等来进行设置     

 

    针对这种情况,我们有三方方法进行解决:

    ①、如果你有权限的话,给对象加上get和set方法。

    ②、用一个类来包装原始的对象,对提供get和set方法。

    ViewWrapper mViewWrapper = new ViewWrapper(mButton);
    ObjectAnimator.ofInt(mViewWrapper, "width", mViewWrapper.getWidth(), 500).setDuration(3000)
    .start();     

 

    private class ViewWrapper {
        
        private View mTargetView;

        public ViewWrapper(@NonNull View mTargetView) {
            if(null == mTargetView){
                throw new UnsupportedOperationException ("参数不能为null");
            }
            this.mTargetView = mTargetView;
        }

        public void setWidth(int width){
            final ViewGroup.LayoutParams layoutParams = mTargetView.getLayoutParams();
            layoutParams.width = width;
            mTargetView.setLayoutParams(layoutParams);
//            mTargetView.requestLayout();
//            mTargetView.invalidate();
        }

        public int getWidth(){
            return mTargetView.getWidth();
        }

    }        

    ③、采用ValueAnimator,监听动画的改变,自己实现属性的改变。

                使用animation.getAnimatedValue();​获取计算出的属性值;

                使用animation.getAnimatedFraction();获取百分比,然后使用估值器进行估值。​

 

 

ViewPropertyAnimator

v.animate().translationX(12).setInterpolator(new DecelerateInterpolator()).start();


Viewcompact.animate(view).*

 

        

上一篇: Android Studio之版本控制工具的使用之SVN

下一篇:

491 人读过
立即登录, 发表评论.
没有帐号? 立即注册
0 条评论
文档导航