I am attempting to re-size an image and maintain its aspect ratio the bitmap mBitmap measures 1200x539 and I need to reduce this to about 1/3rd of that.
mBitmap = Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);;
int H = (int)mBitmap.getHeight();
int W =(int)mBitmap.getWidth();
nBitmap = BitmapScaler.setBitmapScale(mBitmap, W,H);
I spotted this answer provided by Streets Of Boston and have attempted to use it in my app but I may have messed up the variables and I am getting a blank image the same size as the original, can anyone show me how to achieve this correctly?
Scaled Bitmap maintaining aspect ratio
The code runs without error but returns an image the same size as the original!
public static Bitmap setBitmapScale(Bitmap originalImage, int width, int height){
Bitmap background = Bitmap.createBitmap((int)width, (int)height, Config.ARGB_8888);
float originalWidth = originalImage.getWidth(), originalHeight = originalImage.getHeight();
Canvas canvas = new Canvas(background);
float scale = width/originalWidth;
float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale)/2.0f;
Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(originalImage, transformation, paint);
return background;
}