android如何模糊图片处理图片,Android图片模糊效果

在进行模糊的时候,可以先对原始图片进行压缩,然后选择一个合适的方式进行模糊。

效果如下:

a7fbe6fee999

模糊

1、处理图片

缩放、旋转图片

private Bitmap getBitmap(Bitmap source) {

//scaleFactor:压缩比

Bitmap tempBitmap= Bitmap.createBitmap((int) (source.getWidth() / scaleFactor), (int) blurHeight, Bitmap.Config.ARGB_8888);

Canvas canvas=new Canvas(tempBitmap);

canvas.scale(1/scaleFactor,-1*blurHeight/source.getHeight());

canvas.translate(0, -source.getHeight());

Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);

paint.setFlags(Paint.FILTER_BITMAP_FLAG);

// ColorMatrix colorMatrix=new ColorMatrix();

// colorMatrix.setScale(0.8f,0.8f,0.8f,1);

// paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

canvas.drawBitmap(source, 0, 0, paint);

return tempBitmap;

}

2、模糊图片

1、fastblur

stackblur的java实现,这里

使用

Blur.fastblur(context,tempBitmap,25);

2、rs-stackblur

stackblur的Renderscript实现,这里

添加依赖

compile 'com.commit451:NativeStackBlur:1.0.2'

使用

NativeStackBlur.process(tempBitmap, 60);

其中,模糊半径越大,处理后的图片越模糊

**3、RenderScript ** ScriptIntrinsicBlur

RenderScript,一个强大的图片处理框架,可以使用ScriptIntrinsicBlur 实现高斯模糊的效果(API 17)。

需要进行如下配置:

defaultConfig {

......

renderscriptTargetApi 19

renderscriptSupportModeEnabled true

}

代码如下:

public Bitmap blurBitmap(Bitmap bitmap){

//Let's create an empty bitmap with the same size of the bitmap we want to blur

Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

//Instantiate a new Renderscript

RenderScript rs = RenderScript.create(getApplicationContext());

//Create an Intrinsic Blur Script using the Renderscript

ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

//Create the Allocations (in/out) with the Renderscript and the in/out bitmaps

Allocation allIn = Allocation.createFromBitmap(rs, bitmap);

Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

//Set the radius of the blur: 0 < radius <= 25

blurScript.setRadius(25.0f);

//Perform the Renderscript

blurScript.setInput(allIn);

blurScript.forEach(allOut);

//Copy the final bitmap created by the out Allocation to the outBitmap

allOut.copyTo(outBitmap);

//recycle the original bitmap

bitmap.recycle();

//After finishing everything, we destroy the Renderscript.

rs.destroy();

return outBitmap;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 中对网络图片应用盒状模糊效果,你可以使用第三方库 Glide 和 RenderScript。下面是一个示例代码: 1. 首先,在你的 Android 项目的 `build.gradle` 文件中添加 Glide 的依赖: ```groovy dependencies { // ... implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0' } ``` 2. 创建一个 `GlideBlurTransformation` 类来实现盒状模糊的 Glide 转换: ```java import android.content.Context; import android.graphics.Bitmap; import android.renderscript.Allocation; import android.renderscript.Element; import android.renderscript.RenderScript; import android.renderscript.ScriptIntrinsicBlur; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; import java.security.MessageDigest; public class GlideBlurTransformation extends BitmapTransformation { private static final String ID = "com.example.app.GlideBlurTransformation"; private static final byte[] ID_BYTES = ID.getBytes(CHARSET); private Context context; private float radius; public GlideBlurTransformation(Context context, float radius) { this.context = context.getApplicationContext(); this.radius = radius; } @Override protected Bitmap transform(BitmapPool bitmapPool, Bitmap bitmap, int outWidth, int outHeight) { Bitmap blurredBitmap = bitmap.copy(bitmap.getConfig(), true); RenderScript renderScript = RenderScript.create(context); Allocation inputAllocation = Allocation.createFromBitmap(renderScript, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED); Allocation outputAllocation = Allocation.createTyped(renderScript, inputAllocation.getType()); ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); blurScript.setInput(inputAllocation); blurScript.setRadius(radius); blurScript.forEach(outputAllocation); outputAllocation.copyTo(blurredBitmap); renderScript.destroy(); return blurredBitmap; } @Override public boolean equals(Object o) { return o instanceof GlideBlurTransformation; } @Override public int hashCode() { return ID.hashCode(); } @Override public void updateDiskCacheKey(MessageDigest messageDigest) { messageDigest.update(ID_BYTES); } } ``` 3. 在你的代码中,使用 Glide 来加载网络图片并应用盒状模糊转换: ```java String imageUrl = "https://example.com/image.jpg"; ImageView imageView = findViewById(R.id.imageView); float radius = 10f; // 模糊半径,可以根据需要调整 Glide.with(this) .load(imageUrl) .transform(new GlideBlurTransformation(this, radius)) .into(imageView); ``` 在上述代码中,我们使用 Glide 来加载网络图片,并通过 `GlideBlurTransformation` 类将其应用盒状模糊转换。你可以根据需要调整模糊半径 `radius` 的值。 请确保在你的 AndroidManifest.xml 文件中添加相应的网络访问权限。 希望以上信息对你有帮助!如果你还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值