如何使用BitmapFactory.Options在ImageView中显示位图图像?

时间:2014-04-08 05:53:07

标签: android android-bitmap

我按照developers.android.com中的步骤有效地加载大位图的最佳实践,但我不能让视频缩略图在图像视图中出现,问题是什么?

holder.imageHolder.setImageBitmap(decodeSampledBitmapFromResource(
                    aListData.get(position).path, 130, 100));

    public Bitmap decodeSampledBitmapFromResource(String path,
            int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }

    public int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2
            // and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

如果它来自developers.android.com它应该可以工作,但我遵循指南,但它没有工作。 参考:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

0 个答案:

没有答案