Android 我的异步任务无法顺利更新UI(动画)
我想让文本视图一点一点地出现,就像动画一样。问题是,动画不是平滑的。它有时会被卡住一段时间,然后恢复。有时甚至更糟的是,它可以追溯到。。。我的意思是,文本视图变得越来越大,但突然变得越来越小,然后又越来越大。有人能帮我吗Android 我的异步任务无法顺利更新UI(动画),android,android-asynctask,android-animation,android-ui,Android,Android Asynctask,Android Animation,Android Ui,我想让文本视图一点一点地出现,就像动画一样。问题是,动画不是平滑的。它有时会被卡住一段时间,然后恢复。有时甚至更糟的是,它可以追溯到。。。我的意思是,文本视图变得越来越大,但突然变得越来越小,然后又越来越大。有人能帮我吗 private class UnfoldTask extends AsyncTask<Integer, Integer, Integer> { View view; public UnfoldTask(View v) { this
private class UnfoldTask extends AsyncTask<Integer, Integer, Integer> {
View view;
public UnfoldTask(View v) {
this.view = v;
ViewGroup.LayoutParams pa = view.getLayoutParams();
pa.height = 0;
view.setLayoutParams(pa);
}
@Override
protected Integer doInBackground(Integer... maxHeight) {
ViewGroup.LayoutParams pa = view.getLayoutParams();
while (pa.height < maxHeight[0]) {
pa.height += (int) (24 * getResources().getDisplayMetrics().density + 0.5f);
sleep(100);
publishProgress(pa.height);
}
return maxHeight[0];
}
private void sleep(int i) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
protected void onProgressUpdate(Integer... values) {
ViewGroup.LayoutParams pa = view.getLayoutParams();
pa.height = values[0];
view.setLayoutParams(pa);
}
@Override
protected void onPostExecute(Integer result) {
ViewGroup.LayoutParams pa = view.getLayoutParams();
pa.height = result;
view.setLayoutParams(pa);
}
}
您应该为此使用缩放动画。下面是一个例子:
ScaleAnimation animation = new ScaleAnimation(1, 2, 1, 2, centerX, centerY); // Scales from normal size (1) to double size (2). centerX/Y is the center of your text view. Change this to set the pivot point of your animation.
animation.setDuration(1000);
myTextView.startAnimation(animation);
您可以使用来简化此操作:
//this will set the height of myView to 0px.
$.with(myView).height(0);
//when you are ready to animate to height (in pixels):
$.with(myView).animate("{height:" + height + "}", new AnimationOptions());
如果您想了解更多,请查看文档,例如添加持续时间和事件回调。如果您仍然注意到非平滑动画,请考虑将应用程序属性添加到ANDROIDMASIL:
对此,您可能应该使用,而不是异步任务。异步任务是在后台不确定地运行的,这意味着它们将在运行时运行,但不能保证运行的速度
android:hardwareAccelerated="true"