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

声明:TIL|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA[ZH]协议进行授权

转载:转载请注明原文链接 - Bitmap的赋值与拷贝


Life is very interesting. In the end, some of your greatest pains become your greatest strengths.