Android 设置主屏幕壁纸并将图像居中

标签 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();
            }
        }
    });
}
}

最佳答案

经过多次尝试,终于达到了预期的效果。

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

关于Android 设置主屏幕壁纸并将图像居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31118799/

相关文章:

android - 设置 Intent 返回的最大项目数

Android - 在静态类中使用通用图像加载器

android - UIL 加载精确图像

android - 图像加载器不工作

Android:如何删除sdcard中的文件夹

android - 为 Android 编码视频

android - 如何在 fragment 销毁方法上关闭 ListView 的上下文操作模式

Android从图像列表制作动画视频

java - 使用 TestFairy 获取 Travis-CI 构建的视频记录