Image gets zoomed while setting as wallpaper in my android application - android

I am trying to set my image as wallpaper but it gets zoomed/cropped when set as wallpaper. I have tried almost everything but nothing works.
Here is my piece of code:
public void setAswallpaper(Bitmap wallpaper)
{
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
System.out.println("screen dimensions "+width+" "+height);
System.out.println("wallpaper dimensions "+wallpaper.getWidth()+" "+wallpaper.getHeight());
wallpaper = Bitmap.createScaledBitmap(wallpaper, width*2 , height, true);
try {
WallpaperManager wallpaperManager=WallpaperManager.getInstance(context);
wallpaperManager.suggestDesiredDimensions(width,height);
System.out.println("wallpaper manager dimensions "+wallpaperManager.getDesiredMinimumWidth()+" "+wallpaperManager.getDesiredMinimumHeight());
wallpaperManager.setBitmap(wallpaper); }
}
catch (IOException e)
{
e.printStackTrace();
Toast.makeText(context,
"Wallpaper Not Set",
Toast.LENGTH_SHORT).show();}
}
I need help so that I can set my complete image as wallpaper
My image size is : 640*960
bitmap size is : 1960*2880
screen dimensions : 1080*1960

Related

Home screen wallpaper gets zoomed when it goes to the background

I have scheduled background service to change device home screen wallpaper periodically. The wallpaper changes successfully and they are perfectly set to the screen size.
But when I move away from the home screen and return the image gets zoomed in too much,leaving only a portion of the image visible.Below given is the code I use
Bitmap theBitmap = null;
try {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
theBitmap = Glide.with(mContext)
.load(PhotoPath)
.asBitmap().into(width, height).get();
WallpaperManager wm = WallpaperManager.getInstance(mContext);
wm.setWallpaperOffsetSteps(1, 1);
wm.suggestDesiredDimensions(width, height);
try {
wm.setBitmap(theBitmap);Album
} catch (IOException e) {
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
Try not to rush and flag this question as a duplicate. There are questions stating the image appears zoomed in when set but My wallpaper gets set perfectly,atleast initially it does. Why does it zoom in when I move homescreen to the background.

Get the size of the screen not using getMetrics

I'd like to get the size of the screen of the phones but it keeps giving me wrong values, I already used
WindowManager windowmanager = (WindowManager)
getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
Display display = windowmanager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);
float deviceWidth = displayMetrics.widthPixels;
float deviceHeight = displayMetrics.ydpi;
I tried this code too :
Resources resources = getResources();
Configuration config = resources.getConfiguration();
DisplayMetrics dm = resources.getDisplayMetrics();
// Note, screenHeightDp isn't reliable
// (it seems to be too small by the height of the status bar),
// but we assume screenWidthDp is reliable.
// Note also, dm.widthPixels,dm.heightPixels aren't reliably pixels
// (they get confused when in screen compatibility mode, it seems),
// but we assume their ratio is correct.
double screenWidthInPixels = (double)config.screenWidthDp *dm.density;
double screenHeightInPixels = screenWidthInPixels * dm.heightPixels / dm.widthPixels;
deviceWidth = (int)(screenWidthInPixels + .5);
deviceHeight = (int)(screenHeightInPixels + .5);
And also that :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Point realSize = new Point();
display.getRealSize(realSize);
deviceWidth= realSize.x;
deviceHeight = realSize.y;
}
But on my Samsung S7 on sdk 7.0 I got 1920x1080 that is wrong because on a S7 on sdk 6.0.1 I got 2560x1440 that is the real value.
I tried a lot of solution but found nothing good.
Thanks
Your code is correct. Just in case if you wondering why you get that values, it is because your phone will automatically set the default resolution size to 1920x1080 after updated to 7.0 (Nougat) to conserve the battery life. One of the new features in Nougat is display scaling option, where you can set your phone (in this case, S7) to 3 available modes (WQHD (2560x1440), FHD (1920x1080), and HD (1280x720)). Try go to Settings -> Display and change the settings to your needs. You can read more here: Galaxy S7 on Nougat defaults to 1080p
use this
getWindowManager().getDefaultDisplay().getHeight();
getWindowManager().getDefaultDisplay().getWidth();
This will work for sure.
try {
display.getRealSize(size);
height = size.y;
width=size.x;
} catch (NoSuchMethodError e) {
height = display.getHeight();
width=display.getWidth();
}
Try this link
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
The method below return a Point that contain display size (x as Width and y as Height):
public static Point checkDisplaySize(Context context) {
Point tempDisplaySize = new Point();
try {
WindowManager manager = (WindowManager) context().getSystemService(Context.WINDOW_SERVICE);
if (manager != null) {
Display display = manager.getDefaultDisplay();
if (display != null) {
display.getSize(tempDisplaySize);
Log.d("tmessages", "display size = " + displaySize.x + " " + displaySize.y);
}
}
} catch (Exception e) {
Log.e("tmessages", e.getMessage(), e);
}
return tempDisplaySize;
}
P.S: This is the Code Telegram uses to get display size.

Android Images are stretched when the phone is rotated

I am getting images using the Html.fromHtml() method. So I am using URLImageParser and URLDrawable classes as defined here: Android HTML ImageGetter as AsyncTask.
I have adapted two methods from URLImageParser in order to scale the image to the width of the screen whilst maintaining the aspect ratio. The changed methods are as follows:
#Override
protected void onPostExecute(Drawable result) {
DisplayMetrics dm = new DisplayMetrics();
((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * container.getHeight() / container.getWidth();
urlDrawable.setBounds(0, 0, 0+width, 0+height);
// change the reference of the current drawable to the result
// from the HTTP call
urlDrawable.drawable = result;
// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
// For ICS
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height));
// Pre ICS
URLImageParser.this.container.setEllipsize(null);
}
and
public Drawable fetchDrawable(String urlString) {
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
DisplayMetrics dm = new DisplayMetrics();
((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * container.getHeight() / container.getWidth();
drawable.setBounds(0, 0, 0 + width, 0
+ height);
return drawable;
} catch (Exception e) {
return null;
}
}
My problem is that when the phone is held in portrait orientation the image looks fine. However when I rotate the phone the width of the image goes to the new width of the phone, however the height does not change so the image looks distorted.
Is there a way to reset the height when the phone is rotated?
Pretty sure this is a silly mistake somewhere
To detect a screen rotation, you can try something like this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
See more.
Here is another great example.

Android : Issue with memory for large Bitmap objects

Lots been said, written, vented out ;) on this already, I know. But my question is a bit different. I realize that there is issue with using large bitmap objects in Android. In my Android App, I need to use large bitmaps in various places. cannot help it. I tried various things listed in stackoverflow. One that I want to try but not able to is to avoid using ARBG_8888. The App crashes exactly at that line many times. I have an ImageView where I load an initial large bitmap. Then the user can make some markings in the bitmap and then I need to save the whole image (i.e. the original bitmap + the user's markings). Am trying this with this code I got from stackoverflow:
public static Bitmap loadBitmapFromView(Context context, View view) {
Log.d(logtag, "loadBitmapFromView");
Bitmap bitmap;
BitmapFactory.Options options = new BitmapFactory.Options();
// These help in saving/recycling the bitmap memory
options.inPurgeable = true;
options.inInputShareable = true;
Bitmap dummy = null;
try {
dummy = BitmapFactory.decodeStream(context.getAssets().open("icon_add.png"), new Rect(-1,-1,-1,-1), options);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap = Bitmap.createBitmap(view.getLayoutParams().width,
view.getLayoutParams().height, Bitmap.Config.ARGB_8888);
// bitmap = Bitmap.createScaledBitmap( dummy, view.getLayoutParams().width,
// view.getLayoutParams().height, false);
Canvas c = new Canvas(bitmap);
view.layout(0, 0, view.getLayoutParams().width, view.getLayoutParams().height);
view.draw(c);
Log.d(logtag, "loadBitmapFromView: width:" + view.getLayoutParams().width);
c = null;
return bitmap;
}
How do I avoid using ARGB_8888 in the above code? Can someone please help?
I removed it from many other places in the code, but, in the above snippet, am left in vain.
I tried using an initial dummy object (you can see the object 'dummy' in the above code) to create a scaled bitmap object ( with createScaledBitmap() which necessarily asks for a source bitmap ) and then tried to load the canvas, but the generated image is having only the dummy object (icon_add.png) and not the one from the imageview.
you can show image in device specific height width....then you don't get out of memory for imagesize.
this is code:
#SuppressLint("NewApi")
public static int getDeviceWidth(Activity activity) {
int deviceWidth = 0;
Point size = new Point();
WindowManager windowManager = activity.getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
windowManager.getDefaultDisplay().getSize(size);
deviceWidth = size.x;
} else {
Display display = windowManager.getDefaultDisplay();
deviceWidth = display.getWidth();
}
return deviceWidth;
}
#SuppressLint("NewApi")
public static int getDeviceHeight(Activity activity) {
int deviceHeight = 0;
Point size = new Point();
WindowManager windowManager = activity.getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
windowManager.getDefaultDisplay().getSize(size);
deviceHeight = size.y;
} else {
Display display = windowManager.getDefaultDisplay();
deviceHeight = display.getHeight();
}
return deviceHeight;
}
you can use device height and width..and set this image in imageview..
ImageView image = new ImageView(viewHomeScreen);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT);
params.height = getDeviceHeight(activity);
params.width = getDevicewidth(activity);
FrameLayout framelayout = new FrameLayout(viewHomeScreen);
framelayout.addView(image, 0, params);
i hope if you use device specific height and width then your image memory issue will be solved.

Programatically set background (drawable) doesn't center

I wrote an app that lets people select an image (drawable) and after they hit the "set as background" button the selected image should appear centered as the background of my phone.
Following code used to work fine. The image was placed and centered. But for operating systems (> api 13) it is depreciated. Does anyone know how to fix so that the image is also centered (or scaled to fit the screen if you wish)?
I've tried replacing it with "displaymetrics" or "points" to get w or h but it doesn't help centering the image. Is it the "suggestDesiredDimensions" that doesn't do it anymore?
ResID is the identifier of the needed drawable.
Anyone knows how to handle this? Thanks!
public void setBackground(View v)
{
try {
// Set background from a resource
WallpaperManager.getInstance(this).setResource(resID);
WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth(); // deprecated
int h = display.getHeight(); //deprecated
wm.suggestDesiredDimensions(w, h);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Try using display.getSize()
public void setBackground(View v){
try {
// Set background from a resource
WallpaperManager.getInstance(this).setResource(resID);
WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int w = size.x;
int h = size.y;
wm.suggestDesiredDimensions(w, h);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Categories

Resources