I am setting an image from within my application as wallpaper it is working fine on my nokia 5 and image is being set and is filling the entire view but when i try to run the same code on huawei honor 5x or nexus 4 the image is enlarged and goes out of the home screen.
private Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
return resizedBitmap;
}
Related
i have try to remove the black background after the rotate image in my code
Bitmap rotate(float x, Bitmap bitmapOrg) {
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 200;
int newHeight = 200;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(x);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,height, matrix, true);
//Canvas canvas = new Canvas(resizedBitmap);
//canvas.drawColor(Color.TRANSPARENT);
return resizedBitmap;
}
how can i remove black background i have seen that only in my android lolipop , i also try in my emulator Google galaxy nexus - 4.2.2 in that black background is not display .
You must use resizedBitmap.setHasAlpha(true);
put that line of code right after you declare your bitmap.
I want to resize the bitmap height & width into same as the user device width & height.So please help me if u know the code.Thank You !
This is my code:
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.nature);
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
width = metrics.widthPixels;
height=metrics.heightPixels;
But it was not working
Try to use this method:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
// GET CURRENT SIZE
int width = bm.getWidth();
int height = bm.getHeight();
// GET SCALE SIZE
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
Bitmap.createScaledBitmap works great. As the document:
Creates a new bitmap, scaled from an existing bitmap, when possible. If the specified width and height are the same as the current width and height of the source bitmap, the source bitmap is returned and no new bitmap is created.
I found this bit of code on a similar question, but I can't figure out how to use it.
Resizing a Bitmap:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
I'm also confused as to where to place this. With my initial variables? Inside the constructor, or runnable? If the bitmap I wanted to change the size of is called Background, how do I add that to this method?
Thanks in advance.
I am trying to scale a Bitmap, doubling its size.
But after the scaling the bitmap shows empty, all plain gray...
here is the code:
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(2, 2);
// recreate the new Bitmap and set it back
Bitmap bm2=Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
//bm.recycle();
EDIT EDIT EDIT EDIT EDIT
I figured it out it's memory issue, if I do it with small images it works fine.
Still the problem remains with large pictures!!!
Thanks for any suggestion!!!
As from the Bitmap Doc it is clearly saying that Bitmap.createBitmap() returns the same bitmap or part of the source bitmap.So may be here it returns the same bitmap object.But here you are recycling it
bm.recycle();
thats why you are getting null
Use this method
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
and pass width as 1920 and height as 2560
I use the following method to resize images on Android.
public Bitmap resize(Bitmap img, int Width, int Height) {
int width = img.getWidth();
int height = img.getHeight();
int newWidth = (int) Width;
int newHeight = (int) Height;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
//matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
return resizedBitmap;
}
It works fine on most Android devices. But on some devices the resized image is not displaying. How might I resolve this?
You may create a scaled bitmap with
Bitmap bitmap = Bitmap.createScaledBitmap(b, width, height, true);
Here, width and height you can provide according to the device's screen size for different devices, by reading screen sizes programmatically. Here is how you can read the screen size of a device programmatically.
Most likely, on "some devices" and depending on the requested width and height, there's not enough memory to create the resized bitmap. Check (in debugger) if the resizedBitmap is null when the image "is not displaying".