热门标签 | 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());
    

推荐阅读
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 利用Selenium与ChromeDriver实现豆瓣网页全屏截图
    本文介绍了一种使用Selenium和ChromeDriver结合Python代码,轻松实现对豆瓣网站进行完整页面截图的方法。该方法不仅简单易行,而且解决了新版Selenium不再支持PhantomJS的问题。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 本文详细介绍了 GWT 中 PopupPanel 类的 onKeyDownPreview 方法,提供了多个代码示例及应用场景,帮助开发者更好地理解和使用该方法。 ... [详细]
  • This guide provides a comprehensive step-by-step approach to successfully installing the MongoDB PHP driver on XAMPP for macOS, ensuring a smooth and efficient setup process. ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 本文探讨了高质量C/C++编程的最佳实践,并详细分析了常见的内存错误及其解决方案。通过深入理解内存管理和故障排除技巧,开发者可以编写更健壮的程序。 ... [详细]
  • 本文介绍了一个SQL Server自定义函数,用于从字符串中提取仅包含数字和小数点的子串。该函数通过循环删除非数字字符来实现,并附带创建测试表、存储过程以演示其应用。 ... [详细]
  • 本文详细介绍了优化DB2数据库性能的多种方法,涵盖统计信息更新、缓冲池调整、日志缓冲区配置、应用程序堆大小设置、排序堆参数调整、代理程序管理、锁机制优化、活动应用程序限制、页清除程序配置、I/O服务器数量设定以及编入组提交数调整等方面。通过这些技术手段,可以显著提升数据库的运行效率和响应速度。 ... [详细]
  • 本文介绍了如何使用JavaScript的Fetch API与Express服务器进行交互,涵盖了GET、POST、PUT和DELETE请求的实现,并展示了如何处理JSON响应。 ... [详细]
  • 版本控制工具——Git常用操作(下)
    本文由云+社区发表作者:工程师小熊摘要:上一集我们一起入门学习了git的基本概念和git常用的操作,包括提交和同步代码、使用分支、出现代码冲突的解决办法、紧急保存现场和恢复 ... [详细]
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社区 版权所有