以编程方式改变布局高度,如何避免滞后?的AsyncTask

时间:2011-02-25 02:23:28

标签: android animation layout android-asynctask lag

我试图以一种看起来好像是动画/扩展的方式以编程方式改变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);
    }

2 个答案:

答案 0 :(得分:2)

我找到了另一种方法来做得更好。对于那些想要制作类似动画的人,请参阅我对这个问题的回答:Android: Expand/collapse animation

答案 1 :(得分:1)

做布局非常昂贵,你将无法避免滞后。要执行此类动画,您应该为视图的快照设置动画。