Android缩放动画制作:使ImageView运行

3

在我的应用程序中,我动态地创建了一个ImageView,并设置了一些固定的X、Y位置,然后对其进行缩放动画。但我不确定为什么它会有点运行(ImageView会改变其位置)。

    <?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="2.5"
        android:fromYScale="1.0"
        android:toYScale="2.5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="1000" />

    <alpha
        android:fromAlpha="1"
        android:toAlpha="0"
        android:duration="1000"
        />
</set>

但我希望它能够保持在同一位置,缩放需要从小到大逐渐进行。请帮助我,我不确定我的错误在哪里。

final ImageView rippleImageView = new ImageView(context);
            rippleImageView.setX(X);
            rippleImageView.setY(Y);
            rippleImageView.setImageBitmap(image);

            ((ViewGroup)(findViewById(R.id.rippleEffectView)).addView(rippleImageView, 0);

            Animation a = AnimationUtils.loadAnimation(context, R.anim.scale_up);
            a.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
                @Override
                public void onAnimationEnd(Animation animation) {
                    ((ViewGroup) rippleImageView.getParent()).removeView(rippleImageView);
                }
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
            rippleImageView.startAnimation(a);

布局文件非常简单,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true" />

    <RelativeLayout
        android:id="@+id/rippleEffectView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp">

        </RelativeLayout>



</RelativeLayout>

目前我有以下的样式:

enter image description here

我希望两个圆圈在同一位置。

2个回答

3

最终,我通过以下2个步骤成功了。

我将布局文件中的RelativeLayout更改为AbsoluteLayout。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true" />

    <AbsoluteLayout
        android:id="@+id/rippleEffectView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp">

        </AbsoluteLayout>

</RelativeLayout>

不确定为什么,setX()和setY()方法不起作用,但更改布局参数可以解决问题。

    final ImageView rippleImageView = new ImageView(context);
LayoutParams params = new AbsoluteLayout.LayoutParams(200, 200, X, Y);
rippleImageView.setParams(params);
                rippleImageView.setImageBitmap(image);

                ((ViewGroup)(findViewById(R.id.rippleEffectView)).addView(rippleImageView, 0);

                Animation a = AnimationUtils.loadAnimation(context, R.anim.scale_up);
                a.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        ((ViewGroup) rippleImageView.getParent()).removeView(rippleImageView);
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                rippleImageView.startAnimation(a);

现在我的输出是:

输入图像描述


0

如果我理解正确的话,您希望在动画运行后图像视图保持更新位置,对吗?如果是这样,您需要在设置中添加 android:fillAfter="true"。请参考文档这里

最终您的动画 XML 将会如下:

<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="2.5"
        android:fromYScale="1.0"
        android:toYScale="2.5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000" />

    <alpha
        android:fromAlpha="1"
        android:toAlpha="0"
        android:duration="1000"
        />
</set>

我在动态添加的视图上尝试了您的动画,效果如预期。您能分享一下如何将图像视图添加到父内容中吗? - Much Overflow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接