赞
踩
最近开始学习安卓5.0新特性,今天学习到transition,不知道transition,请自我科普,怕因时间太久,故把学习心得整理如下:
TransitionManager.beginDelayedTransition(root)
root:是场景的父控件
简单的一句代码就能使此root控件下的元素变化带有默认的fade的效果。如果想指定其他动画,如下:
TransitionManager.beginDelayedTransition(root,new Explode());
场景布局代码:
<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>
* 重点提示:代码中的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>
自定义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;
}
}
至此场景切换模块结束,当然transition 5.0后加入Activity或Fragment切换的动画,这个下个模块进行讲解。
此文章完全是小编自己总结出来的,喜欢的同行请点赞以下哦!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。