问题描述
我想说明一下在Android中如何使用视图动画,属性动画等.
I'd like to clarify using of view, propery, etc. animations in Android.
通常,我创建的动画没有任何Tread的东西-直接在我的Activity/Fagment/ViewClass中:
Usually, I create animation without any Tread's things - directly within my Activity/Fagment/ViewClass:
AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
alphaAnimation.setDuration(100);
mView.setAnimation(alphaAnimation);
alphaAnimation.start();
我将其理解为在主UI线程中执行的动画.
I understand this as an animation executing within main UI thread.
但是,如果我要在辅助线程中创建动画,这是正常的方法吗?
But if I will create an animation within a worker thread - will it be normal way?
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
final AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
alphaAnimation.setDuration(100);
mView.post(new Runnable() {
public void run() {
mView.setAnimation(alphaAnimation);
alphaAnimation.start();
}
});
}
}).start();
}
推荐答案
您的线程在这里没有实际作用.对 mView.post()
的调用实质上是调度可运行的程序,使其在下一个可用周期中在主线程上执行.您也可以省去启动新线程的工作,并在主线程上完成所有工作.这里没什么事.
Your thread has no real effect here. The call to mView.post()
essentially schedules that runnable to execute on the main thread in its next available cycle. You might as well save the effort of starting a new thread and do it all on the main thread. There is not much going on here.
动画的工作原理是,安排每帧大约每16毫秒在主线程上运行许多微小的工作.这是非常典型的.除非您看到需要优化的非常具体的问题,否则没有理由避免在主线程上进行此类工作.
Animations work by scheduling lots of tiny bits of work to run on the main thread with each frame, which is about every 16ms. This is very typical. Unless you are seeing a very specific problem that you need to optimize, there is no reason to avoid doing this kind of work on the main thread.
这篇关于android动画总是在UI线程中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!