Skip to content

Instantly share code, notes, and snippets.

@huuphuoc1396
Last active August 21, 2022 02:46
Show Gist options
  • Save huuphuoc1396/e3131302440faa3807634feee82c1ad8 to your computer and use it in GitHub Desktop.
Save huuphuoc1396/e3131302440faa3807634feee82c1ad8 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="BlurTextView">
<attr name="blurRadius" format="dimension" />
<attr name="blurType">
<flag name="normal" value="0" />
<flag name="solid" value="1" />
<flag name="outer" value="2" />
<flag name="inner" value="3" />
</attr>
</declare-styleable>
</resources>
import android.content.Context
import android.graphics.BlurMaskFilter
import android.util.AttributeSet
import com.google.android.material.textview.MaterialTextView
import com.gooo_group.gooo.R
class BlurTextView(context: Context, attrs: AttributeSet) : MaterialTextView(context, attrs) {
init {
val typedArray = context.theme.obtainStyledAttributes(
attrs, R.styleable.BlurTextView, 0, 0
)
val blurRadius = typedArray.getDimension(R.styleable.BlurTextView_blurRadius, 0f)
val filter = when (typedArray.getInt(R.styleable.BlurTextView_blurType, 0)) {
0 -> BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.NORMAL)
1 -> BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.SOLID)
2 -> BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.OUTER)
3 -> BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.INNER)
else -> BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.NORMAL)
}
paint.maskFilter = filter
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.android.example.view.BlurTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:blurRadius="4dp"
app:blurType="solid" />
</FrameLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment