将一个ImageView的不同位置的动画方式的Andr​​oid不同、位置、方式、动画

2023-09-13 23:41:13 作者:你满身是刺我也会紧紧抱住

我有几个的ImageView S在一个 RelativeLayout的。现在,当用户点击任何一个ImageView的,我希望它被转移到与微妙的动画指定的位置。

例如,我已初步建立与的ImageView layoutparams1.setMargins(90相关的利润率的LayoutParams , 70,0,0); ,然后把它添加到布局

和当ImageView的被窃听,我想它的新位置是 200,200 ,与动画。

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

请注意,我有两个 RelativeLayout的及其所有子的ImageView 取值程序创建的。

和我新的Andr​​oid开发这样一个煞费苦心的答案的预期。

解决方案

  TranslateAnimation动画=新TranslateAnimation(0,50,0,100);
animation.setDuration(1000);
animation.setFillAfter(假);
animation.setAnimationListener(新MyAnimationListener());

imageView.startAnimation(动画);
 

的问题是,查看是居然还在它的老位置。因此,我们必须在动画完成后移动它。为了检测当动画完成后,我们必须创建自己的 animationListener (我们活动里面类):

 私有类MyAnimationListener实现AnimationListener {

    @覆盖
    公共无效onAnimationEnd(动画动画){
        imageView.clearAnimation();
        的LayoutParams LP =新的LayoutParams(imageView.getWidth(),imageView.getHeight());
        lp.setMargins(50,100,0,0);
        imageView.setLayoutParams(LP);
    }

    @覆盖
    公共无效onAnimationRepeat(动画动画){
    }

    @覆盖
    公共无效onAnimationStart(动画动画){
    }

}
 

因此​​, onClickEvent 将得到再次发射它的新的地方。 动画现在将移动更加下来,所以你可能要保存 X 在一个变量,因此,在 onAnimationEnd()移动它不是一个固定的位置。

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.

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.

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?

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

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);

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) {
    }

}

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.