Android 图片虚化

android 中blur的使用很多。

但是大部分都是固定的图片

解决方案一般都是替换bitmap

但是对于可以缩放旋转的图片来说,虚化的的处理不能使用直接替换

在网上也查了很久的资料。但是没有结果。

最后研究了下空间的绘制过程

我们可以在OnDraw的时候使用

canvas.drawBitmap(blurbitmap, srcRect(),dstRectF(), null);

来替换被做过放大缩小处理的view

因为资料比较少,特此记录,希望能帮到有需要的人

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Android中实现背景虚化,可以使用以下步骤: 1. 在布局文件中添加一个ImageView来显示背景图片。 2. 在Java代码中,使用BitmapFactory.decodeResource()方法将背景图像文件加载到Bitmap中。 3. 使用RenderScript库来创建一个RenderScript对象并使用它来创建一个Allocation对象。 4. 创建一个ScriptIntrinsicBlur对象并使用它来处理Allocation对象。 5. 将处理后的Allocation对象写入到一个新的Bitmap中。 6. 最后,将处理后的Bitmap设置为ImageView的背景。 以下是示例代码: XML布局文件: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/background_image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/background_image" /> </LinearLayout> ``` Java代码: ``` // Load the background image into a Bitmap Bitmap backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background_image); // Create a RenderScript context RenderScript rsContext = RenderScript.create(this); // Create an Allocation object from the Bitmap Allocation inputAllocation = Allocation.createFromBitmap(rsContext, backgroundBitmap); // Create an Allocation object for the output Allocation outputAllocation = Allocation.createTyped(rsContext, inputAllocation.getType()); // Create a ScriptIntrinsicBlur object ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rsContext, Element.U8_4(rsContext)); // Set the blur radius (0 < radius <= 25) blurScript.setRadius(10); // Process the inputAllocation and write the result to the outputAllocation blurScript.setInput(inputAllocation); blurScript.forEach(outputAllocation); // Copy the outputAllocation to a new Bitmap Bitmap blurredBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(), Bitmap.Config.ARGB_8888); outputAllocation.copyTo(blurredBitmap); // Set the blurredBitmap as the background of the ImageView ImageView backgroundImageView = findViewById(R.id.background_image); backgroundImageView.setImageBitmap(blurredBitmap); ``` 这将在ImageView中显示一个虚化的背景图片。请注意,这可能会影响应用程序的性能,因此应该尽量避免在频繁更新的UI元素中使用背景虚化

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值