How to resize an image to fit multiple screen densities

梦想的初衷 提交于 2019-11-29 15:45:01

问题


I need to add an avatar to a grid item.

I want to know how to handle the resizing of an image chosen from the phones gallery. Once chosen, I imagine some resizing will be needed, to fit it into the grid.

However, do I need to store a resized image for each screen density; store one xhdpi version and scale down for other devices, or be clever in some other way?

The reason is, the app stores this image to a cloud db and other people can download this image. They may see the image on different devices (hence the requirement of different image sizes). How should the managment of this image be processed?


回答1:


Do something like this:

 DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    Bitmap bitmapOrg = new BitmapDrawable(getResources(), new  ByteArrayInputStream(imageThumbnail)).getBitmap();

    int width = bitmapOrg.getWidth();
    int height = bitmapOrg.getHeight();

    float scaleWidth = metrics.scaledDensity;
    float scaleHeight = metrics.scaledDensity;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);



回答2:


I hope you find the code below useful. It will return image with reqd dimensions with minimum overhead. I have used this many times, works like charm. You can set the required dimension according to target device. Scaling will cause blur in picture but this doesn't.

private Bitmap getBitmap(Uri uri) {                             
    InputStream in = null;
    try {
        final int IMAGE_MAX_SIZE = 200000; // 0.2MP
        in = my_context.getContentResolver().openInputStream(uri);

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;                    //request only the dimesion
        BitmapFactory.decodeStream(in, null, o);
        in.close();

        int scale = 1;
        while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE) {
            scale++;
        }

        Bitmap b = null;
        in = my_context.getContentResolver().openInputStream(uri);
        if (scale > 1) {
            scale--;
            // scale to max possible inSampleSize that still yields an image
            // larger than target
            o = new BitmapFactory.Options();
            o.inSampleSize = scale;
            b = BitmapFactory.decodeStream(in, null, o);
            // resize to desired dimensions
            int height = b.getHeight();
            int width = b.getWidth();

            double y = Math.sqrt(IMAGE_MAX_SIZE
                    / (((double) width) / height));
            double x = (y / height) * width;

            Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
            b.recycle();
            b = scaledBitmap;
            System.gc();
        } else {
            b = BitmapFactory.decodeStream(in);
        }
        in.close();

        return b;
    } catch (IOException e) {

        return null;
    }

}



回答3:


android:scaleType="fitXY"
android:layout_gravity="center"

Will scale an image and center it Size the Size a container to fill_parent with and it should do just that.




回答4:


You can put your images to drawable (without creating xhdpi,hdpi,mdpi,ldpi...) (global images).

Then you can create 4 same layouts for different screensizes . All of your layout can use the images in your drawable director. So you can resize your image easly.



来源:https://stackoverflow.com/questions/13659682/how-to-resize-an-image-to-fit-multiple-screen-densities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!