Android set wallpaper of home screen with centering the image -


i wrote simple application sets wallpaper on device. can't achieve 1 effect. wish picture automatically centrated horizontally. means center of image on central desktop of luncher app. picture @ bottom shows how looks now: enter image description here

effect want achieve:

enter image description here

and image itself:

enter image description here

i tried use code this question, however, did not achieve desired effect.

my code:

public class systemwallpaperhelper { private context context; private imageloader imageloader; private displayimageoptions imageloaderoptions;  public systemwallpaperhelper(context context){     this.context = context;     setimageloaderoptions(); }  private void setimageloaderoptions() {     final int width = sharedhelper.getdevicewidth(context) << 1 ; // best wallpaper width twice screen width     imageloaderoptions = new displayimageoptions.builder()             .imagescaletype(imagescaletype.none)             .cacheinmemory(false)             .cacheondisk(false)             .postprocessor(new bitmapprocessor() {                 @override                 public bitmap process(bitmap bmp) {                     float scale =  (float) width / bmp.getwidth() ;                     int height = (int) (scale * bmp.getheight());                     return bitmap.createscaledbitmap(bmp, width, height, false);                 }             })             .build();     imageloader = imageloader.getinstance(); }  public void setdevicewallpaper(wallpaper wallpaper){     imageloader.loadimage(wallpaper.getsrcurl(), imageloaderoptions, new simpleimageloadinglistener(){         @override         public void onloadingcomplete(string imageuri, view view, bitmap loadedimage)         {             wallpapermanager wallpapermanager = wallpapermanager.getinstance(context);             try {                 wallpapermanager.setbitmap(loadedimage);             } catch (ioexception e) {                 e.printstacktrace();             }         }     }); } } 

after several attempts, managed achieve desired effect.

public class systemwallpaperhelper { private context context; private imageloader imageloader; private displayimageoptions imageloaderoptions; private wallpapermanager wallpapermanager;  public systemwallpaperhelper(context context) {     this.context = context;     setimageloaderoptions();     wallpapermanager = wallpapermanager.getinstance(context); }  private void setimageloaderoptions() {     imageloaderoptions = new displayimageoptions.builder()             .imagescaletype(imagescaletype.none)             .cacheinmemory(false)             .cacheondisk(false)             .postprocessor(new bitmapprocessor() {                 @override                 public bitmap process(bitmap bmp) {                 return centercropwallpaper(bmp, wallpapermanager.getdesiredminimumwidth(), wallpapermanager.getdesiredminimumheight());                 }             })             .build();     imageloader = imageloader.getinstance(); }  private bitmap centercropwallpaper(bitmap wallpaper, int desiredwidth, int desiredheight){     float scale = (float) desiredheight / wallpaper.getheight();     int scaledwidth = (int) (scale * wallpaper.getwidth());     int devicewidth = sharedhelper.getdevicewidth(context);     int imagecenterwidth = scaledwidth /2;     int widthtocut = imagecenterwidth - devicewidth / 2;     int leftwidth = scaledwidth - widthtocut;     bitmap scaledwallpaper = bitmap.createscaledbitmap(wallpaper, scaledwidth, desiredheight, false);     bitmap croppedwallpaper = bitmap.createbitmap(         scaledwallpaper,         widthtocut,         0,         leftwidth,         desiredheight     );     return croppedwallpaper; }  public void setdevicewallpaper(final wallpaper wallpaper, final boolean adjusted) {     imageloader.loadimage(wallpaper.getsrcurl(), imageloaderoptions, new simpleimageloadinglistener() {         @targetapi(build.version_codes.kitkat)         @override         public void onloadingcomplete(string imageuri, view view, bitmap loadedimage) {             if (adjusted) {                 wallpapermanager.getcropandsetwallpaperintent(sharedhelper.getimageuriforbitmap(context, loadedimage));             } else {                 try {                     int width = wallpapermanager.getdesiredminimumwidth();                     int height = wallpapermanager.getdesiredminimumheight();                     int bitwidth = loadedimage.getwidth();                     wallpapermanager.setbitmap(loadedimage);                 } catch (ioexception e) {                     e.printstacktrace();                 }             }         }     }); } } 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -