因此,我正在尝试弄清楚我的ContraintLayout
采取的方法是
ImageView
和MATCH_PARENT
添加一个MATCH_PARENT
。ImageView
。但是它没有按预期工作。任何帮助将不胜感激。
这是我正在使用的一些相关代码。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imageView = ImageView(this)
val param = ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.MATCH_PARENT
)
imageView.layoutParams = param
val bit = convertViewToBitmap(root)
val blurred = blur(bit,25F)
imageView.setImageBitmap(blurred)
root.addView(imageView)
}
获取视图的屏幕截图
private fun convertViewToBitmap(view: View): Bitmap {
val spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
view.measure(spec, spec)
view.layout(0, 0, view.measuredWidth, view.measuredHeight)
val b = Bitmap.createBitmap(view.measuredWidth, view.measuredHeight,
Bitmap.Config.ARGB_8888)
val c = Canvas(b)
//c.translate((-view.scrollX).toFloat(), (-view.scrollY).toFloat())
view.draw(c)
return b
}
要模糊该ScreenShot
var times = 0
fun blur(bitmap: Bitmap, radius: Float): Bitmap? {
val renderScript = RenderScript.create(this)
val blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
val input: Allocation = Allocation.createFromBitmap(
renderScript,
blurredBitmap,
Allocation.MipmapControl.MIPMAP_FULL,
Allocation.USAGE_SHARED
)
val output: Allocation = Allocation.createTyped(renderScript, input.type)
// Load up an instance of the specific script that we want to use.
ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)).apply {
setInput(input)
setRadius(radius)
forEach(output)
}
output.copyTo(blurredBitmap)
if (times < 5) {
times += 1
blur(blurredBitmap, radius)
}
return blurredBitmap
}
这是XML文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.20" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案 0 :(得分:0)
而不是获取视图的屏幕截图并使其模糊。试试这个:
使用TextView
来模糊BlueMaskFilter
float radius = yourTextView.getTextSize() / 3;
BlurMaskFilter filter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
yourTextView.getPaint().setMaskFilter(filter);
将模糊的TextView
转换为BitMap
并将其设置为ImageView
位图