通过翻译动画Android移动视图

时间:2015-02-18 10:22:03

标签: android view android-animation translate-animation

我试图通过单击将屏幕上的视图移动到随机点,然后让它保持在那里并通过单击再次移动,但是,视图仅在0,0坐标开始时执行正确的移动,一次改变了,它是从随机位置到随机位置的动画,并在第三个随机位置结束。

这是我的代码段,有人可以运行这个,看看它是否正常工作?如果没有,我错过了什么?

public class MainActivity extends ActionBarActivity {

ImageView image;
Rect rect;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    final int height = displaymetrics.heightPixels;
    final int width = displaymetrics.widthPixels;

    image = (ImageView) findViewById(R.id.image);

    findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int newLeft = ((int)Math.round(Math.random()*width))/2;
            final int newTop = ((int)Math.round(Math.random()*height))/2;;
            final int newRight = ((int)Math.round(Math.random()*width))/2;;
            final int newBottom = ((int)Math.round(Math.random()*height))/2;;
            rect = new Rect(newLeft, newTop, newRight, newBottom);

           doSomeAnimation(rect);

        }
    });
}

 public void doSomeAnimation(final Rect rect) {
    final FrameLayout.LayoutParams imageParasms = (FrameLayout.LayoutParams) image.getLayoutParams();


    TranslateAnimation animation = new TranslateAnimation(
            imageParasms.leftMargin, rect.left, imageParasms.topMargin, rect.top);



    animation.setDuration(1000);

    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

       }

        @Override
        public void onAnimationEnd(Animation animation) {


            FrameLayout.LayoutParams imageParasms = (FrameLayout.LayoutParams) image.getLayoutParams();
            imageParasms.width = rect.width();
            imageParasms.height = rect.height();

            imageParasms.leftMargin = rect.left;
            imageParasms.topMargin = rect.top;

            image.setLayoutParams(imageParasms);

            image.requestLayout();

             }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    image.startAnimation(animation);
}

1 个答案:

答案 0 :(得分:1)

最好使用ValueAnimator,因为TranslateAnimation使用矩阵,并且它非常难以处理以前的矩阵值,我查看源代码并了解使用ValueAnimator更好,因为我们可以更好地控制值的变化,我实现了以不同的方式,检查这个

public class MainActivity extends FragmentActivity {
ImageView image;
Random rnd = new Random();
int leftMargin;
int topMargin;
boolean xRunning;
boolean yRunning;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    final int height = displaymetrics.heightPixels;
    final int width = displaymetrics.widthPixels;

    image = (ImageView) findViewById(R.id.image);

    findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int newLeft = rnd.nextInt(height);
            int newTop = rnd.nextInt(width);
            Point pt = new Point(newLeft, newTop);
            doSomeAnimation(pt);
        }
    });

    //This Handler is used for optimization, for setting a layout in one place, instead of calling
    //it in every onAnimationUpdate method
    image.post(new Runnable() {
        @Override
        public void run() {
            if(xRunning && yRunning) {
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) image.getLayoutParams();
                params.leftMargin = leftMargin;
                params.topMargin = topMargin;
                image.setLayoutParams(params);
            }
            image.postDelayed(this, 30);
        }
    });
}

public void doSomeAnimation(final Point pt) {
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) image.getLayoutParams();

    ValueAnimator xAnim = ValueAnimator.ofFloat(pxFromDp(this, params.leftMargin), pt.x);
    xAnim.setDuration(1000);

    xAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            leftMargin = (int) dpFromPx(MainActivity.this, (float) animation.getAnimatedValue());
        }
    });

    xAnim.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            xRunning = false;
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });

    xAnim.start();
    xRunning = true;

    ValueAnimator yAnim = ValueAnimator.ofFloat(pxFromDp(this, params.topMargin), pt.y);
    yAnim.setDuration(1000);

    yAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            topMargin = (int) dpFromPx(MainActivity.this, (float) animation.getAnimatedValue());
        }
    });

    yAnim.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            yRunning = false;
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });

    yAnim.start();
    yRunning = true;
}

public static float dpFromPx(final Context context, final float px) {
    return px / context.getResources().getDisplayMetrics().density;
}

public static float pxFromDp(final Context context, final float dp) {
    return dp * context.getResources().getDisplayMetrics().density;
}

}