当前位置:   article > 正文

安卓Transition学习(一)_shell transition

shell transition

Android transition学习总结

最近开始学习安卓5.0新特性,今天学习到transition,不知道transition,请自我科普,怕因时间太久,故把学习心得整理如下:


  • 渐进显示效果

    TransitionManager.beginDelayedTransition(root)
    root:是场景的父控件

    简单的一句代码就能使此root控件下的元素变化带有默认的fade的效果。如果想指定其他动画,如下:
    TransitionManager.beginDelayedTransition(root,new Explode());

  • 指定场景(Scene)
    1. Scene.getSceneForLayout(sceneRoot,R.layout.activity_animations_scene1, this);
      第一个参数:是父控件
      第二个参数:场景布局
      第三个参数:上下文对象

场景布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/test"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#00ff00"/>

    <ImageView
        android:id="@+id/square_green"
       android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/circle_24dp"
        android:tint="@color/sample_green" />

    <ImageView
        android:id="@+id/square_red"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/circle_24dp"
        android:tint="@color/sample_red" />

    <ImageView
        android:id="@+id/square_blue"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_below="@+id/square_green"
        android:src="@drawable/circle_24dp"
        android:tint="@color/sample_blue" />

    <ImageView
        android:id="@+id/square_yellow"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/square_red"
        android:src="@drawable/circle_24dp"
        android:tint="@color/sample_yellow" />
</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

* 重点提示:代码中的id不要乱命名哦,此处与要切换的场景的id是对应。对,切换场景使,安卓就是依靠id寻找共同元素的。*

2.TransitionManager.go(scene1,TransitionInflater.from(MainActivity.this). inflateTransition(R.transition.slide_and_changebounds));

此处调用TransitionManager.go执行切换场景操作。

第一个参数:目标场景
第二个参数:动画xml布局/自定义Transition对象

xml代码:

"><transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:transitionOrdering="sequential">
    <slide android:interpolator="@android:interpolator/decelerate_cubic" />
    <changeBounds android:interpolator="@android:interpolator/bounce" />
</transitionSet> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

自定义Transition:

1.新建类并继承Transition,复写:

captureStartValues();captureEndValues(),createAnimator();三个方法

分别是保存要改变前的view属性,改变之后的属性,属性操作方法。

具体看代码:


import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.transition.Transition;
import android.transition.TransitionValues;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by admin on 2016/3/9.
 */
public class MyTransition extends Transition{
    private static final String PROPNAME_BACKGROUND =
            "com.android.jikexueyuan.transition:MyTransition:background";

    @Override
    public void captureStartValues(TransitionValues transitionValues) {
        View view = transitionValues.view;
        // Store its background property in the values map
        transitionValues.values.put(PROPNAME_BACKGROUND, view.getBackground());
    }

    @Override
    public void captureEndValues(TransitionValues transitionValues) {
        View view = transitionValues.view;
        // Store its background property in the values map
        transitionValues.values.put(PROPNAME_BACKGROUND, view.getBackground());
    }
    @Override
    public Animator createAnimator(ViewGroup sceneRoot,
                                   TransitionValues startValues, TransitionValues endValues) {
        // This transition can only be applied to views that are on both starting and ending scenes.
        if (null == startValues || null == endValues) {
            return null;
        }
        // Store a convenient reference to the target. Both the starting and ending layout have the
        // same target.
        final View view = endValues.view;
        // Store the object containing the background property for both the starting and ending
        // layouts.
        Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND);
        Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND);
        // This transition changes background colors for a target. It doesn't animate any other
        // background changes. If the property isn't a ColorDrawable, ignore the target.
        if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) {
            ColorDrawable startColor = (ColorDrawable) startBackground;
            ColorDrawable endColor = (ColorDrawable) endBackground;
            // If the background color for the target in the starting and ending layouts is
            // different, create an animation.
            if (startColor.getColor() != endColor.getColor()) {
                // Create a new Animator object to apply to the targets as the transitions framework
                // changes from the starting to the ending layout. Use the class ValueAnimator,
                // which provides a timing pulse to change property values provided to it. The
                // animation runs on the UI thread. The Evaluator controls what type of
                // interpolation is done. In this case, an ArgbEvaluator interpolates between two
                // #argb values, which are specified as the 2nd and 3rd input arguments.
                ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(),
                        startColor.getColor(), endColor.getColor());
                // Add an update listener to the Animator object.
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        Object value = animation.getAnimatedValue();
                        // Each time the ValueAnimator produces a new frame in the animation, change
                        // the background color of the target. Ensure that the value isn't null.
                        if (null != value) {
                            view.setBackgroundColor((Integer) value);
                        }
                    }
                });
                // Return the Animator object to the transitions framework. As the framework changes
                // between the starting and ending layouts, it applies the animation you've created.
                return animator;
            }
        }
        // For non-ColorDrawable backgrounds, we just return null, and no animation will take place.
        return null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

至此场景切换模块结束,当然transition 5.0后加入Activity或Fragment切换的动画,这个下个模块进行讲解。

此文章完全是小编自己总结出来的,喜欢的同行请点赞以下哦!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/952675
推荐阅读
相关标签
  

闽ICP备14008679号