7

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!

david.schreiber
  • 3,851
  • 2
  • 28
  • 46
  • replace expand(); by v.expand(); ? – Waza_Be Sep 29 '11 at 17:05
  • the method expand() is a public method of my custom View. Inside my onCreate() I call it via the object view.expand(). Inside my view itself I call it via this.expand(). I have tried it, adding this. to my function call, doesn't change anything. But thanks for the advice.. The constructor arguments (0,0,0,0) are intended, since I'm only interested in the interpolatedTime. Using a Timer and a TimerTask and calculate the interpolation with the Interpolator object itself, brings problems with updating my views out of the TimerTask thread. – david.schreiber Sep 29 '11 at 17:16
  • Also, Logcat shows "onTouch()" and "expand()" which means my touch callback enters the expand() method. But anyhow, the animation is not started. – david.schreiber Sep 29 '11 at 17:22
  • 1
    Have you found a solution to this? – NPike Dec 01 '11 at 00:33
  • the OnTouch is a Listener? if so, "this" means OnTouchListener, not the view. Try using ((customView)v).expand() – Sulfkain Oct 11 '13 at 13:30
  • @Sulfkain: In my example my view implemented the `OnTouchListener` interface, meaning `this` was the actual view with the `expand()` method. There was no compile time error (no error at all). The animation was simply not executed. – david.schreiber Nov 08 '13 at 10:39
  • As this is quite an old question, and I do not exactly remember the circumstances (too many apps and handlers since that :-)) I can only do some assumption using my current experience: I think the problem was with the view invalidation itself. As the animation is only started when the view's `onDraw()` method is called by the **view's parent** not by the view itself! (you have to look into the `View` rendering and animation code). Therefore, probably a `((ViewGroup) view.getParent()).invalidate()` would have solved the problem back then. – david.schreiber Nov 08 '13 at 10:42

1 Answers1

1

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); 
            }
        });
    }
f.old
  • 343
  • 3
  • 15
  • still not working with floatingactionbutton. It seem some other animation with higher priority run and it override my animation. – kemdo Feb 23 '17 at 14:43