Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does view.startAnimation(animation) not work when called from an event?

I have created a custom view which uses an dummy TranslateAnimation to setup some layout properties. I use the Interpolator to calculate height, and apply it to a view inside the applyTransformation() method of the TranslateAnimation.

This is working quite well, if i trigger the animation from my Activity.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Log.i("test", "onCreate()");
    view.expand(); // This method starts the animation
}

When I try to do the same using a touch event, nothing happens.

@Override
// This method is touch handler of the View itself
public boolean onTouch(View v, MotionEvent event) {
    Log.i("test", "onTouch()");
    this.expand(); // onTouch is part of the view itself and calls expand() directly
    return true;
}

My expand method looks like this:

public void expand() {
    Log.i("test", "Expand!");

    TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0) {

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            Log.i("test", "applyTransformation()");

            super.applyTransformation(interpolatedTime, t);

            // do something
        }

    };
    anim.setDuration(500);
    anim.setInterpolator(new AccelerateDecelerateInterpolator());
    this.someInternalView.startAnimation(anim);
}

Once my activity is created Logcat shows "onCreate()" Inside my touch event Logcat shows "onTouch()" Inside the expand() method Logcat shows "Expand!" - either called from the activity or from an event.

Inside the method applyTransformation() Logcat shows "applyTransformation()" - BUT! only if expand() is called from the onCreate(). Any attempt in trying to start the animation from an event failed.

This looks to me like some sort of threading problem. Could this be? Is there anything I am missing? As far as I see from other posts, starting animations from events should work without any problems...

Thanks in advance!

like image 666
david.schreiber Avatar asked Sep 29 '11 17:09

david.schreiber


1 Answers

try this:

public void expand() {
    Log.i("test", "Expand!");
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0) {

                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    Log.i("test", "applyTransformation()");

                    super.applyTransformation(interpolatedTime, t);

                        // do something
                    }

                };
                anim.setDuration(500);
                anim.setInterpolator(new AccelerateDecelerateInterpolator());
                this.someInternalView.startAnimation(anim); 
            }
        });
    }
like image 51
f.old Avatar answered Nov 13 '22 06:11

f.old