Android 视图的屏幕截图是否与视图大小不同?
所以,我试图模糊我的Android 视图的屏幕截图是否与视图大小不同?,android,kotlin,view,Android,Kotlin,View,所以,我试图模糊我的约束布局我采取的方法是什么 拍摄根视图的屏幕截图 在根视图中添加一个ImageView,其中包含布局参数MATCH_PARENT和MATCH_PARENT 模糊屏幕截图 将屏幕截图位图设置为此ImageView 但它并没有像预期的那样工作。任何帮助都将不胜感激 下面是我正在使用的一些相关代码 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceStat
约束布局
我采取的方法是什么
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
}
模糊屏幕截图
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
}
var时间=0
有趣的模糊(位图:位图,半径:浮点):位图?{
val renderScript=renderScript.create(此)
val blurredBitmap=bitmap.copy(bitmap.Config.ARGB_8888,true)
val输入:Allocation=Allocation.createFromBitmap(
renderScript,
模糊位图,
Allocation.mipmappcontrol.MIPMAP\u已满,
分配.USAGE\u共享
)
val输出:Allocation=Allocation.createTyped(renderScript,input.type)
//加载我们要使用的特定脚本的实例。
ScriptIntrinsicBlur.create(renderScript,Element.U8_4(renderScript)).apply{
设置输入(输入)
设置半径(半径)
forEach(输出)
}
output.copyTo(模糊位图)
如果(次数<5){
次数+=1
模糊(模糊位图、半径)
}
返回模糊位图
}
这是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>
而不是截图并模糊视图。试试这个:
使用BlueMaskFilter
float radius = yourTextView.getTextSize() / 3;
BlurMaskFilter filter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
yourTextView.getPaint().setMaskFilter(filter);
将模糊的文本视图
转换为位图
,并将其设置为图像视图
位图
拍摄截图、模糊截图或将其位图设置为ImageView,这到底是什么问题?@afhamu检查更新。那么您希望模糊的视图与原始视图大小相同吗?@afhamu yes man,我希望hello world模糊。这只是一个例子,在根视图中会有很多视图。问题是我把文本视图作为一个例子。它可能有多个视图。