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

推荐阅读
  • Flowable 6.6.0 表单引擎在Web应用中的集成与使用
    本文档提供了Flowable 6.6.0版本中表单引擎在Web应用程序中的配置和使用指南,包括表单引擎的初始化、配置以及在Web环境下的具体实现方法。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 深入解析 Apache Shiro 安全框架架构
    本文详细介绍了 Apache Shiro,一个强大且灵活的开源安全框架。Shiro 专注于简化身份验证、授权、会话管理和加密等复杂的安全操作,使开发者能够更轻松地保护应用程序。其核心目标是提供易于使用和理解的API,同时确保高度的安全性和灵活性。 ... [详细]
  • 本文详细介绍了优化DB2数据库性能的多种方法,涵盖统计信息更新、缓冲池调整、日志缓冲区配置、应用程序堆大小设置、排序堆参数调整、代理程序管理、锁机制优化、活动应用程序限制、页清除程序配置、I/O服务器数量设定以及编入组提交数调整等方面。通过这些技术手段,可以显著提升数据库的运行效率和响应速度。 ... [详细]
  • Webpack中实现环境与代码的有效分离
    本文探讨了如何在Webpack中有效地区分开发与生产环境,并实现代码的合理分离,以提高项目的可维护性和加载性能。 ... [详细]
  • 在使用 Spring Cloud Config 作为配置中心时,若在配置文件中指定了请求路径但未能生效,本文将探讨其原因及解决方案。 ... [详细]
  • 理解 Hibernate 中的 fetch 和 lazy 属性配置
    本文探讨了在 Hibernate 中如何利用 fetch 和 lazy 属性来优化数据查询效率。通过调整这两个参数,可以有效管理数据库查询的性能,减少不必要的资源消耗。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 在尝试从数据库获取设置的过程中,遇到了一个致命错误:Fatal error: Call to a member function bind_param() on boolean。本文将详细分析该错误的原因,并提供解决方案。 ... [详细]
  • 本文介绍了一个优化过的JavaScript函数,用于从API获取电影信息并渲染到网页上,同时注册Service Worker以提升用户体验和性能。 ... [详细]
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社区 版权所有