具有任意纵横比的android画廊

Jef*_*man 5 android image gallery

我希望有一个 android 画廊,将托管不同纵横比的图像。我想要的是画廊中图像的 CENTER_CROP 之类的东西。但是,当我将图像比例类型设置为此时,图像会超出图库图像边框。

当然, FIT_XY 会产生压扁/展平的图像。CENTER 导致画廊图像边框内的水平或垂直黑色空间。

任何想法如何做到这一点?我能找到的每个例子都使用带有固定尺寸图像的 FIT_XY。我想我可以自己裁剪图像,但我不想。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView iv = (ImageView) convertView;
    if (iv == null) {
        iv = new ImageView(context);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setBackgroundResource(galleryItemBackground);
        iv.setLayoutParams(new Gallery.LayoutParams(200, 200));
    }

    InputStream is;
    try {
        is = getInputStream(position);
    } catch (IOException ioe) {
        // TODO?
        throw new RuntimeException(ioe);
    }
    Bitmap bm = BitmapFactory.decodeStream(is);
    iv.setImageBitmap(bm);



    /*
     * if (bitmaps[position] != null) { bitmaps[position].recycle();
     * bitmaps[position] = null; } bitmaps[position] = bm;
     */

    return iv;
}
Run Code Online (Sandbox Code Playgroud)

Jef*_*man 0

我最终只是自己将位图修剪成正方形,因为,

Bitmap bm = BitmapFactory.decodeStream(is);

// make it square

int w = bm.getWidth();
int h = bm.getHeight();
if (w > h) {
    // trim width
    bm = Bitmap.createBitmap(bm, (w - h) / 2, 0, h, h);
} else {
    bm = Bitmap.createBitmap(bm, 0, (h - w) / 2, w, w);
}
Run Code Online (Sandbox Code Playgroud)