如何在Android

时间:2016-04-26 10:12:29

标签: android memory-management bitmap universal-image-loader

我正在为TV& amp创建一个Android应用程序平板电脑设备,以图像的形式显示各种广告。

我正在使用我使用Universal-image-loader库存储在缓存中的method to load Bitmaps

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说的那样:

 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

我还能做些什么来减少内存使用或提高性能?

2 个答案:

答案 0 :(得分:1)

使用PicassoGlide库来避免内存不足问题。

将路径替换为本地文件夹路径或服务器URL

Glide.with (context).load (path).asBitmap().into(imageView);

答案 1 :(得分:1)

Rai的回答告诉我使用UIL从缓存中加载图像。我想我应该早点做到这一点。

从中学习,使用任何库进行图像加载,缓存或显示而不是手动处理它总是一个好主意。由于我在项目中已经使用了UIL,我最终使用了以下内容:

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