Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please some one help me to add bitmap center crop #151

Open
Itscrazyshivam opened this issue Jul 1, 2021 · 0 comments
Open

Please some one help me to add bitmap center crop #151

Itscrazyshivam opened this issue Jul 1, 2021 · 0 comments

Comments

@Itscrazyshivam
Copy link

Hello,
i am trying to add center crop in my code but i get issue please help me to solve this.

here is my current codes,
//set wallpaper on Home Screen
private void setWallpaperToHomeScreen() {
relativeLayoutLoadMore.setVisibility(View.VISIBLE);
Glide.with(this)
.asBitmap()
.load(Config.ADMIN_PANEL_URL+"/images/"+arrayList.get(currentPosition).getImage_url())
.into(new CustomTarget() {
@RequiresApi(api = Build.VERSION_CODES.N)
@OverRide
public void onResourceReady(@nonnull Bitmap resource, @nullable Transition<? super Bitmap> transition) {

                    WallpaperManager wallpaperManager = WallpaperManager.getInstance(WallpaperDetailsActivity.this);
                    try {

                        DisplayMetrics metrics = new DisplayMetrics();
                        getWindowManager().getDefaultDisplay().getMetrics(metrics);
                        int height = metrics.heightPixels;
                        int width = metrics.widthPixels;
                        Bitmap bitmap = Bitmap.createScaledBitmap(resource,width,height, true);
                        wallpaperManager.getDesiredMinimumWidth();//returned 1280 on s3
                        wallpaperManager.getDesiredMinimumHeight();//also returned 1280 on s3
                        wallpaperManager.suggestDesiredDimensions(width, height);
                        wallpaperManager.setBitmap(bitmap,null, true, WallpaperManager.FLAG_SYSTEM);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Snackbar.make(rootLayout,"Wallpaper was set successfully",Snackbar.LENGTH_LONG).show();
                                relativeLayoutLoadMore.setVisibility(View.GONE);
                                showFullScreenAds();
                            }
                        }, Constant.DELAY_SET_WALLPAPER);
                    } catch (IOException e) {
                        e.printStackTrace();
                        relativeLayoutLoadMore.setVisibility(View.GONE);
                    }

                }
                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }
    });
}

Code that i want add in my app
public static void setWallpaper(Context context, BitmapDrawable wallpaper) {

try {

        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);

                if(wallpaper != null) {

                            Bitmap bmp = wallpaper.getBitmap();

                                        DisplayMetrics metrics = new DisplayMetrics();

                                                    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

                                                                windowManager.getDefaultDisplay().getMetrics(metrics);

                                                                            int height = metrics.heightPixels;

                                                                                        int width = metrics.widthPixels;

                                                                                                    wallpaperManager.setWallpaperOffsetSteps(1, 1);

                                                                                                                wallpaperManager.suggestDesiredDimensions(width, height);

                                                                                                                            Bitmap bitmap = centerCropWallpaper(context, bmp, Math.min(wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight()));

                                                                                                                                        wallpaperManager.setBitmap(bitmap);

                                                                                                                                                } else {

                                                                                                                                                            Log.e(TAG, "wallpaper could not be set.");

                                                                                                                                                                    }

                                                                                                                                                                        } catch (Exception ex) {

                                                                                                                                                                                Log.e(TAG, "error setting wallpaper. " + ex.getMessage(), ex);

                                                                                                                                                                                    }

                                                                                                                                                                                    }

private static Bitmap centerCropWallpaper(Context context, Bitmap wallpaper, int desiredHeight){

float scale = (float) desiredHeight / wallpaper.getHeight();

    int scaledWidth = (int) (scale * wallpaper.getWidth());

        DisplayMetrics metrics = new DisplayMetrics();

            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

                windowManager.getDefaultDisplay().getMetrics(metrics);

                    int deviceWidth = metrics.widthPixels;

                        int imageCenterWidth = scaledWidth /2;

                            int widthToCut = imageCenterWidth - deviceWidth / 2;

                                int leftWidth = scaledWidth - widthToCut;

                                    Bitmap scaledWallpaper = Bitmap.createScaledBitmap(wallpaper, scaledWidth, desiredHeight, false);

                                        return Bitmap.createBitmap(scaledWallpaper, widthToCut, 0, leftWidth, desiredHeight);

                                        }

please help me to add this my app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant