Android -> how to animate to the new position

两盒软妹~` 提交于 2019-12-03 07:14:11

问题


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

回答1:


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



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/7283477/android-how-to-animate-to-the-new-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!