在Android中以动画方式将ImageView移动到不同位置

时间:2012-06-21 18:24:14

标签: android animation android-animation

我在ImageView中有几个RelativeLayout个。现在,当用户点击任何ImageView时,我希望将它移动到具有微妙动画的指定位置。

EG;我最初将与LayoutParams关联的ImageView的边距设置为layoutparams1.setMargins(90,70,0,0);,然后将其添加到布局中。

当点击了imageview时,我希望它的新位置为200,200,并带有动画。

那么,有可能吗?如果是,那怎么样?

请注意,我以编程方式创建了RelativeLayout及其所有子ImageView

我是Android开发的新手,因此需要一个精心的答案。

4 个答案:

答案 0 :(得分:54)

TranslateAnimation animation = new TranslateAnimation(0, 50, 0, 100);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());

imageView.startAnimation(animation);

更新: 问题是View实际上仍处于旧位置。所以我们必须在动画结束时移动它。要检测动画何时结束,我们必须创建自己的animationListener(在我们的activity类中):

private class MyAnimationListener implements AnimationListener{

    @Override
    public void onAnimationEnd(Animation animation) {
        imageView.clearAnimation();
        LayoutParams lp = new LayoutParams(imageView.getWidth(), imageView.getHeight());
        lp.setMargins(50, 100, 0, 0);
        imageView.setLayoutParams(lp);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationStart(Animation animation) {
    }

}

所以onClickEvent会在它的新地方再次被解雇。 动画现在会将其向下移动得更远,因此您可能希望将xy保存在变量中,以便在onAnimationEnd()中将其移动到修复位置。

答案 1 :(得分:8)

使用ObjectAnimator也更好。这一观点处于新的位置。 例如:

ImageView splash ;
@Override
public boolean onTouchEvent(MotionEvent event) {
    float tx = event.getX();
    float ty = event.getY();

    int action = event.getAction();
    switch(action) {
        case MotionEvent.ACTION_DOWN:
            tx = event.getX();
            ty = event.getY();

            //       findViewById(R.id.character).setX(tx-45);
            //      findViewById(R.id.character).setY(ty-134);

            ObjectAnimator animX = ObjectAnimator.ofFloat(splash, "x", tx-45);
            ObjectAnimator animY = ObjectAnimator.ofFloat(splash, "y", ty-134);
            AnimatorSet animSetXY = new AnimatorSet();
            animSetXY.playTogether(animX, animY);
            animSetXY.start();

            break;
        default:
    }
    return true;
}

答案 2 :(得分:2)

在下面的代码中,我动态地在帧布局的中心添加一个图像视图。添加后我增加缩放比例并设置alpha以获得缩放效果,完成动画后我只是将我的图像视图转换为一个位置到另一个位置。

在framelayout上添加图片视图

    imgHeart = new ImageView(getBaseContext());
    imgHeart.setId(R.id.heartImage);
    imgHeart.setImageResource(R.drawable.material_heart_fill_icon);
    imgHeart.setLayoutParams(new FrameLayout.LayoutParams(50, 50, Gravity.CENTER));
    mainFrameLaout.addView(imgHeart);

在图像视图中添加动画

       imgHeart.animate()
            .scaleXBy(6)
            .scaleYBy(6)
            .setDuration(700)
            .alpha(2)
            .setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    imgHeart.animate()
                            .scaleXBy(-6f).scaleYBy(-6f)
                            .alpha(.1f)
                            .translationX((heigthAndWidth[0] / 2) - minusWidth)
                            .translationY(-((heigthAndWidth[1] / 2) - minusHeight))
                            .setDuration(1000)
                            .setListener(new Animator.AnimatorListener() {
                                @Override
                                public void onAnimationStart(Animator animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animator animation) {
                                // remove image view from framlayout
                                }
                                @Override
                                public void onAnimationCancel(Animator animation) {
                                }

                                @Override
                                public void onAnimationRepeat(Animator animation) {
                                }
                            }).start();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            }).start();

答案 3 :(得分:1)

您可以使用此代码

imageView.animate().x(80).y(212).setDuration(300);

对于软动画,您可以使用此库

https://github.com/wirecube/android_additive_animations