2

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);
    }
WendiKidd
  • 4,333
  • 4
  • 33
  • 50
Seth Nelson
  • 2,598
  • 4
  • 22
  • 29

2 Answers2

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.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • My problem isn't the inherent lag of animating (in fact, the lag isn't enough, that's why i have the SystemClock.sleep()), it's that it is inconsistent. Sometimes it runs fast, sometimes it run slow, the speed changes dynamically during the resize and leaves it looking very unprofessional. – Seth Nelson Feb 25 '11 at 20:03
2

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

Community
  • 1
  • 1
Seth Nelson
  • 2,598
  • 4
  • 22
  • 29