gpt4 book ai didi

android - View 的屏幕截图与 View 的大小不同吗?

转载 作者:行者123 更新时间:2023-12-02 12:55:57 25 4
gpt4 key购买 nike

所以,我试图模糊我的ContraintLayout我采取的方法是

  • 截取根 View 的屏幕截图。
  • 在根 View 中添加ImageView,其布局参数为MATCH_PARENTMATCH_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)
    }
    获取 View 的屏幕截图
    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>
    这是结果
    enter image description here

    最佳答案

    而不是采取 View 的屏幕截图并使其模糊。试试这个:

  • 使用TextView模糊BlueMaskFilter
     float radius = yourTextView.getTextSize() / 3; 
    BlurMaskFilter filter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
    yourTextView.getPaint().setMaskFilter(filter);
  • 将模糊的TextView转换为BitMap并将其设置为ImageView位图
  • 关于android - View 的屏幕截图与 View 的大小不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63030510/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com