如何调整视图大小并从中创建位图

问题描述

我有一个想转换为位图的视图。我尝试应用 this solution,但它对我不起作用。

尝试解决方案时

如果使用:

v.measure(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
v.layout(0,v.getMeasuredWidth(),v.getMeasuredHeight());

视图的测量值和高度是一个很大的数字并且应用程序崩溃。进行一些测试并尝试将该视图添加到另一个视图中,我看到该视图的大小为 175(像素),因此我尝试了建议的编辑:

int specWidth = MeasureSpec.makeMeasureSpec(parentWidth,MeasureSpec.AT_MOST);
view.measure(specWidth,specWidth);
int questionWidth = view.getMeasuredWidth();

但是当将预期大小设置为 parentWidth 时,位图中的视图似乎被剪切,但在设置 175 时正确。所以我结束生成位图,然后创建一个具有预期大小的新位图

  private fun createBitmapFromView(view : View): Bitmap? {
    val size = 175
    val expectedSize = convertDpToPixels(30F)
    if (view.getMeasuredHeight() <= 0) {

      val specWidth = MeasureSpec.makeMeasureSpec(size,MeasureSpec.EXACTLY)
      view.measure(specWidth,specWidth)
      val b = Bitmap.createBitmap( view.measuredWidth,view.measuredHeight,Bitmap.Config.ARGB_8888)
      val c = Canvas(b)
      view.layout(0,size,size)
      view.draw(c)
      return Bitmap.createScaledBitmap(b,expectedSize,false)
    }
    return null
  }

如何调整视图的大小,使其高度和宽度与预期大小相同?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)