0

I am developing an app, in which the wallpaper image of the device should change at fixed intervals. I have kept the images in the drawable folder. I am using a WallpaperManager to set these images as the wallpaper.

To ensure that the app works well on various devices, I have kept multiple copies of different sized images in various folders like drawable-small,drawable-xlarge folders in the res directory. However, these do not scale properly on every device.

My question is how do I ensure that the wallpaper images fit nicely on every device. Will I have to do it programmatically? Any sample code would be of immense help. Thanks.

This is the code I have used

Drawable drawable;
WallpaperManager wpm;

@Override
    public void onCreate() {
        super.onCreate();
        wpm=WallpaperManager.getInstance(WallAlarm.this);
        drawable = getResources().getDrawable(R.drawable.two);
    }

     Bitmap wallpaper=((BitmapDrawable)drawable).getBitmap();
     ImageView iv=new ImageView(this);
     iv.setImageDrawable(drawable);
     iv.setScaleType(ScaleType.FIT_XY);

     try {
            wpm.setBitmap(wallpaper);
     } catch (IOException e) {
            e.printStackTrace();
     }

}

}
ambit
  • 1,099
  • 2
  • 28
  • 43

2 Answers2

0

Try using something like DisplayMetrics, and scale/crop the image according to the display:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

metrics.heightPixels;
metrics.widthPixels;

or use or getSize():

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
RobinF
  • 349
  • 1
  • 2
  • 17
0

you can use

Display display = Activity.getWindowManager().GetDefaultDisplay();
display.getHeight();
display.getWidth();

to choose the image according to the ratio of the screen.

Or use "scaleType option"

Alix Bloom
  • 217
  • 1
  • 8
  • Thanks..I am trying to use the display.getHeight(). But somehow, the images keep getting cropped on smaller devices. Also, am not sure how to use scaleType option for a wallpaper image through a drawable image file. Kindly help. – ambit Mar 30 '12 at 11:42
  • Your wallpaper image is an ImageView? so you can do something like that : iv.setImageBitmap(bitmap); (or iv.setImageDrawable) iv.setScaleType(ScaleType.FIT_CENTER); – Alix Bloom Mar 30 '12 at 12:22
  • Thanks again. I tried using an imageview in my code. But I am still not sure how would I associate an imageview with my wallpaper. I have edited my original question and put in some code. Kindly go through it and help me if possible. Sorry, am a bit new with Android and so am a bit confused. – ambit Mar 30 '12 at 13:45