I am trying to set bitmap as wallpaper. I am scaling the bitmap to the size of the screen (desired wallpaper size) before setting the wallpaper. Logging the desired size and the size of the scaled image shows confirms that the bitmap has the expected size. Still, the wallpaper gets zoomed in and the whole image isn't visible on the wallpaper. Could anyone point me in the correct direction? Here is the code I am using to scale and set the wallpaper.
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager =
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
display.getMetrics(metrics);
final int screenWidth = metrics.widthPixels;
final int screenHeight = metrics.heightPixels;
Context thisContext = MyApplication.getApp().getApplicationContext();
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(thisContext);
// myWallpaperManager.setWallpaperOffsetSteps(1, 1);
// myWallpaperManager.suggestDesiredDimensions(screenWidth, screenHeight);
// 3. Get the desired size.
final int width = myWallpaperManager.getDesiredMinimumWidth();
final int height = myWallpaperManager.getDesiredMinimumHeight();
// 4. Scale the wallpaper.
Bitmap bitmap = getBitmap();
Bitmap wallpaper = Bitmap.createScaledBitmap(bitmap, width, height, true);
Log.i("wapper", "updateWallpaper: "+width+" "+height+" "+wallpaper.getWidth()+" "+wallpaper.getHeight());
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
// sets both at same time
myWallpaperManager.setBitmap(
wallpaper,null, false,
WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM);
LogUtil.LOG_D(TAG, "completed updating wallpapers");
} else {
myWallpaperManager.setBitmap(wallpaper);
}
Related
The code I am using to set a background to a bitmap is
wallpaperManager.setBitmap(result, null, true, WallpaperManager.FLAG_SYSTEM);
wallpaperManager2.setBitmap(result, null, true, WallpaperManager.FLAG_LOCK);
However, the image is not centered and I believe it has to do with the null parameter, which accepts a Rect as visibleCropHint.
How would I set the result bitmap to be centered by X and Y?
As doc says:
Passing null for this parameter means that the full image should be
displayed if possible given the image's and device's aspect ratios,
etc.
So can't guarantee image displayed fully.
Try this(from here):
Bitmap img = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.paper));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap bitmap = Bitmap.createScaledBitmap(img, width, height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
I want to scale image to fit screen width and keep aspect ratio
if image height is smaller than screen height. I want to strength
it.
if image height is larger than screen height. I want to crop it.
I google for it but set scaleType to FitXY & AdjustViewInBounds doesn't work.
I test with 50x50 image and it's not working
// Use createScaledBitmap will cause OOM, so I set image to imageView first.
ImageView image= (ImageView) mPageView.findViewById(R.id.image);
// Set image to get IntrinsicWidth & IntrinsicHeight
image.setImageResource(imageDrawable);
// Change scale type to matrix
image.setScaleType(ImageView.ScaleType.MATRIX);
// Calculate bottom crop matrix
Matrix matrix = getBottomCropMatrix(mContext, image.getDrawable().getIntrinsicWidth(), image.getDrawable().getIntrinsicHeight());
// Set matrixx
image.setImageMatrix(matrix);
// Redraw image
image_image.invalidate();
And my matrix use following method
Matrix matrix = new Matrix();
// Get screen size
int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
int screenHeight = context.getResources().getDisplayMetrics().heightPixels;
// Get scale to match parent
float scaleWidthRatio = screenWidth / imageWidth;
float scaleHeightRatio = screenHeight / imageHeight;
// screenHeight multi by width scale to get scaled image height
float scaledImageHeight = imageHeight * scaleWidthRatio;
// If scaledImageHeight < screenHeight, set scale to scaleHeightRatio to fit screen
// If scaledImageHeight >= screenHeight, use width scale as height scale
if (scaledImageHeight >= screenHeight) {
scaleHeightRatio = scaleWidthRatio;
}
matrix.setScale(scaleWidthRatio, scaleHeightRatio);
I don't know where I am wrong. It just leave some blank at the bottom.
========
Update. I found out the problem. My matrix is right. The blank at the bottom is soft navigation bar. Use following method will get wrong value.
getResources().getDisplayMetrics()
Change it to
DisplayMetrics displaymetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getRealMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
and it works!
Update. I found out the problem. My matrix is right. The blank at the bottom is soft navigation bar. Use following method will get wrong value.
getResources().getDisplayMetrics()
Change it to
DisplayMetrics displaymetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getRealMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
and it works!
Try this,
Use android:scaleType centerCrop instead of fitXY or matrix.
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
We created a custom camera based on android.hardware.Camera class.
When we press the button "take a photo", the expected behavior is to take the photo and see the still captured photo on a Review Screen. BUT , the camera still works / the viewfinder is showing a 'live' shot (instead of the still Review) when we get to the Review Screen.
It looks like the holder doesn't work.
This has been observed on some Samsung Galaxy S3 phones (GT-I9300); but strangely on all other models everything works fine (the still image appears in the Review Screen as it should).
My solution up to this point is to create a layout where a view takes up the same view as the surfaceview.
Then I get the view bounds for the preview (in my case it's the size of the screen)
//Get our screen size
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
DisplayMetrics metrics = getResources().getDisplayMetrics();
Point size = new Point();
try {
display.getSize(size);
w_width = size.x;
w_height = size.y;
} catch (NoSuchMethodError e) {
w_width = display.getWidth();
w_height = display.getHeight();
}
Then I set the callback to be:
callback = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] bytes, Camera camera) {
//Get the view bounds to determine how to manipulate this image
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
//If the image is smaller than the screen, don't make the image bigger
int finalWidth = (w_width < options.outWidth) ? w_width : options.outWidth;
int finalHeight = (w_height < options.outHeight) ? w_height : options.outHeight;
int inSampleSize = 1;
options = new BitmapFactory.Options();
if (finalHeight > w_height || finalWidth > w_width) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) finalHeight / (float) w_height);
final int widthRatio = Math.round((float) finalWidth / (float) w_width);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
options.inSampleSize = inSampleSize;
//Decode the smaller image
Bitmap b = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
//Rotate the image correctly
Matrix mat = new Matrix();
mat.postRotate(orientation);
Bitmap newBitmap = Bitmap.createBitmap(b, 0, 0, options.outWidth, options.outHeight, mat, true);
imagePreview.setBackgroundDrawable(new BitmapDrawable(newBitmap));
imagePreview.setVisibility(View.VISIBLE);
}
};
So basically the surface view never stops showing the preview, but the imageview hides it so the user can't see it.
Then if the user decides not to keep the image then I just re-hide the view and the surfaceView will keep showing the live preview. Luckily the GS3 does not seem to mind someone calling "camera.startPreview();" even when the preview is already happening.
I really don't like this answer but it's all I have at the moment that works on the galaxy S3. It has worked on older devices (2.3 and up) that I have tried but it is definitely just a work-around.
Hi i am programmatically set a Home Screen Wallpaper. It's working fine. How to fit the Home Screen Wallpaper based on the Emulator Size. My Sample Code is here...
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Drawable drawable = getResources().getDrawable(R.drawable.sample);
Bitmap wallpaper = ((BitmapDrawable) drawable).getBitmap();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenHeight = displaymetrics.heightPixels;
int screenWidth = displaymetrics.widthPixels;
Bitmap bmp2 = Bitmap.createScaledBitmap(wallpaper, screenWidth, screenHeight, true);
try
{
wallpaperManager.setBitmap(wallpaper);
}
catch (IOException e)
{
e.printStackTrace();
}
Based on the Emulator Size to fit Home Screen Wallpaper, How?Please reply your comments and results are valuable me. Thanks.
I am working on a simple wallpaper app of some images that I have. They are .png files in drawable folders.
Here are some code snippets:
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
....
myWallpaperManager.setResource(R.drawable.image1);
No matter what size or resolution I seem to use, when the wallpaper is set it crops the original image. When I use the same image as a background it is the correct size and shows the entire image. I thought it might be a problem with my emulator so I have tried running it on an actual device (HTC eris) and I am having the same problem. I have made the image the screen size and resolution for the eris and it is still cropping it. I then made the image only one inch high and a resolution of 100 dpi. It was very pixelated on the eris, but still cropped the image.
Any help would be greatly appreciated.
I attempted to add some images to show the before and after, but as a newer user I was prevented from doing so.
Check the values returned by http://developer.android.com/reference/android/app/WallpaperManager.html#getDesiredMinimumWidth() and http://developer.android.com/reference/android/app/WallpaperManager.html#getDesiredMinimumHeight() and try to use these values as the dimensions of your wallpaper.
Maybe I can help.
// 1. Get screen size.
DisplayMetrics metrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
display.getMetrics(metrics);
final int screenWidth = metrics.widthPixels;
final int screenHeight = metrics.heightPixels;
// 2. Make the wallpaperManager fit the screen size.
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(ViewWallpaperActivity.this);
wallpaperManager.suggestDesiredDimensions(screenWidth, screenHeight);
// 3. Get the desired size.
final int width = wallpaperManager.getDesiredMinimumWidth();
final int height = wallpaperManager.getDesiredMinimumHeight();
// 4. Scale the wallpaper.
Bitmap bitmap = getBitmap(); // getBitmap(): Get the image to be set as wallpaper.
Bitmap wallpaper = Bitmap.createScaledBitmap(bitmap, width, height, true);
// 5. Set the image as wallpaper.
try {
wallpaperManager.setBitmap(wallpaper);
} catch (IOException e) {
e.printStackTrace();
}
Note that you should call suggestDesiredDimensions, then call getDesiredMinimumWidth and getDesiredMinimumHeight to get the size to be scaled to.
I had the same problem. I made an image that is the size of the screen and adding padding to the image so that it is as large as the WallpaperManager getDesiredMinimumWidth() and getDesiredMinimumHeight().
It seemed better to have some code automatically add the padding so that is what I used below. Make sure the image is the same size as the screen.
private void setWallpaper() {
try {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
//import non-scaled bitmap wallpaper
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap wallpaper = BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper, options);
if (wallpaperManager.getDesiredMinimumWidth() > wallpaper.getWidth() &&
wallpaperManager.getDesiredMinimumHeight() > wallpaper.getHeight()) {
//add padding to wallpaper so background image scales correctly
int xPadding = Math.max(0, wallpaperManager.getDesiredMinimumWidth() - wallpaper.getWidth()) / 2;
int yPadding = Math.max(0, wallpaperManager.getDesiredMinimumHeight() - wallpaper.getHeight()) / 2;
Bitmap paddedWallpaper = Bitmap.createBitmap(wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight(), Bitmap.Config.ARGB_8888);
int[] pixels = new int[wallpaper.getWidth() * wallpaper.getHeight()];
wallpaper.getPixels(pixels, 0, wallpaper.getWidth(), 0, 0, wallpaper.getWidth(), wallpaper.getHeight());
paddedWallpaper.setPixels(pixels, 0, wallpaper.getWidth(), xPadding, yPadding, wallpaper.getWidth(), wallpaper.getHeight());
wallpaperManager.setBitmap(paddedWallpaper);
} else {
wallpaperManager.setBitmap(wallpaper);
}
} catch (IOException e) {
Log.e(TAG,"failed to set wallpaper");
}
}