使 ImageView 运行的 Android 缩放动画

标签 android animation imageview android-animation

在我的应用程序中,我正在动态创建一个 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 个步骤获得了成功。

我已经在布局文件中将 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);

现在我的输出是,

enter image description here

关于使 ImageView 运行的 Android 缩放动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34945649/

相关文章:

android - 通过服务和 Activity 共享单例java类

android - 元素从右到左而不是从左到右对齐

android - android中透明imageView的边框

multithreading - 在后台和线程中加载 JavaFX 图像

android - 调用 Intent 过滤器

iphone - iOS 同步动画

javascript - 变换矩阵线性组合的旋转动画导致放大缩小

CSS 动画 - 如何使用一个关键帧声明切换方向

android - ImageView 太小

android - Eclipse 问题 - 启动错误 : Failed to connect to remote VM. 连接超时