2017-03-29 9 views
2

이미지를 내 앱에로드 할 때 메모리 예외가 발생합니다. 이미지를로드하기 위해 Picasso를 통합했지만, 아래 코드는 AnimationDrawable의 애니메이션 목록으로 작업하지 않습니다. 애니메이션은 널 :피카소로 애니메이션 목록을로드하는 방법은 무엇입니까?

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

하지만이 코드는 메모리 예외의 아웃됩니다. Picasso로 애니메이션 목록을로드 할 수 있습니까?

+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}; 

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

이 그런 다음 이미지 뷰에 애니메이션을로드하는 작업을 실행합니다 :

AsyncTask의 sublcass 만들기 당신의 드로어 블가,보기보다 큰 경우에만 필요한 해상도로 드로어 블로드

대신 직접 그릴 수를 추가하여 메모리를 저장하면

new LoadAnimationTask().execute() 

2 :

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

추가 축소 그것의 -down 버전 :

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

wh은보기의 크기이며 decodeSampledBitmapFromResource()은 Android 공식 문서 (Loading Large Bitmaps Efficiently)에 정의 된 방법입니다.