use*_*594 1 java android image crop
好吧,这一切折磨了我好几个星期,我将图像设置为 227 像素高,将其缩放到 170 像素,即使我希望它在任何时候都是 wrap_content。
好的。在这里,我采用了 1950 像素长的我的图像(我把它的一部分放在这里,这样你就可以理解它应该是什么样子)。

首先,我想将其缩放回 227 像素高,因为这就是它的设计方式以及应该如何
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ver_bottom_panel_tiled_long);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 200; //this should be parent's whdth later
int newHeight = 227;
// calculate the scale
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 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);
BitmapDrawable dmpDrwbl=new BitmapDrawable(resizedBitmap);
verbottompanelprayer.setBackgroundDrawable(dmpDrwbl);
Run Code Online (Sandbox Code Playgroud)
所以......它根本不是裁剪图像 - 不,它是 1950 像素压成 200 像素。

但我只想剪除这 200 像素或我将设置的任何宽度之外的任何东西 - 裁剪它而不是将所有长图像压入 200 像素区域。
还有,BitmapDrawable(Bitmap bitmap); 和 imageView.setBackgroundDrawable(drawable); 已弃用 - 我该如何更改?
根据我所看到的,您创建了一个新尺寸 (200x227) 的位图,所以我不确定您的期望。你甚至在评论中写了你缩放的内容,但没有关于裁剪的字样......
你可以做的是:
如果 API 至少为 10 (gingerbread) ,则可以使用BitmapRegionDecoder,使用decodeRegion:
如果API太旧,需要解码大的位图,然后裁剪成新的位图,使用Bitmap.createBitmap
像这样:
final Rect rect =...
if (VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD_MR1)
{
BitmapRegionDecoder decoder=BitmapRegionDecoder.newInstance(imageFilePath, true);
croppedBitmap= decoder.decodeRegion(rect, null);
decoder.recycle();
}
else
{
Bitmap bitmapOriginal=BitmapFactory.decodeFile(imageFilePath, null);
croppedBitmap=Bitmap.createBitmap(bitmapOriginal,rect.left,rect.top,rect.width(),rect.height());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9369 次 |
| 最近记录: |