如何使用 Picasso 加载动画列表?
How to load animation-lists with Picasso?
我在将图像加载到我的应用程序时出现内存不足异常。我集成了 Picasso 来加载图像,但下面的代码不适用于 AnimationDrawable 的动画列表。动画为空:
Picasso.with(this).load(R.drawable.qtidle).into(qtSelectButton);
qtIdleAnimation = (AnimationDrawable)qtSelectButton.getDrawable();
if (qtIdleAnimation != null)
qtIdleAnimation.start();
AnimationDrawable 有效,如果我在没有 Picasso 的情况下使用此代码:
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 加载动画列表?
Picasso 无法直接将 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 的子类:
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);
添加它的缩小版本:
a.addFrame(new BitmapDrawable(res, decodeSampledBitmapFromResource(res, id, w, h));
其中 w
和 h
是视图的维度,decodeSampledBitmapFromResource()
是 Android 官方文档中定义的方法 (Loading Large Bitmaps Efficiently)
我在将图像加载到我的应用程序时出现内存不足异常。我集成了 Picasso 来加载图像,但下面的代码不适用于 AnimationDrawable 的动画列表。动画为空:
Picasso.with(this).load(R.drawable.qtidle).into(qtSelectButton);
qtIdleAnimation = (AnimationDrawable)qtSelectButton.getDrawable();
if (qtIdleAnimation != null)
qtIdleAnimation.start();
AnimationDrawable 有效,如果我在没有 Picasso 的情况下使用此代码:
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 加载动画列表?
Picasso 无法直接将 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 的子类:
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);
添加它的缩小版本:
a.addFrame(new BitmapDrawable(res, decodeSampledBitmapFromResource(res, id, w, h));
其中 w
和 h
是视图的维度,decodeSampledBitmapFromResource()
是 Android 官方文档中定义的方法 (Loading Large Bitmaps Efficiently)