简体   繁体   English

Android -> 如何动画到新的 position

[英]Android -> how to animate to the new position

Here is simple xml android animation:这里是简单的 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.我想将动画 object 从屏幕中心移动到 0、0 位置。 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.但是我已经通过其他方式动态地解决了我的问题,没有 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. 但我已经通过其他方式动态修复了我的问题,没有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 然后像这样修改动画xml

<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 您还可以查看此Android API参考http://developer.android.com/guide/topics/resources/animation-resource.html#translate-element

%p suffix translates element "in percentage relative to the parent size". %p后缀转换元素“相对于父级大小的百分比”。

If you want it to work on all screens you aren't going to be able to hardcode x,y values. 如果您希望它在所有屏幕上工作,您将无法对x,y值进行硬编码。 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%px and y (which means 0% relative to the parent) This should take you to the top left. 这个动画会将你的视图从0%x和y移动(相对于它自己。换句话说,它在动画之前它将从那里开始)它将移动到0%px和y(这意味着0%相对于父母)这应该带你到左上角。 Depending on how far into the corner you want it you may try 5%p or 10%p to get some extra margin. 根据您想要的角落多远,您可以尝试5%p或10%p来获得额外的余量。

<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. 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.它总是从最初的 position 开始,然后扩展到新的 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.我解决它的方法是将动画 View 包装在 RelativeLayout 中,然后移动带有 ImageView 的 RelativeLayout。

PivotX and PivotY always uses the center of the parent view. PivotX 和 PivotY 始终使用父视图的中心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM