如何在 Android 中平滑地循环大位图

How to loop large bitmaps smootly in Android

我正在为电视和平板设备创建一个 Android 应用程序,它以图像形式显示某种广告。

我正在使用以下 method to load Bitmaps,我使用 Universal-image-loader 库将其存储在缓存中:

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

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

使用 Picasso or Glide 库来避免内存不足问题。

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

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

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