如何在Android中流畅地循环播放大型位图 [英] How to loop large bitmaps smootly in Android
问题描述
我正在为电视和电视创建一个Android应用程序.平板电脑设备,以图像形式显示一种广告.
I am creating an Android Application for TV & Tablet devices which shows kind of advertisements in form of images.
我正在使用以下加载方法
I'm using following method to load Bitmaps
which I had stored in cache using Universal-image-loader library:
public static Bitmap getSampledBitmapFromFile(File file, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getPath(), options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(file.getPath(), options);
}
这是加载Bitmap
的方式:
for(int i ==0; i<media.size() ; i++){
new AsyncTask<String, Void, Bitmap>() {
private final WeakReference<ImageView> imageViewReference = new WeakReference<ImageView>(campaignImg);
// Decode image in background.
@Override
protected Bitmap doInBackground(String... params) {
return Config.getSampledBitmapFromFile(ImageLoader.getInstance().getDiskCache().get(params[0]),
mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels);
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
animateImages(imageView, media.interval);
}
}
}
}.execute(media.imageUrl);
}
我的问题是应用程序图像旋转不顺畅,更改时图像闪烁.图像将在10秒内更改.遵循我的logcat所说:
My problem is app image rotation isn't smooth and images flickering while change. Image is getting changed in 10 second. Following what my logcat says:
D/dalvikvm: GC_FOR_ALLOC freed 11788K, 66% free 12375K/36295K, paused 19ms, total 19ms
I/dalvikvm-heap: Grow heap (frag case) to 23.587MB for 12000016-byte allocation
D/dalvikvm: GC_CONCURRENT freed 2K, 34% free 24091K/36295K, paused 12ms+5ms, total 43ms
D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 17ms
D/dalvikvm: GC_FOR_ALLOC freed 5923K, 50% free 18241K/36295K, paused 59ms, total 60ms
I/dalvikvm-heap: Grow heap (frag case) to 29.316MB for 12000016-byte allocation
D/dalvikvm: GC_CONCURRENT freed 2K, 18% free 29957K/36295K, paused 13ms+6ms, total 50ms
D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 32ms
D/dalvikvm: GC_FOR_ALLOC freed 11791K, 50% free 18241K/36295K, paused 22ms, total 22ms
I/dalvikvm-heap: Grow heap (frag case) to 29.316MB for 12000016-byte allocation
GC_FOR_ALLOC freed 2K, 18% free 29957K/36295K, paused 29ms, total 29ms
我还能做些什么来减少内存使用或提高性能?
What else I can do to reduce memory usages or improve performance?
推荐答案
Rai的回答促使我使用UIL从缓存中加载图像.我想我应该早点做.
Rai's answer inspried me to use UIL for loading image from the cache. I think I should have done this earlier.
作为对此的学习,使用任何库进行图像加载,缓存或显示始终是一个好主意,而不是手动进行处理.因为我已经在项目中使用UIL,所以最终使用以下代码:
As a learning from this, its always a good idea to use any library for image loading,caching or display instead of manually handling it. As I was already using UIL in my project, I ended up using following:
ImageLoader.getInstance().displayImage(media.imageUrl, campaignImg, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
animateImages(campaignImg, media.interval);
}
});
这篇关于如何在Android中流畅地循环播放大型位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!