转换以位图在Android [英] Convert view to bitmap on Android

查看:97
本文介绍了转换以位图在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个视图转换为位图,以preVIEW我的观点,并把它保存为图像。我试着用下面的code,但它会创建一个空白图像。我不明白,我犯了一个错误。

 查看viewToBeConverted;位图viewBitmap = Bitmap.createBitmap(viewToBeConverted.getWidth(),viewToBeConverted.getHeight(),Bitmap.Config.ARGB_8888);
帆布油画=新的Canvas(viewBitmap);
 viewToBeConverted.draw(画布);
 savephoto(F1,viewBitmap);

 ////公共无效savephoto(字符串文件名,位图位)
   {
            文件NEWFILE =新的文件(Environment.getExternalStorageDirectory()+ Picture_Card /+文件名+。PNG);
              尝试
{
                    newFile.createNewFile();
 尝试
 {
                         FileOutputStream中pdfFile =新的FileOutputStream(NEWFILE);位图BM =位; ByteArrayOutputStream BAOS =新ByteArrayOutputStream(); bm.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);字节[]字节= baos.toByteArray();
 pdfFile.write(字节);
      pdfFile.close();
 }
 赶上(FileNotFoundException异常E)
{//

  }
  }赶上(IOException异常E)
 {//
    }
  }
 

解决方案

这里是我的解决方案:

 公共静态位图getBitmapFromView(查看视图){
        //定义一个位图的大小相同的视图
        位图returnedBitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888);
        画布//绑定到它
        帆布油画=新的Canvas(returnedBitmap);
        //获取视图的背景
        可绘制bgDrawable = view.getBackground();
        如果(bgDrawable!= NULL)
            //有背景的绘制,然后绘制在画布上
            bgDrawable.draw(画布);
        其他
            //没有背景的绘制,然后绘制白色背景的画布上
            canvas.drawColor(Color.WHITE);
        //绘制视图的画布上
        view.draw(画布);
        //返回位图
        返回returnedBitmap;
    }
 

享受:)

I need to convert a view to a bitmap to preview my view and to save it as an image. I tried using the following code, but it creates a blank image. I cannot understand where I made a mistake.

 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)
 {                    //          
    }      
  }  

解决方案

here is my solution:

  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;
    }

Enjoy :)

这篇关于转换以位图在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆