2016-04-11 89 views
1

我试图将帧动画设置为Imageview。设置动画后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(); 

所有的图像视图属性,如高度宽度,缩放类型都会丢失。 我明白这是因为设置动画为背景资源,图像将显示无缩放类型。

我看到两种可能性 有没有设置动画而不将其设置为背景以避免此问题的方法? 或 像高度宽度等属性可以设置图像背景。

回答

0

试试这个:

imageAnim.invalidateDrawable(anim); 

这将重绘图像视图。

+0

NO ......它不帮助..它实际上消除了动画和框架并没有改变。 – Bags

0

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

我们需要设置动画文件夹中的动画文件,将其设置为源到ImageView的,getdrawable而不是getResourceBackground并将其分配给animationDrawable并启动动画。有了这个所有的图像视图的布局属性将保持。

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