繁体   English   中英

Android -> 如何动画到新的 position

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

这里是简单的 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" />

我想将动画 object 从屏幕中心移动到 0、0 位置。 我是怎么做的猫(它应该适用于所有屏幕分辨率)


我的答案:

谢谢你们的帮助。 但是我已经通过其他方式动态地解决了我的问题,没有 xml。 这是此方法的完整代码:

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

我的解决方案

谢谢你们的帮助。 但我已经通过其他方式动态修复了我的问题,没有xml。 这是这个方法的完整代码:

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

首先,在占据整个屏幕的容器中插入对象。 然后像这样修改动画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" />

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

%p后缀转换元素“相对于父级大小的百分比”。

如果您希望它在所有屏幕上工作,您将无法对x,y值进行硬编码。 动画允许您使用百分比。 这个动画会将你的视图从0%x和y移动(相对于它自己。换句话说,它在动画之前它将从那里开始)它将移动到0%px和y(这意味着0%相对于父母)这应该带你到左上角。 根据您想要的角落多远,您可以尝试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.

那没有用。 它总是从最初的 position 开始,然后扩展到新的 position。

我解决它的方法是将动画 View 包装在 RelativeLayout 中,然后移动带有 ImageView 的 RelativeLayout。

PivotX 和 PivotY 始终使用父视图的中心。

暂无
暂无

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

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