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
Android Push Notifications Not Being Received When App Closed
Android.Support.V7.App.Activitycompat Not Found
How to Convert Image to Zpl Code for Printing Using Zebra Printer
How to Emulate Gps Location in the Android Emulator
How to Select a Video from the Gallery and Get It's Real Path
Android Scrollview Not Scrolling Down After Keyboard Opens
Android Refresh a Fragment List from Its Parent Activity
How to Make Primary Key as Autoincrement for Room Persistence Lib
How to Set the Starting Position of a Scrollview
How to Use Adb With Emulator Like Memu/Bluestacks/Nox App Player
Unable to Add Window - Token Null Is Not Valid; Is Your Activity Running
How to Send a Firebase Notification to All Devices Via Curl
Setting Webview to View Desktop Site and Not Mobile Site
Move to Another Edittext When Soft Keyboard Next Is Clicked on Android
How to Show a Video Thumbnail from a Video Path
Resolving Problems With Android Studio Freezing Constantly
Android Set Height and Width of Custom View Programmatically