Skip to content Skip to sidebar Skip to footer

Set Wallpaper With Bitmap Avoid Crop And Set Fit Center

I'm trying to make a Wallpaper Application. And I had big trouble during set wallpaper with bitmap. I try to figure out answer for a week. I want to set Bitmap in to wallpaper like

Solution 1:

If you want bitmap crop from centre and same screen resolution then use below method. Here Return bitmap is same your screen resolution and crop.

For Ex. Your Bitmap size 480x820

Your device size 480x800 its Return 480x800 (top 10 pixel and bottom 10 pixel remove after scale).

Your device size 800x1280 its Return 800x1280 (top 43 pixel and bottom 43 pixel remove after scale).

private Bitmap cropBitmapFromCenterAndScreenSize(Bitmap bitmap) {
    float screenWidth, screenHeight;
    floatbitmap_width= bitmap.getWidth(), bitmap_height = bitmap
            .getHeight();
    Displaydisplay= ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay();
    screenWidth = display.getWidth();
    screenHeight = display.getHeight();

    Log.i("TAG", "bitmap_width " + bitmap_width);
    Log.i("TAG", "bitmap_height " + bitmap_height);

    floatbitmap_ratio= (float) (bitmap_width / bitmap_height);
    floatscreen_ratio= (float) (screenWidth / screenHeight);
    int bitmapNewWidth, bitmapNewHeight;

    Log.i("TAG", "bitmap_ratio " + bitmap_ratio);
    Log.i("TAG", "screen_ratio " + screen_ratio);

    if (screen_ratio > bitmap_ratio) {
        bitmapNewWidth = (int) screenWidth;
        bitmapNewHeight = (int) (bitmapNewWidth / bitmap_ratio);
    } else {
        bitmapNewHeight = (int) screenHeight;
        bitmapNewWidth = (int) (bitmapNewHeight * bitmap_ratio);
    }

    bitmap = Bitmap.createScaledBitmap(bitmap, bitmapNewWidth,
            bitmapNewHeight, true);

    Log.i("TAG", "screenWidth " + screenWidth);
    Log.i("TAG", "screenHeight " + screenHeight);
    Log.i("TAG", "bitmapNewWidth " + bitmapNewWidth);
    Log.i("TAG", "bitmapNewHeight " + bitmapNewHeight);

    int bitmapGapX, bitmapGapY;
    bitmapGapX = (int) ((bitmapNewWidth - screenWidth) / 2.0f);
    bitmapGapY = (int) ((bitmapNewHeight - screenHeight) / 2.0f);

    Log.i("TAG", "bitmapGapX " + bitmapGapX);
    Log.i("TAG", "bitmapGapY " + bitmapGapY);

    bitmap = Bitmap.createBitmap(bitmap, bitmapGapX, bitmapGapY,
            screenWidth,screenHeight);
    return bitmap;
}

Post a Comment for "Set Wallpaper With Bitmap Avoid Crop And Set Fit Center"