Android设置主屏幕的壁纸与图像居中

时间:2015-06-29 14:49:55

标签: android android-image universal-image-loader android-bitmap android-wallpaper

我写了一个简单的应用程序来设置设备上的壁纸。我无法达到一个效果。我希望图片自动水平对中。这意味着图像的中心位于Luncher应用程序的最中央桌面上。 底部的图片显示了它现在的样子: enter image description here

我想要实现的效果:

enter image description here

图像本身:

enter image description here

我尝试使用this question中的代码,但是没有达到预期的效果。

我的代码:

public class SystemWallpaperHelper {
private Context context;
private ImageLoader imageLoader;
private DisplayImageOptions imageLoaderOptions;

public SystemWallpaperHelper(Context context){
    this.context = context;
    setImageLoaderOptions();
}

private void setImageLoaderOptions() {
    final int width = SharedHelper.getDeviceWidth(context) << 1 ; // best wallpaper width is twice screen width
    imageLoaderOptions = new DisplayImageOptions.Builder()
            .imageScaleType(ImageScaleType.NONE)
            .cacheInMemory(false)
            .cacheOnDisk(false)
            .postProcessor(new BitmapProcessor() {
                @Override
                public Bitmap process(Bitmap bmp) {
                    float scale =  (float) width / bmp.getWidth() ;
                    int height = (int) (scale * bmp.getHeight());
                    return Bitmap.createScaledBitmap(bmp, width, height, false);
                }
            })
            .build();
    imageLoader = ImageLoader.getInstance();
}

public void setDeviceWallpaper(Wallpaper wallpaper){
    imageLoader.loadImage(wallpaper.getSrcUrl(), imageLoaderOptions, new SimpleImageLoadingListener(){
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
        {
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
            try {
                wallpaperManager.setBitmap(loadedImage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}
}

2 个答案:

答案 0 :(得分:2)

经过多次尝试,我设法达到了预期的效果。

public class SystemWallpaperHelper {
private Context context;
private ImageLoader imageLoader;
private DisplayImageOptions imageLoaderOptions;
private WallpaperManager wallpaperManager;

public SystemWallpaperHelper(Context context) {
    this.context = context;
    setImageLoaderOptions();
    wallpaperManager = WallpaperManager.getInstance(context);
}

private void setImageLoaderOptions() {
    imageLoaderOptions = new DisplayImageOptions.Builder()
            .imageScaleType(ImageScaleType.NONE)
            .cacheInMemory(false)
            .cacheOnDisk(false)
            .postProcessor(new BitmapProcessor() {
                @Override
                public Bitmap process(Bitmap bmp) {
                return centerCropWallpaper(bmp, wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight());
                }
            })
            .build();
    imageLoader = ImageLoader.getInstance();
}

private Bitmap centerCropWallpaper(Bitmap wallpaper, int desiredWidth, int desiredHeight){
    float scale = (float) desiredHeight / wallpaper.getHeight();
    int scaledWidth = (int) (scale * wallpaper.getWidth());
    int deviceWidth = SharedHelper.getDeviceWidth(context);
    int imageCenterWidth = scaledWidth /2;
    int widthToCut = imageCenterWidth - deviceWidth / 2;
    int leftWidth = scaledWidth - widthToCut;
    Bitmap scaledWallpaper = Bitmap.createScaledBitmap(wallpaper, scaledWidth, desiredHeight, false);
    Bitmap croppedWallpaper = Bitmap.createBitmap(
        scaledWallpaper,
        widthToCut,
        0,
        leftWidth,
        desiredHeight
    );
    return croppedWallpaper;
}

public void setDeviceWallpaper(final Wallpaper wallpaper, final boolean adjusted) {
    imageLoader.loadImage(wallpaper.getSrcUrl(), imageLoaderOptions, new SimpleImageLoadingListener() {
        @TargetApi(Build.VERSION_CODES.KITKAT)
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (adjusted) {
                wallpaperManager.getCropAndSetWallpaperIntent(SharedHelper.getImageUriForBitmap(context, loadedImage));
            } else {
                try {
                    int width = wallpaperManager.getDesiredMinimumWidth();
                    int height = wallpaperManager.getDesiredMinimumHeight();
                    int bitWidth = loadedImage.getWidth();
                    wallpaperManager.setBitmap(loadedImage);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}
}

答案 1 :(得分:0)

这是一个更通用的版本,可以粘贴在任何java android类中。另外,它不依赖于显示方向。

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);
}