我想实现“扩展”按钮以使图像适合选定的边界。
public void extendS(View v){
ImageView iv = current;
double width = gallery.getWidth();
double hight= gallery.getHeight();
double aspect = (width+0)/(hight+0);
Log.d("aspect", "w: "+width+" h: "+hight+" a: "+aspect);
if (aspect>1){
hight/=aspect;
}else{
width*=aspect;
}
Bitmap b = ((BitmapDrawable) iv.getDrawable()).getBitmap();
// Matrix matrix = new Matrix();
Log.d("aspect", "w: "+width+" h: "+hight+" a: "+aspect);
Bitmap scale = Bitmap.createBitmap(b, 0, 0, (int)width, (int)hight);
iv.setImageBitmap(scale);
我收到的错误:07-16 12:26:36.855:E / AndroidRuntime(12647):引起:java.lang.IllegalArgumentException:y + height必须是< = bitmap.height() 从我的观点来看,这个错误似乎有点奇怪
答案 0 :(得分:1)
public static Bitmap resizeBitmap(Bitmap photo, float x, float y) {
try {
// get current bitmap width and height
int width = photo.getWidth();
int height = photo.getHeight();
// determine how much to scale
float scaleWidth = x / width;
float scaleHeight = y / height;
// create the matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bitmap
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new bitmap
Bitmap resizebitmap = Bitmap.createBitmap(photo, 0, 0, width,
height, matrix, false);
return resizebitmap;
} catch (NullPointerException e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
System.gc();
}
return null;
}
答案 1 :(得分:1)
您收到此错误是因为您尝试访问原始位图区域之外的数据。你实际上非常接近,你只是使用了错误的Bitmap.createBitmap()函数。尝试使用不同大小的原始源创建位图:
Bitmap.createScaledBitmap(b, (int)width, (int)hight, true);
另外,你的宽高比是错误的。提示:您无需担心此代码的宽高比。