需要帮助在ObjectAnimator上制作DRY方法

时间:2014-06-10 01:17:51

标签: java android

有人可以建议一种有效的方法来遵循此代码的DRY原则吗?我用它太多了......我需要执行两种不同的方法吗?一个返回图像,然后返回一个textview?谢谢!

 mInspireMeImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (mInspireMeBOOL) {

                mInspireMeImgAnimation = ObjectAnimator.ofFloat(mInspireMeImg, "translationY", +300);

                mInspireMeTxtAnimation = ObjectAnimator.ofFloat(mInspireMeTxt, "translationY", +300);


                Log.w(tag, "DOWN Coordinates: " + mInspireMeImg.getY() + " Txt: " + mInspireMeTxt.getY());


                mInspireMeImgAnimation.setDuration(1000);
                mInspireMeTxtAnimation.setDuration(1000);

                mInspireMeImgAnimation.start();
                mInspireMeTxtAnimation.start();

                mInspireMeBOOL = false;
            } else {



                mInspireMeBOOL = true;
                mInspireMeImgAnimation = ObjectAnimator.ofFloat(mInspireMeImg, "translationY", -200);
                mInspireMeTxtAnimation = ObjectAnimator.ofFloat(mInspireMeTxt, "translationY", -200);

                Log.w(tag, "UP Coordinates: " + mInspireMeImg.getY() + "Txt: " + mInspireMeTxt.getY());

                mInspireMeImgAnimation.setDuration(1000);
                mInspireMeTxtAnimation.setDuration(1000);

                mInspireMeImgAnimation.start();

                mInspireMeTxtAnimation.start();

            }

        }
    });

2 个答案:

答案 0 :(得分:0)

怎么样:

private static void translateView(View view, int translationY)
{
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", translationY);
    animator.setDuration(1000);
    animator.start();
}

然后使用mInspireMeImgAnimation300和& c。

进行调用

答案 1 :(得分:0)

除了上面的答案,你可以移动这些行:

Log.w(tag, "DOWN Coordinates: " + mInspireMeImg.getY() + " Txt: " + mInspireMeTxt.getY());


mInspireMeImgAnimation.setDuration(1000);
mInspireMeTxtAnimation.setDuration(1000);

mInspireMeImgAnimation.start();
mInspireMeTxtAnimation.start();

在if语句之外,因为它们都是相同的。