壁纸正在及时缩放

时间:2015-03-11 13:48:50

标签: android

壁纸有问题。某些旧的和低端设备上会出现此问题。我正在尝试安装可能无法滚动的壁纸(设备比例)并且通常会安装它们。但是经过一段时间(2或3天)后,壁纸会缩放2次(看起来并不漂亮)并开始滚动 以下是安装壁纸的代码的一部分:

public class WallpaperInstaller {

    private Context mContext;
    private CropImageView cropImageView;
    private ImageLoader loader;
    private boolean isCropped;

    public WallpaperInstaller(Context context, final CropImageView civ, ImageLoader imageLoader) {
        this.mContext = context;
        this.cropImageView = civ;
        this.loader = imageLoader;
        this.isCropped = true;
    }

    public WallpaperInstaller(Context context, ImageLoader imageLoader) {
        this.mContext = context;
        this.loader = imageLoader;
        this.isCropped = false;
    }

    public Thread setWallpaper(final String URL){
        Thread setWallpaperThread = new Thread(new Runnable() {
            @Override
            public void run() {

                WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext);
                try {
                    Bitmap croppedBitmap;
                    if(isCropped){
                        int scale = 1;
                        RectF rect = cropImageView.getActualCropRect();
                        int cropX = (int) rect.left * scale;
                        int cropY = (int) rect.top * scale;
                        int cropW = (int) rect.width() * scale;
                        int cropH = (int) rect.height() * scale;
                        croppedBitmap = Bitmap.createBitmap(loader.loadImageSync(URL), cropX, cropY, cropW, cropH);
                    } else {
                        try{
                            croppedBitmap = Bitmap.createBitmap(loader.loadImageSync(URL));
                        } catch (NullPointerException e){
                            loader.init(ImageLoaderConfiguration.createDefault(mContext));
                            croppedBitmap = Bitmap.createBitmap(loader.loadImageSync(URL));
                        }

                    }

                    boolean isScrollable = croppedBitmap.getWidth() > croppedBitmap.getHeight();

                    if(isScrollable){
                        wallpaperManager.setWallpaperOffsetSteps(-1, -1);
                        wallpaperManager.suggestDesiredDimensions(getWidth(croppedBitmap), getDisplay().getHeight());
                        wallpaperManager.setBitmap(Bitmap.createScaledBitmap(croppedBitmap, getWidth(croppedBitmap), getDisplay().getHeight(), false));
                    } else{
                        wallpaperManager.setWallpaperOffsetSteps(1, 1);
                        wallpaperManager.suggestDesiredDimensions(getDisplay().getWidth(), getDisplay().getHeight());
                        wallpaperManager.setBitmap(Bitmap.createScaledBitmap(croppedBitmap, getDisplay().getWidth(), getDisplay().getHeight(), false));
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            private int getWidth(Bitmap bitmap){
                return (int)((float)getDisplay().getHeight()*(float)bitmap.getWidth()/(float)bitmap.getHeight());
            }

            private Display getDisplay(){
                WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
                return windowManager.getDefaultDisplay();
            }

        });
        return setWallpaperThread;
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

尝试添加下一行:

wallpaperManager.forgetLoadedWallpaper(); wallpaperManager.clear();