Setting Wallpaper

by Neilz » Sat, 18 Dec 2010 23:25:02 GMT


Sponsored Links
 Hi all, I've asked this before but never got a useful answer.

I'm setting wallpaper as follows:

InputStream stream = resources.openRawResource(myResource);

getApplicationContext().setWallpaper(stream);

It works, but the image doesn't fit the screen. Is there a way to
force the image to fit the screen, or is this down to differences in
device types? Are wallpapers specific to each advice depending upon
the resolution and screen dimensions? Am I wasting my time trying to
make a single image available for the wallpaper?

-- 



Re: Setting Wallpaper

by String » Sat, 18 Dec 2010 23:32:28 GMT


 It's up to you to resize the image as you see fit. Get the device's 
preferred wallpaper dimensions with code like this:

final WrapWallpaperManager wallpaperMgr = new WrapWallpaperManager(context);
    int wallWidth  = wallpaperMgr.getDesiredMinimumWidth();
int wallHeight = wallpaperMgr.getDesiredMinimumHeight();

Then use a Bitmap or two to do the resizing. You'll load the resource into 
one of them, rather than direct into the wallpaper.

String

-- 


Sponsored Links


Re: Setting Wallpaper

by Neilz » Sat, 18 Dec 2010 23:36:17 GMT


 Many thanks, I'll give that a go.




-- 



Re: Setting Wallpaper

by Neilz » Sun, 19 Dec 2010 01:46:06 GMT


 I'm still having exactly the same problem as I always had.

It doesn't matter if I resize the image, when I set it as the
wallpaper, it doesn't fit the screen.

The problem is this... android has a number of home screens, right? So
you can scroll from one screen to another. My Nexus has five screens,
my Hero has 7, and the Samsung I'm looking at now has 3. So the OS
tries to stretch the image to fit the entire backdrop which contains
all these home screens.

I need to fix the image, regardless of whether the user is scrolling
between screens.





-- 



Re: Setting Wallpaper

by String » Sun, 19 Dec 2010 01:56:03 GMT


 Then you need to use a live wallpaper, which is a LOT more than 2 lines of 
code. AFAIK, there's no way to stop Launcher from scrollscrolling the wallpaper 
as the user moves between home screen panes. 

String

-- 



Re: Setting Wallpaper

by Neilz » Sun, 19 Dec 2010 02:04:52 GMT


 In fact it doesn't matter what size I resize the bitmap to, it always
tries to stretch it to waaay bigger than the size of the screen so you
only get to see a small part of the image. And that's the same for
three different devices.

I've been playing around with WallpaperManager too, which is since
level 5 (or 7 for many of the methods) and I can't get this to do
anything different to the original
getApplicationContext().setWallpaper() method.

Live Wallpapers... I guess I'd better to look into that. Just seems
strange that this isn't simple...




-- 



Re: Re: Setting Wallpaper

by Dianne Hackborn » Sun, 19 Dec 2010 03:04:27 GMT


 You don't need a live wallpaper.

Just set it to a bitmap that is the expected size, so it doesn't need to
adjust it to fit.  The size can be retrieved from the wallpaper manager.

This is the exact same API that things like Gallery use to set the
wallpaper.  You can look in the code for that app if you want a working
example.







-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 



Re: Setting Wallpaper

by String » Sun, 19 Dec 2010 06:48:16 GMT


 Don't know what to tell you, man, but Context.setWallpaper does work, and 
doesn't resize bitmaps that are the requested size to start with. The code I 
posted earlier was directly from one of my apps. 

However, I do stand by what I said about multiple home screen panes. I don't 
think there's a way around that one without live wallpapers. 

String 

-- 



Re: Re: Setting Wallpaper

by Dianne Hackborn » Sun, 19 Dec 2010 06:53:05 GMT


 Oh yeah if you want to *prevent* scrolling, you'll need to do a live
(unlive) wallpaper.

For a static bitmap image, it's pretty easy, but more than 2 lines of code.







-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 



Re: Setting Wallpaper

by Neilz » Sun, 19 Dec 2010 17:17:08 GMT


 Ok, I think the problem I have here is not my code after all...

I tried simply setting the wallpaper from the phone options, picking
an image from the gallery. Exactly the same thing happens as when I do
it from the code... it picks a central part of the image and stretches
it.

I think the problem seems to be that it wants a rectangular image
(wider than it is high) to fit the multiple desktops. But the images
that I have in my app are all tall. But, if it wants an image that
size, then why does the WallpaperManager suggest an image size which
is basically the same results as the DisplayMetrics dimensions? (i.e.
240 * 320, 320 * 480 etc). That clearly isn't the size that it wants.

Seriously, try setting a wallpaper from your own image gallery, and
you'll see what I mean.

-- 



Re: Re: Setting Wallpaper

by Dianne Hackborn » Mon, 20 Dec 2010 03:45:53 GMT


 What phone are you doing this on?  It works fine on all of my phones.

Also note the desired image size is based on what the current launcher app
expects.  Something else could be installed that sets it to something
different.  The platform just reports whatever was last requested by an app.







-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 



Re: Re: Setting Wallpaper

by Dianne Hackborn » Mon, 20 Dec 2010 03:48:49 GMT


 Oh and in the standard launcher app, the requested size will be slightly
wider than the screen in landscape, and the same height as the screen in
portrait.  This is to ensure the image is large enough to fill a portrait
screen, and still be able to provide a bit of parallax on a landscape screen
(in which case the image will be cropped vertically).

Actually on some devices without a keyboard it may just ask for an image the
same size as the display in landscape, because the standard launcher doesn't
run in landscape mode in that case.







-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 



Re: Re: Setting Wallpaper

by Dianne Hackborn » Mon, 20 Dec 2010 03:51:24 GMT


 ctually ignore that last part, the wallpaper still needs to be the height
of the screen in portrait. Duh. :p You should see in the gallery wallpaper
cropping UI that the size it wants is slightly longer than tall, but not as
much as a WVGA screen. At least, again, for the standard platform running
the standard launcher.

On Sun, Dec 19, 2010 at 11:48 AM, Dianne Hackborn <hack...@android.com>wrote:



--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails. All such
questions should be posted on public forums, where I and others can see and
answer them.

--



Re: Setting Wallpaper

by Neilz » Mon, 20 Dec 2010 06:13:46 GMT


 ianne, I'm using three devices to play with this... Nexus, Hero,
Europa.

Basically I've spent way too many hours trying to get this to work
programmatically with a screen sized (320 * 480) image. It clearly
isn't practical to set wallpaper with an image of those dimensions...
I'm just going to have to tell my client "No..."!

On Dec 19, 7:50pm, Dianne Hackborn <hack...@android.com> wrote:

--



Re: Setting Wallpaper

by String » Mon, 20 Dec 2010 06:35:33 GMT


 I've tested this extensively on my G1 and N1 (not to mention innumerable 
emulator instances), and the stock Launcher asks for an image 
2*portrait-height x 1*portrait-height. So on HVGA it's a 640x320, on N1-spec 
WVGA it's 960x480, etc. If you give it any other size, it will be scaled to 
fit those dimensions, as you've found. Beyond my own handsets, this is also 
my experience from a couple of apps deployed to a good few thousand users; 
the only notable deviations from those guidelines that I've seen are 
Samsung's old TouchWiz UI (which used different images for different panes) 
and Garmin's Nuvifone (which wouldn't report dimensions at all).

Again, if you DON'T want the image to stretch across the multiple homescreen 
panes with the parallax effect, you'll need to go the live wallpaper route. 
I think we're all in agreement here that it can't be done with 
Context.setWallpaper().

String

--