0
我最初显示一个空的视图。当我点击“添加图像”按钮时,它会在网格中显示一组图像,然后点击图像。我通过使用拖动图层动态添加ImageView来将图像放置在视图中。通过这种方式,我添加了更多图像。现在我想以编程方式调整每个图像的大小。可能吗?如果可能,请提供代码。是否有可能通过点击按钮来调整图像编程?
我最初显示一个空的视图。当我点击“添加图像”按钮时,它会在网格中显示一组图像,然后点击图像。我通过使用拖动图层动态添加ImageView来将图像放置在视图中。通过这种方式,我添加了更多图像。现在我想以编程方式调整每个图像的大小。可能吗?如果可能,请提供代码。是否有可能通过点击按钮来调整图像编程?
您可以使用下面的代码来调整图像大小。如果你知道图片的路径和所需的宽度和高度
public static Bitmap resizeBitMapImage1(String filePath, int targetWidth,
int targetHeight) {
Bitmap bitMapImage = null;
// First, get the dimensions of the image
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
double sampleSize = 0;
// Only scale if we need to
// (16384 buffer for img processing)
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
.abs(options.outWidth - targetWidth);
if (options.outHeight * options.outWidth * 2 >= 1638) {
// Load, scaling to smallest power of 2 that'll get it <= desired
// dimensions
sampleSize = scaleByHeight ? options.outHeight/targetHeight
: options.outWidth/targetWidth;
sampleSize = (int) Math.pow(2d,
Math.floor(Math.log(sampleSize)/Math.log(2d)));
}
// Do the actual decoding
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[128];
while (true) {
try {
options.inSampleSize = (int) sampleSize;
bitMapImage = BitmapFactory.decodeFile(filePath, options);
break;
} catch (Exception ex) {
try {
sampleSize = sampleSize * 2;
} catch (Exception ex1) {
}
}
}
return bitMapImage;
}
感谢 迪帕克