我怎样才能让这个code的动画更有效? [英] How can I make this code for Animations more effective?

查看:157
本文介绍了我怎样才能让这个code的动画更有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code通过 ObjectAnimator y位置 C>和 AnimationSet 秒。虽然翻译动画的两种观点的字母值将再次降低和增长。这仅仅是玩弄有点设置。阿尔法动画以XML定义,转换动画与code完成的。

 <?XML版本=1.0编码=UTF-8&GT?;
<设置的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:订货=顺序>
    < objectAnimator
        机器人:propertyName的=阿尔法
        机器人:时间=500
        机器人:valueTo =0.5
        机器人:VALUETYPE =floatType/>
    < objectAnimator
        机器人:propertyName的=阿尔法
        机器人:时间=500
        机器人:valueTo =1.0
        机器人:VALUETYPE =floatType/>
< /集>

  @覆盖
公共无效的onClick(视图v){
    如果(mView1 = NULL&放大器;!&安培;!mView2 = NULL){        INT顶= mView2.getTop();        ObjectAnimator translateView1 = NULL;
        ObjectAnimator translateView2 = NULL;        如果(mView1.getTranslationY()== 0){                translateView1 = ObjectAnimator.ofFloat(mView1,translationY,顶部);
            translateView2 = ObjectAnimator.ofFloat(mView2translationY,顶*( - 1));        }其他{                translateView1 = ObjectAnimator.ofFloat(mView2translationY,0);
            translateView2 = ObjectAnimator.ofFloat(mView1translationY,0);        }        translateView1.setDuration(1000L);
        translateView2.setDuration(1000L);        AnimatorSet ALPHA1 =(AnimatorSet)AnimatorInflater.loadAnimator(mCtx,R.anim.switcher);
        alpha1.setTarget(mView1);        AnimatorSetα2 =(AnimatorSet)AnimatorInflater.loadAnimator(mCtx,R.anim.switcher);
        alpha2.setTarget(mView2);        AnimatorSet套=新AnimatorSet();
        set.playTogether(translateView1,translateView2,α1,α2);
        set.start();    }
}

现在,因为这是工作正常,我想知道我怎么能缩短code?


  • 是否真正有必要的一个单独的实例 AnimatorSet 查看,它使用从XML动画?我可以吹的XML一次,并把它用在不同的AnimatorSets / ObjectAnimators?因为我没有找到一个setter这需要一个动画作为参数。<​​/ p>


  • 我可以定义多个目标的一个AnimationSet / ObjectAnimator?或者是定义不同ObjectAnimators的意见,并在AnimationSet使用它们的唯一途径?



解决方案

您想要把动画code,它的动画片里面的类...是这样的:

 公共类FadingView扩展视图{    私人ObjectAnimator褪色,MOVEX,moveY;
    私人AnimatorSet moveSet;    公共FadingView(上下文的背景下){
        超级(上下文);
    }    私人无效淡入淡出(INT持续时间,浮...值){        如果(淡出== NULL){
            淡出= ObjectAnimator.ofFloat(这一点,阿尔法,值);
        }其他{
            fade.setFloatValues​​(值);
        }
        fade.setDuration(持续时间);
        fade.start();    }    私人无效移动(INT持续时间,诠释的x,int y)对{        如果(MOVEX == NULL){
            的Movex = ObjectAnimator.ofFloat(这一点,translation_x,X);
        }其他{
            moveX.setFloatValues​​(X);
        }
        如果(moveY == NULL){
            moveY = ObjectAnimator.ofFloat(这一点,translation_y,Y);
        }其他{
            moveY.setFloatValues​​(Y);
        }
        如果(moveSet == NULL){
            moveSet =新AnimatorSet();
            moveSet.playTogether(MOVEX,moveY);
        }        moveSet.setDuration(持续时间);
        moveSet.start();    }    公共无效moveToUpperLeft(INT持续时间){
        招(持续时间,0,0);
    }    公共无效显示(INT持续时间){
        淡入淡出(持续时间,1);
    }    公共无效隐藏(INT持续时间){
        褪色(持续时间,0);
    }}

只是一个基本的例子,但现在你可以拨打:

  FadingView视图=新FadingView(背景);
view.hide(500);
view.moveToUpperLeft(500);

当然,你可以自定义和概括这甚至更多...

This code switches the y-position of two Views (mView1 and mView2) on Button click via ObjectAnimator and AnimationSets. While the translate animation the alpha value of both views will be reduced and growth again. This is just a setup to play around a bit. The alpha animation is defined in XML and the translate animation is done with code.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially">
    <objectAnimator
        android:propertyName="alpha"
        android:duration="500"
        android:valueTo="0.5"
        android:valueType="floatType"/>
    <objectAnimator
        android:propertyName="alpha"
        android:duration="500"
        android:valueTo="1.0"
        android:valueType="floatType"/>
</set>

@Override
public void onClick(View v) {
    if (mView1 != null && mView2 != null) {

        int top = mView2.getTop();

        ObjectAnimator translateView1 = null;
        ObjectAnimator translateView2 = null;

        if (mView1.getTranslationY() == 0) {

                translateView1 = ObjectAnimator.ofFloat(mView1, "translationY", top);
            translateView2 = ObjectAnimator.ofFloat(mView2, "translationY", top*(-1));

        } else {

                translateView1 = ObjectAnimator.ofFloat(mView2, "translationY", 0);
            translateView2 = ObjectAnimator.ofFloat(mView1, "translationY", 0);

        }

        translateView1.setDuration(1000L);
        translateView2.setDuration(1000L);

        AnimatorSet alpha1 = (AnimatorSet)AnimatorInflater.loadAnimator(mCtx, R.anim.switcher);
        alpha1.setTarget(mView1);

        AnimatorSet alpha2 = (AnimatorSet)AnimatorInflater.loadAnimator(mCtx, R.anim.switcher);
        alpha2.setTarget(mView2);

        AnimatorSet set = new AnimatorSet();
        set.playTogether(translateView1, translateView2, alpha1, alpha2);
        set.start();

    }
}

Now, because this is working as expected, I wanna know how I can shorten the code?

  • Is it really necessary to have a seperate instance of an AnimatorSet for every View, which uses the animation from XML? Can I inflate the xml once and use it on different AnimatorSets / ObjectAnimators? Because I didn't found a setter which takes an Animator as an Argument.

  • Can I define multiple targets for one AnimationSet/ObjectAnimator? Or is the only way to define different ObjectAnimators for the Views and use them in a AnimationSet?

解决方案

You will want to put the animation code inside the class that's animated ... like this :

public class FadingView extends View {

    private ObjectAnimator fade, moveX, moveY;
    private AnimatorSet moveSet;

    public FadingView(Context context) {
        super(context);
    }

    private void fade(int duration, float...values) {

        if(fade==null) {
            fade = ObjectAnimator.ofFloat(this, "alpha", values);
        } else {
            fade.setFloatValues(values);
        }
        fade.setDuration(duration);
        fade.start();

    }

    private void move(int duration, int x, int y) {

        if(moveX==null) {
            moveX = ObjectAnimator.ofFloat(this, "translation_x", x);
        } else {
            moveX.setFloatValues(x);
        }
        if(moveY==null) {
            moveY = ObjectAnimator.ofFloat(this, "translation_y", y);
        } else {
            moveY.setFloatValues(y);
        }
        if(moveSet == null) {
            moveSet = new AnimatorSet();
            moveSet.playTogether(moveX, moveY);
        }

        moveSet.setDuration(duration);
        moveSet.start();

    }

    public void moveToUpperLeft(int duration) {
        move(duration, 0,0);
    }

    public void show(int duration) {
        fade(duration,1);
    }

    public void hide(int duration) {
        fade(duration,0);
    }

}

Just a basic example, but now you can call :

FadingView view = new FadingView(context);
view.hide(500);
view.moveToUpperLeft(500);

Of course you can customize and generalize this even more ...

这篇关于我怎样才能让这个code的动画更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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