Android->如何为新位置设置动画 [英] Android -> how to animate to the new position

查看:217
本文介绍了Android->如何为新位置设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是简单的xml android动画:

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

我想将动画对象从屏幕中心移动到0、0位置。我是怎么做到的(它应该在所有屏幕分辨率下都能正常工作)

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)

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

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


推荐答案

我的解决方案:

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

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

这篇关于Android->如何为新位置设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆