如何调整图像以适合多种屏幕密度 [英] How to resize an image to fit multiple screen densities

查看:166
本文介绍了如何调整图像以适合多种屏幕密度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要头像添加到一个网格项。

我想知道如何处理来自手机的画廊选择图像的大小调整。一旦选择了,我想一些大小调整是必要的,以适应它并入电网。

不过,我需要存储每个屏幕密度的调整后的图像;存储一个xhdpi版本,并缩减其他设备,或巧以其他方式?

原因是,该应用商店这个图片给云数据库和其他人可以下载这个形象。他们可能会看到不同的设备上(因此不同的图像大小的要求)的图像。应如何图像管理中的应用进行处理?

解决方案
  

做这样的事情:

  DisplayMetrics指标=新DisplayMetrics();
    。getWindowManager()getDefaultDisplay()getMetrics(度量)。

    位图bitmapOrg =新BitmapDrawable(getResources(),新ByteArrayInputStream的(imageThumbnail))getBitmap()。

    INT宽度= bitmapOrg.getWidth();
    INT高= bitmapOrg.getHeight();

    浮scaleWidth = metrics.scaledDensity;
    浮scaleHeight = metrics.scaledDensity;

    //创建矩阵用于操作
    字模=新的Matrix();
    //调整位图
    matrix.postScale(scaleWidth,scaleHeight);

    //重新创建新的位图
    位图resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0,宽度,高度,矩阵,真);
 

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?

解决方案

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

这篇关于如何调整图像以适合多种屏幕密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆