سؤال

I have touch event where I change View position (picture drag n drop)

case MotionEvent.ACTION_MOVE:
         view.setTranslationX(X - xDelta);

On release event I animate View back to original position (0)

slideBack = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,view.getTranslationX()*-1,Animation.ABSOLUTE,0,Animation.ABSOLUTE,0);
slideBack.setFillAfter(true);
slideBack.setDuration(1000);

After animation is done my View will be in default position, but it's getTranslationX() will stay the same. When I start animation it seems to set it's starting position as the default value (0) and then tweens back to getTranslationX().

How do I set that current value should be reset and used as new (0) point? Or how do I make Animation that wont mess with translation values?

Thank you for your time.

هل كانت مفيدة؟

المحلول

TranslateAnimation is a view animation, as opposed to a property animation. The distinction is defined in these docs, and the included snippet:

http://developer.android.com/guide/topics/graphics/overview.html http://developer.android.com/guide/topics/graphics/prop-animation.html

Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.

Basically, view animation works by applying a transformation matrix to the DisplayList in draw(). This effects how the View is drawn, but is entirely distinct from any of the View's properties.

Property animation, on the other hand, does effect your View's properties, and is probably what you'll want to use. Try replacing your TranslateAnimation with the following snippet:

ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationX", view.getTranslationX(), 0f);
anim.setDuration(1000);
anim.start();

As this animator runs it will actually change the value returned by getTranslationX(), so your touch handling code will be able to correctly determine where the View is being displayed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top