Search code examples
androidsizewallpaper

Wallpaper fit image to screen size


I spent hours on an issue that doesn't seem complicated, but despite all the existing messages on this topic, I still don't find a solution...

I'm using a viewpager and in the adapter I have a button and a imageview. I want to set the picture from the imageview as wallpaper when the button is clicked by using myWallpaperManager.setBitmap(image). Before that I resize the picture to fit in screen size. The wallpaper is changed but the picture stays zoomed and cropped. I would like to set the wallpaper with the whole picture. I have the same values for width and height of getDefaultDisplay() and width and height of myWallpaperManager.getDesiredMinimumHeight/Width().

I tried many different ways that didn't work. I don't know what else to do ! Thanks for a lot for your answer.

Here is the code :

 Button ButWallpaper = (Button) viewLayout.findViewById(R.id.ButWallpaper);

      ButWallpaper.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
               Bitmap image;
               Bitmap imagelarge = ((BitmapDrawable)imageView2.getDrawable()).getBitmap();

              int Measuredwidth = 0;  
              int Measuredheight = 0;  
              Point size = new Point();
              WindowManager w = _activity.getWindowManager();

             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)    {
                 w.getDefaultDisplay().getSize(size);
                 Measuredwidth = size.x;
                 Measuredheight = size.y; 
             }else{
                 Display d = w.getDefaultDisplay(); 
                 Measuredwidth = d.getWidth(); 
                 Measuredheight = d.getHeight(); 
             }

             Log.i("TAG", String.valueOf(Measuredwidth));
             Log.i("TAG", String.valueOf(Measuredheight));

            image =  Bitmap.createScaledBitmap(imagelarge, Measuredwidth, Measuredheight,true);


               // TODO Auto-generated method stub
            WallpaperManager myWallpaperManager 
            = WallpaperManager.getInstance(_activity.getApplicationContext());
            try {
                myWallpaperManager.setBitmap(image);
                 myWallpaperManager.suggestDesiredDimensions(Measuredwidth, Measuredheight);

                 Log.i("TAG", String.valueOf(myWallpaperManager.getDesiredMinimumHeight()));
                 Log.i("TAG", String.valueOf(myWallpaperManager.getDesiredMinimumWidth()));

                 Toast.makeText(_activity.getApplicationContext(), "Wallpaper changed", toast.LENGTH_LONG).show();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }});

Solution

  • See if this helps:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int height = metrics.heightPixels;
    int width = metrics.widthPixels; 
    Bitmap bitmap = Bitmap.createScaledBitmap(yourimagebitmap, width, height, true);
    WallpaperManager.getInstance(MyActivity.this).setBitmap(bitmap);  
    

    Source: Android setting wallpaper gone wrong