Android 图像视图尺寸
我有以下情况: 我有Android 图像视图尺寸,android,android-layout,bitmap,android-image,dimensions,Android,Android Layout,Bitmap,Android Image,Dimensions,我有以下情况: 我有ImageView和Bitmap 问题是 我不知道位图的尺寸(不是物理尺寸,它在屏幕上的大小) 或 我需要ImageView不要比屏幕上的Bitmap更大 ImageView的背景在下面的快照上为黑色。您可以通过将图像视图的布局宽度和布局高度设置为在xml文件中的ImageView标记上包裹内容来确保图像视图不大于位图 您还可以使用其scaleType来影响应如何操纵图像以适合imageView 您还可以访问位图的宽度/高度属性以获取其尺寸 编辑:: 您可以使用以下帮
ImageView
和Bitmap
问题是
- 我不知道位图的尺寸(不是物理尺寸,它在屏幕上的大小)
- 我需要
不要比屏幕上的ImageView
更大Bitmap
ImageView
的背景在下面的快照上为黑色。您可以通过将图像视图的布局宽度和布局高度设置为在xml文件中的ImageView标记上包裹内容来确保图像视图不大于位图
您还可以使用其scaleType来影响应如何操纵图像以适合imageView
您还可以访问位图的宽度/高度属性以获取其尺寸
编辑::
您可以使用以下帮助程序将位图转换为字节[],并调整其大小:
/**
* Resize an image to a specified width and height.
* @param targetWidth The width to resize to.
* @param targetHeight The height to resize to.
* @return The resized image as a Bitmap.
* */
public static Bitmap resizeImage(byte[] imageData, int targetWidth, int targetHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = calculateInSampleSize(options, targetWidth, targetHeight);
options.inJustDecodeBounds = false;
Bitmap reducedBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
return Bitmap.createScaledBitmap(reducedBitmap, targetWidth, targetHeight, false);
}
private static int calculateInSampleSize(BitmapFactory.Options options, int requestedWidth, int requestedHeight) {
// Get the image's raw dimensions
final int rawHeight = options.outHeight;
final int rawWidth = options.outWidth;
int inSampleSize = 1;
if (rawHeight > requestedHeight || rawWidth > requestedWidth) {
final int halfHeight = rawHeight / 2;
final int halfWidth = rawWidth / 2;
/*
* Calculate the largest inSampleSize value that is a power of 2 and keeps both
* height and width larger than their requested counterparts respectively.
* */
while ((halfHeight/inSampleSize) > requestedHeight && (halfWidth/inSampleSize) > requestedWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
你们知道位图的屏幕宽度和长宽比。所以你们可以在屏幕上得到位图的大小。@tinysunlight,谢谢你们的快速回答。你能详细解释一下吗?你的位图和imageview是从哪里来的?如果你只是想剪辑位图,我认为使用第三方库更好。位图来自相机意图。我认为解决方案非常接近,但找不到原始位图大小为960x1260。屏幕宽度为900。所以屏幕上的位图大小为900x(900*960/1260)谢谢,但我的ImageView高度和宽度在wrap_内容中;我不想使用scaleType,因为它会使位图变形。问题是原始位图大小为960x1260。很明显,位图并没有填充屏幕上的尺寸(注意并更新了答案,以显示将位图调整为指定尺寸的方法。如果它不能满足您的使用要求,请一定查看一些相关的第三方库,如tiny sunlight所说。我个人使用毕加索处理位图转换。