static wallpaper

sdixon

Member
Licensed User
Longtime User
I've looked all over the forum and cannot find an answer to this problem. I am writing a small app to place images as wallpaper on various phones and devices, nothing live and I am having a problem scaling the image when it is set as wallpaper.

Code: Copy
Sub SetWallPaper(Bmp As Bitmap)
  Dim r As Reflector
  Dim intHeight As Int
  Dim intWidth As Int
  Dim intOffset As Double
  Dim pScale As Double
  Dim aLayOut As LayoutValues
   
  aLayOut = GetDeviceLayoutValues
  pScale = GetDeviceLayoutValues.Scale

  ' calculate the height of the device in relation to the bitmap
  intOffset = (aLayOut.Height / Bmp.Height) * 0.8
  ' calculate the new height for the bitmap
  intHeight = Bmp.Height * intOffset * pScale
  ' calculate the new width for the bitmap
  intWidth = Bmp.Width * intOffset * pScale
  Bmp = f.CreateScaledBitmap(Bmp, intWidth, intHeight, True)
   
  r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", Array As Object(r.GetContext), Array As String("android.content.Context"))
  r.RunMethod4("setBitmap", Array As Object(Bmp), Array As String("android.graphics.Bitmap"))
  Vibrate.Vibrate (50) ' Vibrate phone for 50 ms
  ToastMessageShow(wpWallpaperSet, False)
End Sub

This works fine on my phone (Samsung DUOS) (320x480) but in the emulator (400 x 800 mdpi) the image is not properly scaled (height and width) when it is set as wallpaper. However, the calculations above do return the size that I want the image to be.

Has someone had a similar experience and how did you solve it?

Thanks
 
Last edited:
Thanks for the reply.

I'm not trying to add static wallpaper to live wallpaper. I'm just trying to set the wallpaper of the device.

Image 1 is the emulator.
Image 2 is the app (image is displayed correctly)
Image 3 is the wallpaper after it's been set by the app.

from the logs
Code: Copy
LogCat connected to: emulator-5556
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
bitmap height: 1251
bitmap width: 720
new bitmap height: 640.0
new bitmap width: 368.3453237410072

Thanks again

Emile
 

Attachments

  • emulator.png
    emulator.png
    93.1 KB · Views: 838
  • app_1.png
    app_1.png
    89 KB · Views: 792
  • app_2.png
    app_2.png
    91.2 KB · Views: 764
Upvote 0
Well i also tried the same method but i think the solution is creating an image of needed height width ratio & then writing image.
 
Upvote 0
The zip file was too large to upload to the forum so I sent a private message with the link to download it from my site.

Thanks
 
Upvote 0
This code seems to work:
Code: Copy
Sub SetWallPaper(Bmp As Bitmap)
   Dim r As Reflector
   Bmp = ResizeImage(Bmp, GetDeviceLayoutValues.Width * 2, GetDeviceLayoutValues.Height)
   r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", Array As Object(r.GetContext), Array As String("android.content.Context"))
   r.RunMethod4("setBitmap", Array As Object(Bmp), Array As String("android.graphics.Bitmap"))
   Vibrate.Vibrate (50) ' Vibrate phone for 50 ms
   ToastMessageShow(wpWallpaperSet, False)
End Sub

The default width of a wallpaper is twice the screen width.
 
Upvote 0
I guess I'm not being absolutely clear as to what I want to do.

I want to place an image as wallpaper only as the height and width of the first screen. I has to stay static (not move) when the user scrolls to another page.

Code: Copy
Sub SetWallPaper(Bmp As Bitmap)
    Dim r As Reflector
    Bmp = ResizeImage(Bmp, GetDeviceLayoutValues.Width, GetDeviceLayoutValues.Height)
    r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", Array As Object(r.GetContext), Array As String("android.content.Context"))
    r.RunMethod4("setBitmap", Array As Object(Bmp), Array As String("android.graphics.Bitmap"))
    Vibrate.Vibrate (50) ' Vibrate phone for 50 ms
    ToastMessageShow(wpWallpaperSet, False)
End Sub

This should work and it does on my phone which is

Code: Copy
from the logs

LogCat connected to: B4A-Bridge: samsung GT-S6102-352086052612598

bitmap height: 1402
bitmap width: 807
scale: 0.75
device height: 320
device width: 240
new bitmap height: 320
new bitmap width: 184
resized bitmap height: 320
resized bitmap width: 184

the wallpaper fits on my phone which have the above dimensions (device height and width) looks like picture 2 above

but on the emulator

Code: Copy
LogCat connected to: emulator-5560

bitmap height: 1402
bitmap width: 807
scale: 1.0
device height: 800
device width: 480
new bitmap height: 800
new bitmap width: 460
resized bitmap height: 800
resized bitmap width: 460

The device height and width are correct, but the end result looks like picture 3 above
 

Attachments

Last edited:
Upvote 0
Thanks Erel, I'll look into implementing it as a live wallpaper.
 
Upvote 0
Back
Top