android activity 动态设置 dpi,如何在Android中将位图保存到位图时设置dpi图像?

以下是我用于管理截图和调整图像大小的一些辅助方法。

public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight, boolean recycleOriginal) {

int width = bm.getWidth();

int height = bm.getHeight();

// Determine scale to change size

float scaleWidth = ((float) newWidth)/width;

float scaleHeight = ((float) newHeight)/height;

// Create Matrix for maniuplating size

Matrix matrix = new Matrix();

// Set the Resize Scale for the Matrix

matrix.postScale(scaleWidth, scaleHeight);

//Create a new Bitmap from original using matrix and new width/height

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

//Remove memory leaks if told to recycle, warning, if using original else where do not recycle it here

if(recycleOriginal) {

bm.recycle();

}

//Return the scaled new bitmap

return resizedBitmap;

}

public static Bitmap cropImage(Bitmap imgToCrop, int startX, int startY, int width, int height, boolean recycleOriginal){

Bitmap croppedImage = Bitmap.createBitmap(imgToCrop, startX, startY , width , height);

if(recycleOriginal){

imgToCrop.recycle();

}

return croppedImage;

}

public static Bitmap takeScreenshotOfView(Activity context, Bitmap.CompressFormat compressFormat){

Bitmap screenshot = null;

try {

// create bitmap screen capture

View v1 = context.getWindow().getDecorView().getRootView();

v1.setDrawingCacheEnabled(true);

screenshot = Bitmap.createBitmap(v1.getDrawingCache());

v1.setDrawingCacheEnabled(false);

File imageFile = new File(context.getFilesDir() + File.separator + "A35_temp" + File.separator + "screenshot_temp");

FileOutputStream outputStream = new FileOutputStream(imageFile);

int quality = 100;

screenshot.compress(compressFormat, quality, outputStream);

outputStream.flush();

outputStream.close();

} catch (Throwable e) {

// Several error may come out with file handling or OOM

e.printStackTrace();

}

return screenshot;

}

这些都是我ImageHelper课程的一部分一样,我只是用:

Bitmap screenshot = ImageHelper.takeScreenshotOfView(this, Bitmap.CompressFormat.JPEG);

Bitmap croppedImage = ImageHelper.cropImage(screenshot, ImageHelper.mStartXCrop, ImageHelper.mStartYCrop, ImageHelper.mCropWidth, ImageHelper.mCropHeight, true);

returnImage = ImageHelper.getResizedBitmap(croppedImage, mCropImageWidth, mCropImageHeight, false);

我不认为你正在尝试做的截图,但你仍然可以使用大小调整方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值