2017-03-29 15 views
2

私のアプリに画像を読み込む際にメモリの例外が発生します。イメージを読み込むためにPicassoを統合しましたが、以下のコードはAnimationDrawableのアニメーションリストでは動作しません。アニメーションはnullです:ピカソでアニメーションリストを読み込むには?

Picasso.with(this).load(R.drawable.qtidle).into(qtSelectButton); 
qtIdleAnimation = (AnimationDrawable)qtSelectButton.getDrawable(); 
if (qtIdleAnimation != null) 
    qtIdleAnimation.start(); 

AnimationDrawable私はピカソせずにこのコードを使用している場合、動作します:

qtIdleAnimation = new AnimationDrawable(); 
qtIdleAnimation.setOneShot(false); 
for (int i = 1; i <= 7; i++) { 
    int qtidleId = res.getIdentifier("qtidle" + i, "drawable", this.getPackageName()); 
    qtIdleAnimation.addFrame(res.getDrawable(qtidleId), 100); 
} 
qtSelectButton.setImageDrawable(qtIdleAnimation); 
if (qtIdleAnimation != null) 
    qtIdleAnimation.start(); 

しかし、このコードはメモリの例外のうちの原因となります。ピカソでアニメーションリストを読み込むことは可能ですか?

+0

見つけましたか?返信してください。 –

+0

申し訳ありませんが、それを理解したことはありませんし、私はプロジェクトを放棄しました。 – Christian

答えて

0

ピカソはxmlファイルに定義されているanimation-listをビューに直接ロードできません。しかし、ピカソ自身の基本的な動作を模倣するためにあまりにも難しいことではありません。

1.定義し、起動アニメーションをプログラムで、あなたがやったように、しかし、AsyncTask内でメモリ不足の例外

Resources res = this.getResources(); 
ImageView imageView = findViewById(R.id.image_view); 
int[] ids = new int[] {R.drawable.img1, R.drawable.img2, R.drawable.img3}; 
を避けるために、

AsyncTaskのsublcassを作成します。

private class LoadAnimationTask extends AsyncTask<Void, Void, AnimationDrawable> { 
    @Override 
    protected AnimationDrawable doInBackground(Void... voids) { 
     // Runs in the background and doesn't block the UI thread 
     AnimationDrawable a = new AnimationDrawable(); 
     for (int id : ids) { 
      a.addFrame(res.getDrawable(id), 100); 
     } 
     return a; 
    } 
    @Override 
    protected void onPostExecute(AnimationDrawable a) { 
     // This will run once 'doInBackground' has finished 
     imageView.setImageDrawable(a); 
     if (a != null) 
      a.start(); 
    } 
} 

次に、あなたのImageViewのにアニメーションをロードするためにタスクを実行します。

new LoadAnimationTask().execute() 

2.あなたのDrawableがあなたの視野よりも大きい場合には、唯一の代わりに直接描画可能を追加するのに必要な解像度

でドローアブルをロードすることによって、メモリを節約:

a.addFrame(res.getDrawable(id), 100); 

がスケーリングを追加それの-downバージョン:

a.addFrame(new BitmapDrawable(res, decodeSampledBitmapFromResource(res, id, w, h)); 

whはあなたのビューの寸法です。decodeSampledBitmapFromResource()はAndroidの公式ドキュメント(Loading Large Bitmaps Efficiently)に定義されているメソッドです

関連する問題