问题
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);
}
This is how loading 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);
}
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?
回答1:
Rai's answer inspried me to use UIL for loading image from the cache. I think I should have done this earlier.
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);
}
});
回答2:
Use Picasso or Glide libraries to avoid out of memory issues.
Replace path with local folder path or server url
Glide.with (context).load (path).asBitmap().into(imageView);
来源:https://stackoverflow.com/questions/36861868/how-to-loop-large-bitmaps-smootly-in-android