以编程方式更改布局高度,如何避免滞后?异步任务

发布于 2024-10-19 10:00:02 字数 1005 浏览 5 评论 0原文

我试图以一种看起来像是动画/扩展的方式以编程方式改变 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2024-10-26 10:00:02

进行布局非常昂贵,您将无法避免延迟。要制作这种动画,您应该为视图快照制作动画。

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.

一张白纸 2024-10-26 10:00:02

我找到了一种效果更好的替代方法。对于那些想要制作类似动画的人,请参阅我对此问题的回答: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文