2014-01-29 21 views
0

這個問題已經被問到,但我沒有找到任何合適的答案,所以我發佈的問題。 我有一個ProgressBar動畫,它具有多個圖像並像加載欄一樣加載這些圖像。我能夠在4.0及更高版本中成功運行它,但它在2.3中不起作用。如何使它在較低版本中工作?動畫Drawable not in GingerBread

我的動畫繪製對象XML文件

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false" 
    android:pivotX="50%" 
    android:pivotY="50%" > 
    <item android:drawable="@drawable/p1_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p2_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p3_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p4_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p5_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p6_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p7_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p8_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p9_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p10_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p11_wheel" android:duration="200"/> 
    <item android:drawable="@drawable/p12_wheel" android:duration="200"/> 
</animation-list> 

我的XML文件

<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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_centerInParent="true"/> 


</RelativeLayout> 

我的Java文件

package com.example.progressbarsample; 

import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.DrawableContainer; 
import android.view.Menu; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 

public class MainActivity extends Activity { 
    ImageView progressbar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     progressbar = (ImageView) findViewById(R.id.imageView1); 
     progressbar.setBackgroundResource(R.drawable.progressbar); 
     final AnimationDrawable frame = (AnimationDrawable) progressbar.getBackground(); 
     progressbar.post(new Runnable() { 

      @Override 
      public void run() { 
       frame.start(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 
+0

什麼是您的最低SDK版本? – Piyush

+0

@PiyushGupta Min SDK是10 – Saraschandraa

+0

您是否遇到錯誤或者只有它不工作? – Mert

回答

1

最後我找到了一個解決的辦法。視圖後

progressbar = (ImageView) findViewById(R.id.imageView1); 
progressbar.setBackgroundResource(R.drawable.progressbar); 
final AnimationDrawable frame = (AnimationDrawable)getResources.getDrawable(R.drawable.progressbar); \\progressbar is the anim.xml file 
progressbar.setBackgroundDrawable(frame); 
progressbar.post(new Runnable() { 

      @Override 
      public void run() { 
       frame.start(); 
      } 
     }); 
    } 
0

我與可繪製動畫的工作和我的作品在這裏我的代碼:

mImageView.setBackgroundResource(R.drawable.anim); 
AnimationDrawable explosionAnimation = (AnimationDrawable) mImageView.getBackground(); 
explosionAnimation.start(); 

但我認爲它的帖子becausse,嘗試代碼,而無需使用progressbar.post

+0

感謝您的回覆,它在4.0和更高版本的設備中運行良好,但在2.3中只顯示第一個drawable動畫不起作用。 – Saraschandraa

+0

好,然後看看這個[鏈接](http://stackoverflow.com/questions/11385081/animationdrawable-dont-work-in-android-2-2),可以幫助你,我希望 – Mert

+0

我已經嘗試過,但ID不起作用。 – Saraschandraa

1

嘗試動畫是這個樣子可見:

@Override 
public void onWindowFocusChanged (bool hasFocus) 
{ 
    super.onWindowFocusChanged (hasFocus); 
    //get the animation from your view 
    AnimationDrawable explosionAnimation = (AnimationDrawable) mImageView.getBackground(); 
    //set it visible and start animation 
    explosionAnimation.setVisible (true, true); 
    explosionAnimation.start(); 
} 

的方法onWindowFocusChanged從活動視圖後,被稱爲已經是可見的,它獲得焦點。所以,你將能夠爲你的圖像製作動畫。 我試過沒有setVisible但只顯示一個靜態圖像。當我從onResume,onStart等開始動畫時會發生同樣的情況。 上面的代碼適用於我,包括Android 2.3。我目前正在與單聲道(與Xamarin)合作,但我希望它能在Java中爲你工作。