Android - >如何动画到新位置

ple*_*ock 15 animation android android-2.2-froyo

这是简单的xml android动画:

<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" />
Run Code Online (Sandbox Code Playgroud)

我想将动画对象从屏幕中心移动到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);
    }
Run Code Online (Sandbox Code Playgroud)

ple*_*ock 15

我的解决方案

谢谢你们的帮助.但我已经通过其他方式动态修复了我的问题,没有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);
    }
Run Code Online (Sandbox Code Playgroud)


Dmi*_*nko 6

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

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

%p后缀转换元素"相对于父级大小的百分比".