2017-03-29 27 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(); 

但此代码导致内存不足异常。是否有可能加载与毕加索的动画列表?

+0

你有什么发现吗?请回复。 –

+0

对不起,从来没有想过,然后我放弃了这个项目。 – Christian

回答

0

毕加索无法将xml文件中定义的animation-list直接加载到View中。但它不是太难模仿毕加索的基本行为自己:

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.如果您的绘图资源比你查看大图,只在需要的分辨率加载可绘制

而不是直接添加绘制节省内存:

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

添加比例它的下行版本:

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

其中wh是你的视图的尺寸,而decodeSampledBitmapFromResource()是Android官方文档中定义的一种方法(Loading Large Bitmaps Efficiently