Reputation: 4627
I am tring to implement "extend" button to fit image into chosen boundaries.
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);
Error which I recieve: 07-16 12:26:36.855: E/AndroidRuntime(12647): Caused by: java.lang.IllegalArgumentException: y + height must be <= bitmap.height() this error seems a bit wierd from my point of view
Upvotes: 0
Views: 137
Reputation: 2374
You're getting this error because you're trying to access data outside the original bitmap's area. You're actually very close, you're just using the wrong Bitmap.createBitmap() function. Try this to create a bitmap from the original source with a different size:
Bitmap.createScaledBitmap(b, (int)width, (int)hight, true);
Also, you're doing the aspect ratio wrong. Hint: you don't need to worry about aspect ratio with this code.
Upvotes: 1
Reputation: 21117
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;
}
Upvotes: 1