以编程方式更改布局高度,如何避免滞后?异步任务
我试图以一种看起来像是动画/扩展的方式以编程方式改变 webview (用于显示 html)的高度。我的代码可以工作,但它很滞后。有时它运行平稳,有时运行缓慢,通常速度在整个运动过程中发生变化。我的代码如下。有没有人有什么好的方法来解决这个问题?
class ViewDropDownASynch extends AsyncTask<String,Void,String>{
WebView wv;
int height = 0;
LinearLayout.LayoutParams params;
int stop;
int step;
public ViewDropDownASynch(WebView v, int a, int o, int e){
wv = v;
params = new LinearLayout.LayoutParams(wv.getLayoutParams());
height = a;
stop = o;
step = e;
textAnimateExecute = true;
}
public String doInBackground(String... para){
for(int i = height; i != stop+step; i+=step){
params.height = i;
SystemClock.sleep(4);
this.publishProgress();
}
return "";
}
protected void onPostExecute(String result){
textAnimateExecute = false;
}
protected void onProgressUpdate(Void...values){
wv.setLayoutParams(params);
}
I am trying to programatically alter the height of a webview (used to display html) in a way that looks as though it is animating/expanding. My code works, but it is laggy. Sometimes it runs smoothly, other times it runs slowly, often the speed changes thruoghout the movement. My code is below. Does anyone have any good ways to fix this?
class ViewDropDownASynch extends AsyncTask<String,Void,String>{
WebView wv;
int height = 0;
LinearLayout.LayoutParams params;
int stop;
int step;
public ViewDropDownASynch(WebView v, int a, int o, int e){
wv = v;
params = new LinearLayout.LayoutParams(wv.getLayoutParams());
height = a;
stop = o;
step = e;
textAnimateExecute = true;
}
public String doInBackground(String... para){
for(int i = height; i != stop+step; i+=step){
params.height = i;
SystemClock.sleep(4);
this.publishProgress();
}
return "";
}
protected void onPostExecute(String result){
textAnimateExecute = false;
}
protected void onProgressUpdate(Void...values){
wv.setLayoutParams(params);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
进行布局非常昂贵,您将无法避免延迟。要制作这种动画,您应该为视图快照制作动画。
Doing layouts is very expensive, you won't be able to avoid lag. To do this kind of animation, you should animate a snapshot of the views.
我找到了一种效果更好的替代方法。对于那些想要制作类似动画的人,请参阅我对此问题的回答:Android:Expand/折叠动画
I've found an alternative way to do this that works much better. For those looking to do a similar animation, see my answer to this question: Android: Expand/collapse animation