Bitmap.recycle()


There is a nice callback in the cache called entryRemoved which gives you the item after it’s removed, and you would think here is the perfect time to call Bitmap.recycle(), but do not do this. The reason is that there is no guarantee that the Bitmap isn’t being referenced by a View, and you can’t recycle a Bitmap being used.
public Drawable scaleWallpaper(Context context, Bitmap originalBitmap) { if (originalBitmap == null) { Log.d(TAG, "Invalid input: originalBitmap is null"); return null; } Bitmap scaledBitmap = null; Bitmap croppedBitmap = null; try { WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics displayMetrics = new DisplayMetrics(); if (windowManager != null) { windowManager.getDefaultDisplay().getRealMetrics(displayMetrics); } final int screenWidth = displayMetrics.widthPixels; final int screenHeight = displayMetrics.heightPixels; final int originalWidth = originalBitmap.getWidth(); final int originalHeight = originalBitmap.getHeight(); float scale = Math.max( (float) screenWidth / originalWidth, (float) screenHeight / originalHeight ); int scaledWidth = Math.round(originalWidth * scale); int scaledHeight = Math.round(originalHeight * scale); scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, scaledWidth, scaledHeight, true); int cropX = Math.max((scaledWidth - screenWidth) / 2, 0); int cropY = 0; int safeWidth = Math.min(screenWidth, scaledWidth); int safeHeight = Math.min(screenHeight, scaledHeight); croppedBitmap = Bitmap.createBitmap(scaledBitmap, cropX, cropY, safeWidth, safeHeight) .copy(Bitmap.Config.RGB_565, true); final Drawable resultDrawable = new BitmapDrawable(context.getResources(), croppedBitmap); if (originalBitmap != null && originalBitmap != croppedBitmap && !originalBitmap.isRecycled()) { originalBitmap.recycle(); } if (scaledBitmap != null && scaledBitmap != croppedBitmap && !scaledBitmap.isRecycled()) { scaledBitmap.recycle(); } return resultDrawable; } catch (Exception e) { Log.d(TAG, "Bitmap scale processing failed: " + e.getMessage()); return null; } }解释该段代码,当前获取壁纸有条纹怎么优化
10-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值