Android动画深入分析
动画分类
Android动画能够分3种:View动画。帧动画和属性动画;属性动画为API11的新特性。在低版本号是无法直接使用属性动画的。但能够用nineoldAndroids来实现(可是本质还是viiew动画)。
学习本篇内容主要掌握下面知识:
1,View动画以及自己定义View动画。
2,View动画的一些特殊使用场景。
3,对属性动画做了一个全面的介绍。
4,使用动画的一些注意事项。
view动画
android:shareInterpolator表示集合中的动画是否和集合共享同一个插值器,假设集合不指定插值器,那么子动画就须要单独指定所需的插值器或默认值。
view动画应用场景
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animationOrder="normal" android:delay="0.3" android:animation="@anim/anim_item"/> //--- animationOrder 表示子元素的动画的顺序,有三种选项: //normal(顺序显示)、reverse(逆序显示)和random(随机显示)。第一种。在布局中引用LayoutAnimation<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:shareInterpolator="true"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" /> <translate android:fromXDelta="300" android:toXDelta="0" /> </set>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layoutAnimation="@anim/anim_layout"/>另外一种。代码种使用Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_item); LayoutAnimationController controller = new LayoutAnimationController(animation); controller.setDelay(0.5f); controller.setOrder(LayoutAnimationController.ORDER_NORMAL); listview.setLayoutAnimation(controller);
帧动画
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@mipmap/lottery_1" android:duration="200" /> // ...省略非常多 <item android:drawable="@mipmap/lottery_6" android:duration="200" /> </animation-list>
然后
imageView.setImageResource(R.drawable.frame_anim); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); animationDrawable.start();//启动start,关闭stop
属性动画
属性动画作用属性
<set
android:ordering=["together" | "sequentially"]>
<objectAnimator
android:propertyName="string"
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
<animator
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
<set>
...
</set>
</set>里面有一个 ordering属性。主要是指定动画的播放顺序。
android:valueFrom --------变化開始值
android:valueTo ------------变化结束值
android:duration ---------持续时间
它主要有下面属性。
android:valueTo
android:duration
android:startOffset
android:repeatCount
android:repeatMode
android:valueType
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
R.anim.property_animator);
set.setTarget(myObject);//myObject表示作用的对象
set.start();插值器和估值器
public class IntEvaluator implements TypeEvaluator<Integer> {
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
int startInt = startValue;
return (int)(startInt + fraction * (endValue - startInt));
}
}上述代码就是计算当前属性所占总共的百分百。属性动画监听器
public static interface AnimatorListener {
void onAnimationStart(Animator animation); //动画開始
void onAnimationEnd(Animator animation); //动画结束
void onAnimationCancel(Animator animation); //动画取消
void onAnimationRepeat(Animator animation); //动画反复播放
}AnimatorUpdateListenerpublic static interface AnimatorUpdateListener {
void onAnimationUpdate(ValueAnimator animator);
}应用场景
- 给你的对象加上get和set方法,假设你有权限的话
- 用一个类来包装原始对象,间接提高get和set方法
- 採用ValueAnimator,监听动画运行过程。实现属性的改变
public class ViewWrapper {
private View target;
public ViewWrapper(View target) {
this.target = target;
}
public int getWidth() {
return target.getLayoutParams().width;
}
public void setWidth(int width) {
target.getLayoutParams().width = width;
target.requestLayout();
}
}另外一种,採用ValueAnimator。监听动画过程。
private void startValueAnimator(final View target, final int start, final int end) {
ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
private IntEvaluator mEvaluation = new IntEvaluator();//新建一个整形估值器作为暂时变量
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//获得当前动画的进度值 1~100之间
int currentValue = (int) animation.getAnimatedValue();
//获得当前进度占整个动画过程的比例,浮点型。0~1之间
float fraction = animation.getAnimatedFraction();
//调用估值器,通过比例计算出宽度
int targetWidth = mEvaluation.evaluate(fraction, start, end);
target.getLayoutParams().width = targetWidth;
//设置给作用的对象,刷新页面
target.requestLayout();
}
});
}属性动画的工作原理
然后通过消息机制(Handler(只是这里的Handler不是我们经常使用的handler。而是AnimationHandler,它事实上本质就是一个Runable)和Looper去将动画运行出来),通过代码我们发现它调了JNI的代码,只是这个我们不用关心。我们直接看ObjectAnimator.start()
private void start(boolean playBackwards) {
if(Looper.myLooper() == null) {
throw new AndroidRuntimeException("Animators may only be run on Looper threads");
} else {
this.mPlayingBackwards = playBackwards;
this.mCurrentIteration = 0;
this.mPlayingState = 0;
this.mStarted = true;
this.mStartedDelay = false;
((ArrayList)sPendingAnimations.get()).add(this);
if(this.mStartDelay == 0L) {
this.setCurrentPlayTime(this.getCurrentPlayTime());
this.mPlayingState = 0;
this.mRunning = true;
if(this.mListeners != null) {
ArrayList animationHandler = (ArrayList)this.mListeners.clone();
int numListeners = animationHandler.size();
for(int i = 0; i < numListeners; ++i) {
((AnimatorListener)animationHandler.get(i)).onAnimationStart(this);
}
}
}
ValueAnimator.AnimationHandler var5 = (ValueAnimator.AnimationHandler)sAnimationHandler.get();
if(var5 == null) {
var5 = new ValueAnimator.AnimationHandler(null);
sAnimationHandler.set(var5);
}
var5.sendEmptyMessage(0);
}
} private static final ThreadLocal<ArrayList<ValueAnimator>> sAnimations = new ThreadLocal() {
protected ArrayList<ValueAnimator> initialValue() {
return new ArrayList();
}
};
private static final ThreadLocal<ArrayList<ValueAnimator>> sPendingAnimations = new ThreadLocal() {
protected ArrayList<ValueAnimator> initialValue() {
return new ArrayList();
}
};
private static final ThreadLocal<ArrayList<ValueAnimator>> sDelayedAnims = new ThreadLocal() {
protected ArrayList<ValueAnimator> initialValue() {
return new ArrayList();
}
};
private static final ThreadLocal<ArrayList<ValueAnimator>> sEndingAnims = new ThreadLocal() {
protected ArrayList<ValueAnimator> initialValue() {
return new ArrayList();
}
};
private static final ThreadLocal<ArrayList<ValueAnimator>> sReadyAnims = new ThreadLocal() {
protected ArrayList<ValueAnimator> initialValue() {
return new ArrayList();
}
};这里系统怎么计算每一帧的动画的呢。看看下面的代码
void animateValue(float fraction) {
fraction = this.mInterpolator.getInterpolation(fraction);
this.mCurrentFraction = fraction;
int numValues = this.mValues.length;
int numListeners;
for(numListeners = 0; numListeners < numValues; ++numListeners) {
this.mValues[numListeners].calculateValue(fraction);
}
if(this.mUpdateListeners != null) {
numListeners = this.mUpdateListeners.size();
for(int i = 0; i < numListeners; ++i) {
((ValueAnimator.AnimatorUpdateListener)this.mUpdateListeners.get(i)).onAnimationUpdate(this);
}
}
}只是我们知道要改变动画。一定调用了get/set方法,那我们重点看下这相关的代码。这段代码在setProperty方法里面
public void setProperty(Property property) {
if(this.mValues != null) {
PropertyValuesHolder valuesHolder = this.mValues[0];
String oldName = valuesHolder.getPropertyName();
valuesHolder.setProperty(property);
this.mValuesMap.remove(oldName);
this.mValuesMap.put(this.mPropertyName, valuesHolder);
}
if(this.mProperty != null) {
this.mPropertyName = property.getName();
}
this.mProperty = property;
this.mInitialized = false;
}这里有一个PropertyValuesHolder,顾名思义这是一个操作数据的类,和我们的adapter的Holder几乎相同,该方法的get方法主要用到了反射。 private void setupValue(Object target, Keyframe kf) {
if(this.mProperty != null) {
kf.setValue(this.mProperty.get(target));
}
try {
if(this.mGetter == null) {
Class e = target.getClass();
this.setupGetter(e);
}
kf.setValue(this.mGetter.invoke(target, new Object[0]));
} catch (InvocationTargetException var4) {
Log.e("PropertyValuesHolder", var4.toString());
} catch (IllegalAccessException var5) {
Log.e("PropertyValuesHolder", var5.toString());
}
}代码就看到这,有兴趣的能够去看下源代码
浙公网安备 33010602011771号