15

ビューなどにアタッチされていないカスタムクラスの BitmapStorage がいくつかあります。これはユーティリティです。そして、アニメーション フレームを含む <animation-list> を含む Born_animation.xml ファイルがあります。

<animation-list oneshot="true" >
    <item drawable="@drawable/frame01" />
    <item drawable="@drawable/frame02" />
</animation-list>

Resources クラスを使用して xml ファイルから AnimationDrawable としてアニメーションをロードし (すべての解析を行うため)、ビットマップを抽出してカスタム ストレージ クラスに配置します。

私が持っている問題:

Resources res = context.getResources(); 
AnimationDrawable drawable = (AnimationDrawable)res.getDrawable(R.drawable.born_animation); 
assertTrue( drawable != null ); <= fails! it's null 

なんてこと?誰かが私にそれを説明できますか?コードは正常にコンパイルされます。すべてのリソースが配置されています。

別の方法を試しました-ImageViewを使用して解析を行います(開発ガイドで説明されているように)

ImageView view = new ImageView(context); 
view.setBackgroundResource(R.drawable.born_animation); 
AnimationDrawable drawable = (AnimationDrawable)view.getBackground(); 
assertTrue( drawable != null ); <= fails! it's null 

結果は同じです。null ドローアブルを返します。

事前に感謝します。

4

3 に答える 3

29

ドローアブル

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"   
                android:id="@+id/myprogress" 
                android:oneshot="false">
    <item android:drawable="@drawable/progress1" android:duration="150" />
    <item android:drawable="@drawable/progress2" android:duration="150" />
    <item android:drawable="@drawable/progress3" android:duration="150" />
</animation-list> 

コード:

ImageView progress = (ImageView)findViewById(R.id.progress_bar);
if (progress != null) {
    progress.setVisibility(View.VISIBLE);
    AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
    frameAnimation.setCallback(progress);
    frameAnimation.setVisible(true, true);
}

意見

<ImageView
  android:id="@+id/progress_bar"
  android:layout_alignParentRight="true"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/myprogress" />
于 2010-01-25T22:56:29.820 に答える
7

ええ、私は原因を見つけました!:)

それは私の悪いことでした:私は私のanimation.xmlファイルの適切なフォーマットを持っていませんでした:

  • 私はandroid:属性の名前空間を使用しませんでした(何らかの理由で私はそれが必要でないと判断しました)
  • <item>タグの「duration」属性を削除しました

これらを修正した後、res.getDrawable()は正しいAnimationDrawableインスタンスを返し始めました。

Resources.NotFoundExceptionをより正確に調べる必要があり、何が問題なのかを見つけるのはgetCause()です:)

于 2010-01-26T13:56:59.393 に答える
3

これは、「xml」ディレクトリからリソースをロードするために使用できます。

Drawable myDrawable;
Resources res = getResources();
try {
   myDrawable = Drawable.createFromXml(res, res.getXml(R.xml.my_drawable));
} catch (Exception ex) {
   Log.e("Error", "Exception loading drawable"); 
}
于 2012-04-19T14:14:31.830 に答える