Android: How to Scale a Bitmap to Fit the Screen Size Using Canvas/Draw Bitmap

Android: How do I scale a bitmap to fit the screen size using canvas/draw bitmap?

Rect frameToDraw = new Rect(0, 0, width, height);
RectF whereToDraw = new RectF(0, 0, width, height);

Take a look at the above code. Your are not scaling the picture. You are simply taking a part of the picture (in this case the whole picture) and pasting it somewhere (in this case the size of the original picture).

The width and heigth of the frameToDraw rectangle should be the width and height of your picture.

Scaling a Bitmap to fit in a canvas

I'm going to add to nycynik and nikmin's answers.

You can utilise the 'createScaledBitmap' function to scale the Bitmap to the size of the screen (well, the canvas). Try to make your Bitmap resource fairly high-res so that if it is used on a larger screen, it won't have ugly scaling artifacts.

How to scale bitmap to screen size?

Try this to Decode the Bitmap :

Where imagefilepath is the path name of image,it will be in String covert that to File by using

File photos= new File(imageFilePath);

Where photo is the File name of the Image,Now you set your height and width according t your requirements.

public void main(){
Bitmap bitmap = decodeFile(photo);
bitmap = Bitmap.createScaledBitmap(bitmap,150, 150, true);
imageView.setImageBitmap(bitmap);
}

private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale++;
}

//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}

Android draw a part of scaled bitmap using canvas

do it this way

Rect rs = new Rect();
Rect rd = new Rect();
rs.left = rs.top = 0;
rs.right = 480;
rs.bottom = 800;

<calculate destination rectangle from device size>

canvas.drawBitmap(myBitmap, rs, rd, null);

You can also scale and translate (shift) the entire canvas

canvas.scale(float scaleX, float scaleY);
canvas.translate(float dx, float dy);

How to resize bitmap when drawing in canvas?

You can call public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) for this function (Android Doc). The code below should accomplish what you are trying to do:

canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);

Scaled Bitmap maintaining aspect ratio

What about this:

Bitmap background = Bitmap.createBitmap((int)width, (int)height, Config.ARGB_8888);

float originalWidth = originalImage.getWidth();
float originalHeight = originalImage.getHeight();

Canvas canvas = new Canvas(background);

float scale = width / originalWidth;

float xTranslation = 0.0f;
float 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;

I added a paint to filter the scaled bitmap.



Related Topics



Leave a reply



Submit