在 Android 上将视图转换为位图

Posted

技术标签:

【中文标题】在 Android 上将视图转换为位图【英文标题】:Convert view to bitmap on Android 【发布时间】:2011-07-29 00:12:24 【问题描述】:

我需要将视图转换为位图以预览我的视图并将其保存为图像。我尝试使用以下代码,但它会创建一个空白图像。我不明白我在哪里犯了错误。

 View viewToBeConverted;  Bitmap viewBitmap =   Bitmap.createBitmap(viewToBeConverted.getWidth(), viewToBeConverted.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap); 
 viewToBeConverted.draw(canvas); 
 savephoto(“f1”, viewBitmap); 

 ////  public void savephoto(String filename,Bitmap bit)     
     
            File newFile = new File(Environment.getExternalStorageDirectory() + Picture_Card/"+ filename+ ".PNG");
              try 

                    newFile.createNewFile();                   
 try
  
                         FileOutputStream pdfFile = new FileOutputStream(newFile);                                                Bitmap bm = bit;                          ByteArrayOutputStream baos = new ByteArrayOutputStream();                          bm.compress(Bitmap.CompressFormat.PNG,100, baos);                                                     byte[] bytes = baos.toByteArray();                         
 pdfFile.write(bytes);                                              
      pdfFile.close();                   
 
 catch (FileNotFoundException e) 
                          //       

              
   catch (IOException e)
                     //          
          
    

【问题讨论】:

【参考方案1】:

这是我的解决方案:

  public static Bitmap getBitmapFromView(View view) 
        //Define a bitmap with the same size as the view
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
        //Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        //Get the view's background
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null) 
            //has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else 
            //does not have background drawable, then draw white background on the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        view.draw(canvas);
        //return the bitmap
        return returnedBitmap;
    

享受:)

【讨论】:

@Gil SH,您是否发现最终图片的质量不如屏幕上的那样好?我花了一些时间来解决它,但它失败了。你有什么想法吗?【参考方案2】:

投票最多的解决方案对我不起作用,因为我的视图是 ViewGroup(已从 LayoutInflater 膨胀)。我需要调用 view.measure 来强制计算视图大小,以便使用 view.getMeasuredWidth(Height) 获得正确的视图大小。

public static Bitmap getBitmapFromView(View view) 
    view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.draw(canvas);
    return bitmap;

【讨论】:

终于!真正的答案在这里。【参考方案3】:

使用新的kotlin扩展函数view.drawToBitmap()

mLayout.drawToBitmap()

【讨论】:

【参考方案4】:

这是 Kotlin 中的扩展,灵感来自:Google android Maps Utils Icon Generator

fun View.convertToBitmap(): Bitmap 
val measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
measure(measureSpec, measureSpec)
layout(0, 0, measuredWidth, measuredHeight)
val r = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888)
r.eraseColor(Color.TRANSPARENT)
val canvas = Canvas(r)
draw(canvas)
return r 

【讨论】:

【参考方案5】:

在画布上使用绘图的所有答案都不适用于 GLSurfaceView。

要将 GLSurfaceView 的内容捕获到位图中,您应该考虑在 Renderer::onDrawFrame() 内实现带有 gl.glReadPixels 的自定义方法。

解决方案 sn-p 已发布here。

【讨论】:

【参考方案6】:

将布局或视图转换为位图:

  private Bitmap createBitmapFromLayout(View tv)         
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    tv.measure(spec, spec);
    tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(tv.getMeasuredWidth(), tv.measuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate((-tv.getScrollX()), (-tv.getScrollY()));
    tv.draw(c);
    return b;

没有xml:

  private Bitmap createBitmapFromView() 
    TextView tv = new TextView(this);
    tv.setText("Hello Android !!");
    tv.setTextColor(Color.WHITE);
    tv.setBackgroundColor(Color.GRAY);
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    tv.measure(spec, spec);
    tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(tv.getMeasuredWidth(), tv.measuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate((-tv.getScrollX()), (-tv.getScrollY()));
    tv.draw(c);
    return b;

【讨论】:

以上是关于在 Android 上将视图转换为位图的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 上将大型位图文件调整为缩放的输出文件

将视图转换为位图而不在 Android 中显示?

Android将base64编码的字符串转换为图像视图

打开相机意图时如何防止android应用程序方向

在 Android 中将位图转换为灰度

如何在android上将kotlin转换为java? [复制]