0

对于使用带有图像视图的网格视图创建的自定义画廊,我希望从 sd 存储中读取图像。这给我带来了巨大的性能问题,因为它会读取整个图像,并将其加载到 imageview 中。

  • 如何在运行时将大图像作为拇指读取并强调性能?

    File imgFile = new File(img.getInternalImagePath()); if(imgFile.exists()){ Bitmap myBitmap; try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 4; myBitmap = BitmapFactory.decodeStream(new FlushedInputStream(new FileInputStream(imgFile)),null,options); picture.setImageBitmap(myBitmap);

提前致谢。

/安迪

编辑:添加了一些代码来查看

4

4 回答 4

2

利用

ThumbnailUtils.extractThumbnail

还可以考虑遵循本教程

有效加载大型位图

于 2013-08-02T11:26:45.163 回答
1
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(fileName);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);  
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);  
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();

或者

Bitmap thumbBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filePath), thumbWidth, thumbHeight);
于 2013-08-02T11:28:06.217 回答
0

流行的技巧是通过跳过一些像素来对原始图像进行采样。稍微优化的代码(仅限方形缩略图):

public static Bitmap getImageThumbnail(String filePath, int size) {

    Options queryOPs = queryBitmap(filePath);

    int imgSize = Math.max(queryOPs.outWidth, queryOPs.outHeight);
    imgSize = Math.max(imgSize, size);

    int sampleSize = 1;

    while (imgSize / (sampleSize * 2) > size) {
        sampleSize *= 2;
    }

    Options decodeOps = new Options();
    decodeOps.inSampleSize = sampleSize;

    Bitmap img = BitmapFactory.decodeFile(filePath, decodeOps);

    if (img == null) {
        return null;
    }

    Bitmap thumb = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);
    Canvas can = new Canvas(thumb);

    Paint pnt = new Paint(Paint.DITHER_FLAG);

    can.drawBitmap(img, 0, 0, pnt);
    return thumb;
}

private static Options queryBitmap(String filePath) {

    Options ops = new Options();
    ops.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, ops);
    return ops;
}
于 2013-08-02T11:30:36.510 回答
0
@Override
public View getView(final int position, View view, ViewGroup parent) {
    pos = position;
    View v = view;
    RecordHolder holder = null;
    LayoutInflater mInflater = (LayoutInflater) mContext
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (v == null) {
        v = mInflater.inflate(R.layout.items, parent, false);
        holder = new RecordHolder();
        holder.imagev = (ImageView) v.findViewById(R.id.imag_v);
        v.setTag(holder);
    } else {

        holder = (RecordHolder) v.getTag();
    }
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 6;
    Bitmap bitmap1 = BitmapFactory.decodeResource(v.getResources(),
            items.get(position), options);
    holder.imagev.setImageBitmap(bitmap1);

} }

于 2014-12-31T09:57:19.790 回答