Android 是否有可能通过点击按钮来调整图像的大小?
我最初显示的是一个空视图。当我点击“添加图像”按钮时,它会在网格中显示一组图像,然后我点击一个图像。我通过使用拖动层动态添加ImageView,将图像放置在视图中。通过这种方式,我添加了更多的图像。现在我想以编程方式调整每个图像的大小。可能吗?如果可能,请提供代码。您可以使用以下代码调整图像大小。如果您知道图像路径以及所需的宽度和高度Android 是否有可能通过点击按钮来调整图像的大小?,android,Android,我最初显示的是一个空视图。当我点击“添加图像”按钮时,它会在网格中显示一组图像,然后我点击一个图像。我通过使用拖动层动态添加ImageView,将图像放置在视图中。通过这种方式,我添加了更多的图像。现在我想以编程方式调整每个图像的大小。可能吗?如果可能,请提供代码。您可以使用以下代码调整图像大小。如果您知道图像路径以及所需的宽度和高度 public static Bitmap resizeBitMapImage1(String filePath, int targetWidth,
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;
}
public静态位图resizeBitMapImage1(字符串文件路径,int-targetWidth,
int targetHeight){
位图bitMapImage=null;
//首先,获取图像的尺寸
选项=新选项();
options.inJustDecodeBounds=true;
解码文件(文件路径,选项);
双重抽样=0;
//只有在我们需要的时候才能扩大规模
//(用于img处理的16384缓冲器)
布尔scaleByHeight=Math.abs(options.outHeight-targetHeight)>=Math
.abs(可选。外宽-目标宽);
如果(options.outHeight*options.outWidth*2>=1638){
//加载,缩放到2的最小幂次方即可您可以使用以下代码调整图像大小。如果您知道图像路径以及所需的宽度和高度
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;
}
public静态位图resizeBitMapImage1(字符串文件路径,int-targetWidth,
int targetHeight){
位图bitMapImage=null;
//首先,获取图像的尺寸
选项=新选项();
options.inJustDecodeBounds=true;
解码文件(文件路径,选项);
双重抽样=0;
//只有在我们需要的时候才能扩大规模
//(用于img处理的16384缓冲器)
布尔scaleByHeight=Math.abs(options.outHeight-targetHeight)>=Math
.abs(可选。外宽-目标宽);
如果(options.outHeight*options.outWidth*2>=1638){
//加载,缩放到最小的2次方即可