热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将画布转换为android中的位图图像-convertingacanvasintobitmapimageinandroid

Iamtryingtodevelopanapponcanvas,Iamdrawingabitmaponthecanvas.Afterdrawing,iamtryin

I am trying to develop an app on canvas,I am drawing a bitmap on the canvas.After drawing,i am trying to convert into bitmap image.

我正在尝试在canvas上开发一个应用程序,我正在画布上绘制位图。在绘制完成后,我正在尝试转换为位图图像。

can anyone give me a suggestion.

谁能给我一个建议吗?

thank you in advance.

提前谢谢你。

4 个解决方案

#1


62  

Advice depends upon what you are trying to do.

建议取决于你想做什么。

If you are concerned that your controls take a long time to draw, and you want to draw to a bitmap so you can blit the bitmap rather than re-drawing via a canvas, then you don't want to be double-guessing the platform - controls automatically cache their drawing to temporary bitmaps, and these can even be fetched from the control using getDrawingCache()

如果你担心控制需要很长时间才能画出你想画一个位图可以位块传输位图,而不是通过一个画布无可厚非,那么你不想double-guessing平台—控制自动缓存临时位图绘制,而这些甚至可以拿来使用getDrawingCache从控制()

If you want to draw using a canvas to a bitmap, the usual recipe is:

如果您想要将画布绘制成位图,通常的方法是:

  1. Create a bitmap of the correct size using Bitmap.createBitmap()
  2. 使用bitmap . createbitmap()创建正确大小的位图
  3. Create a canvas instance pointing that this bitmap using Canvas(Bitmap) constructor
  4. 创建一个画布实例,使用canvas(位图)构造函数指示该位图。
  5. Draw to the canvas
  6. 绘制到画布
  7. Use the bitmap
  8. 使用位图

#2


19  

So you create a new Bitmap, for example:

所以你要创建一个新的位图,例如:

Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 )

位图myBitmap =新的位图(int)宽度,(int)高度,配置。RGB_565)

with width and height being the same as your canvas.

宽度和高度与画布相同。

Next, use canvas.setBitmap(myBitmap), but not drawBitmap().

接下来,使用canvas.setBitmap(myBitmap),而不是drawBitmap()。

After you call setBitmap, all what you draw on canvas is in fact, drawing on your myBitmap going by the example code I have illustrated.

在您调用setBitmap之后,您在画布上绘制的所有内容实际上都是通过我所演示的示例代码绘制的myBitmap。

Edit:

编辑:

You can not create a bitmap directly such as:

不能直接创建位图,如:

Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 );

You must use instead:

您必须使用:

Bitmap myBitmap = Bitmap.createBitmap( (int)Width, (int)Height, Config.RGB_565 );

#3


2  

Other example:

其他的例子:

public Bitmap getBitmapNews(int item , boolean selected, int numbernews){                   
        Bitmap bitmap;

        if(selected)
            bitmap=mBitmapDown[item].copy(Config.ARGB_8888, true);
        else 
            bitmap=mBitmapUp[item].copy(Config.ARGB_8888, true);

        Canvas canvas = new Canvas(bitmap);

        if(numbernews<10){
        canvas.drawBitmap(mNotiNews[numbernews],0,0,null);
        }else{
            canvas.drawBitmap(mNotiNews[0],0,0,null);
        }

 return bitmap; 
}

#4


0  

Following are the steps to convert from canvas to bitmap and storing it to gallery or specific folder.

下面是将画布转换为位图并将其存储到图库或特定文件夹的步骤。

Note: Make sure you have given permission of WRITE_EXTERNAL_STORAGE

注意:请确保您已授予WRITE_EXTERNAL_STORAGE权限

activity_main.xml

activity_main.xml

            

                

            

MainActivity.java

MainActivity.java

  1. Create reference of parent layout

    创建对父布局的引用。

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    
  2. To store it into gallery

    储存在画廊

    final String imagename = UUID.randomUUID().toString() + ".png";
    MediaStore.Images.Media.insertImage(getContentResolver(), linearLayout .getDrawingCache(), imagename, "drawing");
    
  3. To convert into bitmap

    转换成位图

    linearLayout.setDrawingCacheEnabled(true);
    linearLayout.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(linearLayout.getDrawingCache());
    

推荐阅读
author-avatar
郭雪峰Rongeqw_983
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有