在Android中以动画方式将ImageView移动到不同的位置 [英] Move an ImageView to different position in Animated way in Android

查看:41
本文介绍了在Android中以动画方式将ImageView移动到不同的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have several ImageViews in a RelativeLayout. now, when user taps any of the ImageView, I want it to be moved to a specified location with subtle animation.

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

Eg; I have initially set margins for LayoutParams associated with an ImageView as layoutparams1.setMargins(90,70,0,0); and then have it added to the layout.

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

and when imageview is tapped, I'd like its new location to be 200,200, with animation.

所以,有可能吗?如果是,那么如何?

So, is it possible? if yes, then how?

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

Note that I have both RelativeLayout and all of its child ImageViews created programmatically.

我是 android 开发的新手,所以期待详细的答案.

And I'm new to android development so an elaborative answer is expected.

推荐答案

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

imageView.startAnimation(animation);

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

UPDATE : The problem is that the View is actually still in it's old position. So we have to move it when the animation is finished. To detect when the animation is finished we have to create our own animationListener (inside our activity class):

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()代码>您将其移动到固定位置.

So the onClickEvent will get fired again at it's new place. The animation will now move it even more down, so you might want to save the x and y in a variable, so that in the onAnimationEnd() you move it not to a fix location.

这篇关于在Android中以动画方式将ImageView移动到不同的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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