抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

Bitmap的赋值与拷贝

最近有一个需求,需要对壁纸进行处理,意思是先取背景,再取部分进行虚化,再赋值到另外一个view上进行显示。代码如下:

/**
* 高斯模糊
* @param radius
* @param original
* @return
*/
public Bitmap gaussianBlur(@IntRange(from = 1, to = 25) int radius, Bitmap original) {
Allocation input = Allocation.createFromBitmap(renderScript, original);
Allocation output = Allocation.createTyped(renderScript, input.getType());
ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
scriptIntrinsicBlur.setRadius(radius);
scriptIntrinsicBlur.setInput(input);
scriptIntrinsicBlur.forEach(output);
output.copyTo(original);
return original;
}

public Drawable getCurrentWallpaperDrawable(){
WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext);
// 获取当前壁纸
return wallpaperManager.getDrawable();
}

public Observable<Bitmap> getFullImageBitmap() {
return Observable.create(new ObservableOnSubscribe<Bitmap>() {
@Override
public void subscribe(ObservableEmitter<Bitmap> e){
// Drawable,转成Bitmap
Bitmap wallpaper = ((BitmapDrawable) getCurrentWallpaperDrawable()).getBitmap();
e.onNext(gaussianBlur(16,wallpaper.copy(Bitmap.Config.ARGB_8888,false)));
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}

但是问题出现了,需要显示模糊的地方确实没问题,但是壁纸也变模糊了,因为对壁纸处理的地方有好几处,后来通过调试得出位置,在处理模糊时拿的是壁纸本身的bitmap的引用(以上代码已修复)

查阅相关资料得知:等于号并不能用于bitmap的复制,因为传递的是引用,调用bitmap的copy方法即可。

参考:https://blog.csdn.net/qq_31391977/article/details/78677381


本站由以下主机服务商提供服务支持:

0条评论

发表评论