crop bitmap in screen size from custom width

459 Views Asked by At

I have an big ImageView that loads an Image from Url and I puts it in a ScrollView.

I get bitmap from image view by this code :

Bitmap bitmapOrg =((BitmapDrawable) singleWallpaper.getDrawable()).getBitmap();

Now I want to crop from position of ScrollView (ScrollX) to size of the screen and put them to new bitmap (What shows in just in display not others)

2

There are 2 best solutions below

1
On
  1. First get the screen size by

    DisplayMetrics display=Context.getResources().getDisplayMetrics();

  2. Now Crop the Bitmap

Bitmap.createScaledBitmap(Bitmap src, display.widthPixels, display.heightPixels, true);

0
On

Find the Phone Screen Height and Width by

DisplayMetrics display=Context.getResources().getDisplayMetrics();
int width=display.widthPixels;
int height=display.heightPixels;

Then Scale your Bitmap using

Bitmap bitmap=Bitmap.createScaledBitmap(Bitmap src, display.widthPixels,display.heightPixels, true);

Now use this Bitmap

try {               
    getApplicationContext().setWallpaper(bitmap);
    Toast.makeText(this, "Wallpaper Set!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
    e.printStackTrace();
}

After doing this all thing do not forget to give permission in AndroidManifest.xml

<uses-permission android:name="android.permission.SET_WALLPAPER" />