美文网首页UI效果
Android-使用Glide实现高斯模糊

Android-使用Glide实现高斯模糊

作者: 阿博聊编程 | 来源:发表于2022-09-27 02:23 被阅读0次
图片来源网络,入侵必删

在日常的Android开发当中,我们可能会遇到图片需要模糊处理的需求。这篇博客分享一个简单实现的方法,希望对看文章的小伙伴有所帮助。

引入开源库

implementation('com.github.bumptech.glide:glide:4.13.2')
annotationProcessor("com.github.bumptech.glide:compiler:4.13.2")
implementation('jp.wasabeef:glide-transformations:4.3.0')

简单使用

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/ivOriginal"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

    <ImageView
        android:id="@+id/ivBlur"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
</LinearLayout>

代码:

import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions.bitmapTransform
import jp.wasabeef.glide.transformations.BlurTransformation

/**
 * 类说明:实现高斯模糊的效果
 * @author ABo
 */
class BlurActivity :AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_blur)

        val ivOriginal = findViewById<ImageView>(R.id.ivOriginal)
        val ivBlur = findViewById<ImageView>(R.id.ivBlur)
        // 加载原图
        Glide.with(this).load(R.drawable.img).into(ivOriginal)
        // 高斯模糊处理
        Glide.with(this).load(R.drawable.img)
            .apply(bitmapTransform(BlurTransformation(100))).into(ivBlur)
    }
}

这里需要说明一下BlurTransformation()的构造参数是模糊程度,可以自己调节。

效果图:


代码的效果图

相关文章

网友评论

    本文标题:Android-使用Glide实现高斯模糊

    本文链接:https://www.haomeiwen.com/subject/odqfartx.html