开发者

Android -> how to animate to the new position

Here is simple xml android ani开发者_StackOverflowmation:

<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);
    }


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);
    }


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".


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" />


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜