Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android -> how to animate to the new position

Here is simple xml android animation:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="-110"
    android:toYDelta="-110" android:duration="1000" android:fillAfter="true" />

I want to move animated object from the center of the screen to 0, 0 positions. How cat I make it (it should work at all screen resolutions)


My Answer:

Thank you guys for your help. But I have fix my problem by other way, dynamically, without xml. Here's full code of this method:

public void replace(int xTo, int yTo, float xScale, float yScale) {
        // create set of animations
        replaceAnimation = new AnimationSet(false);
        // animations should be applied on the finish line
        replaceAnimation.setFillAfter(true);

        // create scale animation
        ScaleAnimation scale = new ScaleAnimation(1.0f, xScale, 1.0f, yScale);
        scale.setDuration(1000);

        // create translation animation
        TranslateAnimation trans = new TranslateAnimation(0, 0,
                TranslateAnimation.ABSOLUTE, xTo - getLeft(), 0, 0,
                TranslateAnimation.ABSOLUTE, yTo - getTop());
        trans.setDuration(1000);

        // add new animations to the set
        replaceAnimation.addAnimation(scale);
        replaceAnimation.addAnimation(trans);

        // start our animation
        startAnimation(replaceAnimation);
    }
like image 239
pleerock Avatar asked Sep 02 '11 12:09

pleerock


1 Answers

My solution:

Thank you guys for your help. But I have fix my problem by other way, dynamically, without xml. Here's full code of this method:

public void replace(int xTo, int yTo, float xScale, float yScale) {
        // create set of animations
        replaceAnimation = new AnimationSet(false);
        // animations should be applied on the finish line
        replaceAnimation.setFillAfter(true);

        // create scale animation
        ScaleAnimation scale = new ScaleAnimation(1.0f, xScale, 1.0f, yScale);
        scale.setDuration(1000);

        // create translation animation
        TranslateAnimation trans = new TranslateAnimation(0, 0,
                TranslateAnimation.ABSOLUTE, xTo - getLeft(), 0, 0,
                TranslateAnimation.ABSOLUTE, yTo - getTop());
        trans.setDuration(1000);

        // add new animations to the set
        replaceAnimation.addAnimation(scale);
        replaceAnimation.addAnimation(trans);

        // start our animation
        startAnimation(replaceAnimation);
    }
like image 126
pleerock Avatar answered Sep 29 '22 14:09

pleerock