Wallpaper not properly fit on device screen

后端 未结 5 508
我寻月下人不归
我寻月下人不归 2020-11-29 08:07

I have one set of images in one drawable folder. I have one button to set image as wallpaper on device screen. But when I set this image as wallpaper its either zoom or crop

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 08:23

    Hello This is working with drawable image i have checked it..

                    DisplayMetrics metrics = new DisplayMetrics(); 
                    getWindowManager().getDefaultDisplay().getMetrics(metrics);
                    int height = metrics.heightPixels; 
                    int width = metrics.widthPixels;
                    Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), R.drawable.img);
                    Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
                    WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this); 
                    wallpaperManager.setWallpaperOffsetSteps(1, 1);
                    wallpaperManager.suggestDesiredDimensions(width, height);
                    try {
                      wallpaperManager.setBitmap(bitmap);
                      } catch (IOException e) {
                      e.printStackTrace();
                    }
    

    Also mention these permissions in Manifest.xml..

        
        
    

    This is screenshot..

    enter image description here

    For reset Fit wallpaper of screen store the image path in shared preferences and use Boot Completed Receiver then reset the same wallpaper on the screen....

    The broadcast receiver is..

    import java.io.IOException;
    
    import android.app.WallpaperManager;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.util.DisplayMetrics;
    import android.util.Log;
    import android.view.WindowManager;
    
    public class BootReceiver extends BroadcastReceiver {
    private static final String TAG="BootReceiver";
    
    @Override public void onReceive(Context context,Intent intent){
        try{
                DisplayMetrics metrics = new DisplayMetrics(); 
                WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                windowManager.getDefaultDisplay().getMetrics(metrics);
                int height = metrics.heightPixels; 
                int width = metrics.widthPixels;
                Bitmap tempbitMap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img);
                Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
                WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); 
                wallpaperManager.setWallpaperOffsetSteps(1, 1);
                wallpaperManager.suggestDesiredDimensions(width, height);
                try {
                  wallpaperManager.setBitmap(bitmap);
                  } catch (IOException e) {
                  e.printStackTrace();
                }
        }catch(Exception e){
            Log.e(TAG,e.toString());
        }
    }
    }
    

    After Add these lines in Manifest.xml

           
                
                    
                    
                
            
    

提交回复
热议问题