15

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);
    }
kenju
  • 5,866
  • 1
  • 41
  • 41
pleerock
  • 18,322
  • 16
  • 103
  • 128
  • i think you left out the "android:" before fromYDelta – nurnachman Sep 02 '11 at 12:40
  • nurne, yeah, thx. but my problem is still actual – pleerock Sep 02 '11 at 12:49
  • ok i'm writing a test app to help you out, so let me get the problem better. you have a UI element in the center of the screen (its center is in the center, or its top-left corner is in the center?) and you want to use an animation to move it during 1 second to 0,0 (again-its center or its top-left to 0,0?) – nurnachman Sep 02 '11 at 13:07
  • I want to change x and y positions of the TextView. It's positions in the center - center. And I want to move to the left top side of the screen by using animation tools – pleerock Sep 02 '11 at 13:37

4 Answers4

15

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);
    }
pleerock
  • 18,322
  • 16
  • 103
  • 128
6

First, insert object in container that occupies entire screen. Then modify you animation xml like this

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

You can also check this android API reference http://developer.android.com/guide/topics/resources/animation-resource.html#translate-element

%p suffix translates element "in percentage relative to the parent size".

Dmitry Ryadnenko
  • 22,222
  • 4
  • 42
  • 56
0

If you want it to work on all screens you aren't going to be able to hardcode x,y values. Animations allow you to use percents. This animation would move your view from 0% x and y(relative to itself. In other words where ever it is before the animation it will start from there) It will move to 0%p x and y (which means 0% relative to the parent) This should take you to the top left. Depending on how far into the corner you want it you may try 5%p or 10%p to get some extra margin.

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="0%p"
    android:toYDelta="0%p" android:duration="1000" android:fillAfter="true" />
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
0

In my case I used an ImageView, randomly moved it using setY() and setX() and then animated it on the new position with a Scale animation in xml.

That didn't work. It always started from the initial position and scaled out to the new position.

The way I solved it was to wrap the animated View in a RelativeLayout and then move the RelativeLayout with the ImageView inside it.

PivotX and PivotY always uses the center of the parent view.

John T
  • 814
  • 10
  • 17