【问题标题】:Imageview attributes lost after setting animation设置动画后Imageview属性丢失
【发布时间】:2023-03-13 07:35:01
【问题描述】:

我正在尝试将帧动画设置为 Imageview。

imageAnim =  (ImageView) rootView.findViewById(R.id.orderCards);
imageAnim.setBackgroundResource(R.drawable.image_switcher_anim);
imageAnim.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
anim = (AnimationDrawable) imageAnim.getBackground();
anim.start();

所有的图像视图属性,如高度、宽度、比例类型都丢失了。 我理解这是因为将动画设置为背景资源,并且图像将在没有 scaletype 的情况下显示。

我看到了两种可能性 有没有办法设置动画而不将其设置为背景以避免这个问题? 或者 图片背景可以设置高宽等属性吗?

【问题讨论】:

    标签: android animation background-image


    【解决方案1】:

    试试这个:

    imageAnim.invalidateDrawable(anim);
    

    这将重绘图像视图。

    【讨论】:

    • NO ...它没有帮助..它实际上删除了动画并且帧不会改变。
    【解决方案2】:

    我在另一个链接上找到了答案 Android animation with multiple jpeg files?

    我们需要在anim文件夹中设置动画文件,将其设置为imageview的源,getdrawable而不是getResourceBackground,并将其分配给animationDrawable并启动动画。这样,imageview 的所有布局属性都将被维护。

    xml代码

    <ImageView android:id="@+id/orderCards
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:src="@anim/image_switcher_anim"/>
    

    Java 代码

    imageAnim = (ImageView) rootView.findViewById(R.id.orderCards);
                    anim = (AnimationDrawable) imageAnim.getDrawable();
                    imageAnim.post(run);
    
    Runnable run = new Runnable() {
            @Override
            public void run() {
                anim.start();
            }
        };
    

    【讨论】:

      猜你喜欢
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      相关资源
      最近更新 更多