gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:39:55 25 4
gpt4 key购买 nike

我写了一个简单的应用程序来设置设备上的壁纸。我无法达到一种效果。我希望图片自动水平居中。这意味着图像的中心位于 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com