2013-04-25 53 views
3

我有我已經設置爲400dp(寬度)和300dp(高度)的圖像圖。此圖像視圖用於我從url加載圖像的列表視圖。期間的圖像的加載,我示出陸侃酒吧等動畫被設置是這樣的:的Android - imageview.setBackgroundResource發出

imageView.setBackgroundResource(R.drawable.progress); 
      final AnimationDrawable startAnimation = (AnimationDrawable) imageView 
        .getBackground(); 

      startAnimation.start(); 

一旦圖象被加載完成,我會刪除該動畫,並設置加載的圖像,如下所示:

imageView.setBackgroundResource(0); 
      imageView.setImageBitmap(bitmap); 

但是問題是,動畫也是採取指定的寬度和高度,即分別爲400dp和300dp,我該如何縮小動畫的大小可以單獨繪製?

我的動畫文件:progress.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/anim_black" 
    android:oneshot="false" > 

    <item 
     android:drawable="@drawable/frame_1" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_2" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_3" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_4" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_5" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_6" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_7" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_8" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_9" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_10" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_11" 
     android:duration="100"/> 
    <item 
     android:drawable="@drawable/frame_12" 
     android:duration="100"/> 

</animation-list> 

幀1,幀2 ....框架12是PNG文件其中我被分割GIF圖像。

+1

你是否嘗試設置動畫繪製爲setImageResource(而不是setBackgroundResource) – MatrixDev 2013-04-25 12:47:30

回答

2

你應該在你的情況下使用setImageResource動畫。背景圖像總是縮放以適合整個圖像。在開始動畫之前以及加載圖像之後,您還需要修改ScaleType

imageView.setImageResource(R.drawable.progress); 
imageView.setScaleType(ScaleType.CENTER); 

final AnimationDrawable startAnimation = (AnimationDrawable) imageView.getDrawable(); 
startAnimation.start(); 

// and once image is loaded 
imageView.setImageBitmap(bitmap); 
imageView.setScaleType(ScaleType.CENTER_INSIDE); 
+0

謝謝。工作完美。但動畫以這種方式顯示。垂直 - 中心和水平 - 極端的左邊。如何使它在水平和垂直方向上居中? – 2013-04-25 13:06:44

+0

你能告訴我如何在佈局中聲明ImageView嗎? – 2013-04-25 13:09:52

+0

2013-04-25 13:10:59

1

來吧。你正在做更長的路。它更好地使用自定義Progressbar爲您的目的。你需要做的是使用相同尺寸的Imageview的框架佈局,並將你的圖像視圖的進度條放到你的圖像視圖上方的相同視圖中。加載圖像後,將進度條的可見性更改爲View.GONE或View.INVISIBLE。它可以在列表視圖中使用,而且不會有任何內存問題。爲了實現一個自定義的進度條使用這個。

 <ProgressBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:indeterminateDrawable="@anim/progress_bar_anim" > 
     </ProgressBar> 

凡YOUT抽拉progress_bar_anim.xml是

<?xml version="1.0" encoding="utf-8"?> 
    <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/pro_indi" 
    android:pivotX="50%" 
    android:pivotY="50%" /> 

pro_indi要被旋轉任何圓形圖像。

<FrameLayout> 
    <ImageView> 
    <Progressbar> 
</FrameLayout>