1

デバイスに壁紙を設定する簡単なアプリケーションを作成しました。1つの効果を達成することはできません。写真が自動的に水平方向に中央揃えされることを望みます。これは、画像の中心が Launcher アプリの最も中心的なデスクトップにあったことを意味します。下の写真は、現在の様子を示しています。 ここに画像の説明を入力

私が達成したい効果:

ここに画像の説明を入力

そして画像自体:

ここに画像の説明を入力

この質問のコードを使用しようとしましたが、目的の効果が得られませんでした。

私のコード:

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

2 に答える 2

3

何度か試みた後、私は望ましい効果を達成することができました。

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();
                }
            }
        }
    });
}
}
于 2015-07-07T21:06:56.723 に答える