Android属性动画赏析,android8.0属性动画原理分析

补间动画只是改变了View的视觉效果,而不会真正去改变View的属性。

如,将屏幕左上角的按钮 通过补间动画 移动到屏幕的右下角

点击当前按钮位置(屏幕右下角)是没有效果的,因为实际上按钮还是停留在屏幕左上角,补间动画只是将这个按钮绘制到屏幕右下角,改变了视觉效果而已。

aa86832a6bd7

image

ValueAnimator主要有三种估值器(TypeEvaluator)

ValueAnimator.ofFloat()采用默认的浮点型估值器(FloatEvaluator)

ValueAnimator.ofInt()采用默认的整型估值器(IntEvaluator)

ValueAnimator.ofObject()需要自己写估值器

ValueAnimator 类是先改变值,然后 手动赋值 给对象的属性从而实现动画;是 间接 对对象属性进行操作;

ObjectAnimator 类是先改变值,然后 自动赋值 给对象的属性从而实现动画;是 直接对对象属性进行操作;

aa86832a6bd7

image

ObjectAnimator也可以实现补间动画的效果,通过改变这些参数,需要有get/set方法

aa86832a6bd7

image

AnimationHandler:: MyFrameCallbackProvider::postFrameCallback(FrameCallback)

->Choreographer.postFrameCallback()

->Choreographer.postFrameCallbackDelayed()

->Choreographer. postCallbackDelayedInternal()

private void postCallbackDelayedInternal(int callbackType,

Object action, Object token, longdelayMillis) {

synchronized (mLock) {

final long now =SystemClock.uptimeMillis();

final long dueTime = now +delayMillis;//callback到达时间

mCallbackQueues[callbackType].addCallbackLocked(dueTime, action,token);//存到队列中

if (dueTime <= now) {

scheduleFrameLocked(now);

} else {

Message msg =mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action);

msg.arg1 = callbackType;

msg.setAsynchronous(true);

mHandler.sendMessageAtTime(msg,dueTime);

}

}

}

Choreographer. scheduleFrameLocked()

->Choreographer. scheduleVsyncLocked()

->DisplayEventReceiver.scheduleVsync() //安排接收Vsync信号,如有Vsync信号过来会调用到FrameDisplayEventReceiver. onVsync()

->FrameDisplayEventReceiver. onVsync()

->FrameDisplayEventReceiver. run()

->Choreographer. doFrame()

->doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos);

->CallbackRecord.run()

->((FrameCallback)action).doFrame(frameTimeNanos);

->AnimationHandler::mFrameCallback. doFrame()

->AnimationHandler .doAnimationFrame()

->callback.doAnimationFrame(frameTime)

->ValueAnimator. doAnimationFrame()

->ValueAnimator. animateBasedOnTime()

->ValueAnimator. animateValue()

-> PropertyValuesHolder. calculateValue(fraction) //在这里计算当前时间的动画值

void animateValue(float fraction){

fraction =mInterpolator.getInterpolation(fraction);

mCurrentFraction = fraction;

int numValues = mValues.length;//[ValueAnimator](https://www.jianshu.com/writer)这个值为1

for (int i = 0; i < numValues; ++i) {

mValues[i].calculateValue(fraction);// ValueAnimator计算mValues[0]

}

if (mUpdateListeners != null) {

int numListeners =mUpdateListeners.size();

for (int i = 0; i < numListeners;++i) {

mUpdateListeners.get(i).onAnimationUpdate(this);//回调

}

}

}

获取值

public Object getAnimatedValue() {

if (mValues != null && mValues.length> 0) {

returnmValues[0].getAnimatedValue();

}

// Shouldn't get here; should alwayshave values unless ValueAnimator was set up wrong

return null;

}

ObjectAnimator介绍

public static ObjectAnimator ofInt(T target, Property property, int...values) {

……

}

有多少个属性值变化,mValues也就有多少个值

ObjectAnimator. animateValue()除了调用ValueAnimator.animateValue去计算动画值,也会调用对象的set方法来设置属性值

void animateValue(float fraction) {

final Object target = getTarget();

if (mTarget != null && target== null) {

// We lost the target reference,cancel and clean up. Note: we allow null target if the

/// target has never been set.

cancel();

return;

}

super.animateValue(fraction);

int numValues = mValues.length;

for (int i = 0; i < numValues; ++i){

mValues[i].setAnimatedValue(target);//反射调用get方法

}

}

参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Android Studio古诗赏析代码的示例: ```java public class MainActivity extends AppCompatActivity { private TextView mTitleTextView; private TextView mAuthorTextView; private TextView mContentTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTitleTextView = findViewById(R.id.title_text_view); mAuthorTextView = findViewById(R.id.author_text_view); mContentTextView = findViewById(R.id.content_text_view); // 创建一个古诗对象 Poem poem = new Poem("登高", "杜甫", "风急天高猿啸哀,渚清沙白鸟飞回。\n无边落木萧萧下,不尽长江滚滚来。"); // 将古诗的标题、作者和内容显示在TextView中 mTitleTextView.setText(poem.getTitle()); mAuthorTextView.setText(poem.getAuthor()); mContentTextView.setText(poem.getContent()); } } class Poem { private String title; private String author; private String content; public Poem(String title, String author, String content) { this.title = title; this.author = author; this.content = content; } public String getTitle() { return title; } public String getAuthor() { return author; } public String getContent() { return content; } } ``` 这个示例中,我们创建了一个名为`Poem`的类,它有三个属性:标题、作者和内容。我们还创建了一个名为`MainActivity`的类,它是我们的主活动。在`onCreate`方法中,我们创建了一个`Poem`对象,并将其标题、作者和内容显示在三个TextView中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值