Android开发中经常需要创建图片,而且从表现上来说,图片信息往往比文字信息更直观,更容易被人理解。所以创建图片的方法还是需要掌握的比较透彻,不应该像刚学Android开发一样——从别的地方复制过来再修改。

虽然我就是刚入门

目前接触到的都是通过Bitmap生成图片文件,所以创建Bitmap就成为了工作的第一布。Bitmap的创建往往是通过createBitmap方法来实现。createBitmap方法有多种,根据传入参数来选择所需。其中有:

//Returns an immutable bitmap from the specified subset of the source bitmap.

createBitmap(Bitmap source, int x, int y, int width, int height)

//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.

createBitmap(int[] colors, int width, int height, Bitmap.Config config)

//Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix.

createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

//Returns a mutable bitmap with the specified width and height.

createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config, boolean hasAlpha,ColorSpace colorSpace)

//Returns an immutable bitmap from the source bitmap.

createBitmap(Bitmap src)

//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.

createBitmap(DisplayMetrics display, int[] colors, int offset, int stride, int width, int height,Bitmap.Config config)

//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.

createBitmap(DisplayMetrics display, int[] colors, int width, int height, Bitmap.Config config)

//Returns a mutable bitmap with the specified width and height.

createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config)

//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.

createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)

//Returns a mutable bitmap with the specified width and height.

createBitmap(int width, int height, Bitmap.Config config, boolean hasAlpha,ColorSpace colorSpace)

//Returns a mutable bitmap with the specified width and height.

createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config, boolean hasAlpha)

//Returns a mutable bitmap with the specified width and height.

createBitmap(int width, int height, Bitmap.Config config)

//Returns a mutable bitmap with the specified width and height.

createBitmap(int width, int height, Bitmap.Config config, boolean hasAlpha)

//Creates a new bitmap, scaled from an existing bitmap, when possible.

createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)

在获得到Bitmap后创建Canvas,然后对Canvas进行操作,例如设置背景色,填充其它Bitmap等。用创建或传入的File创建OutputStream,再调用bitmap.compress();将Bitmap写入(压缩)文件,格式JPEG,压缩率20%,最后关闭OutputStream。代码如下:

File photo = new File("文件夹", "文件名");

Canvas canvas = new Canvas(bitmap);

OutputStream stream = new FileOutputStream(photo);

bitmap.compress(Bitmap.CompressFormat.JPEG, 20, stream);

stream.close();

对canvas进行操作完后,不是使用canvas进行什么写入文件等,而是直接通过创建canvas的bitmap。个人猜测是因为canvas与创建它的bitmap具有一一对应的连接关系,对canvas的操作使bitmap具有了相应的特性,所以直接使用bitmap来输出。

图片生成之后,若需要要在图库中显示,则最好使用广播通知媒体库的扫描,其代码如下,常传入File,封装为一个方法使用:

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

Uri contentUri = Uri.fromFile(photo);

mediaScanIntent.setData(contentUri);

sendBroadcast(mediaScanIntent);

以上就是创建图片的简单流程,复杂图片的创建大多也是在此基础上进行扩展。新技术很多,很诱人,但不打好基础,好高骛远好像不太好吧。小程序猿一枚,希望能和大家交流探讨,一起进步。

android 创建图片,[Android]创建图片相关推荐

  1. android创建图片资源文件,Android图片添加水印图片并把图片保存到文件存储的实现代码...

    具体代码如下所示: package zhangphil.test; import android.graphics.Bitmap; import android.graphics.BitmapFact ...

  2. Android学习笔记进阶十一图片动画播放(AnimationDrawable)

    大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它.它的使用更加简单,只需要创建一个 AnimationDrawabledF对象来表示Frame动画,然后通过addFrame ...

  3. 【Android 内存优化】图片文件压缩 ( Android 原生 API 提供的图片压缩功能能 | 图片质量压缩 | 图片尺寸压缩 )

    文章目录 一. 图片压缩 二. 图片文件压缩类型 三. Android 原生 API 提供的质量压缩 四. Android 原生 API 提供的尺寸压缩 一. 图片压缩 图片压缩 : ① 文件压缩 : ...

  4. Android实现Banner界面广告图片循环轮播(包括实现手动滑动循环)

    前言:经常会看到有一些app的banner界面可以实现循环播放多个广告图片和手动滑动循环.本以为单纯的ViewPager就可以实现这些功能.但是蛋疼的事情来了,ViewPager并不支持循环翻页.所以 ...

  5. 目录创建 android,创建目录浏览器  |  Android 开发者  |  Android Developers

    在 TV 上运行的媒体应用需要允许用户浏览其提供的内容.选择要播放的内容,以及开始播放内容.此类应用的内容浏览体验应简单直观,并且视觉上要赏心悦目. 本节课介绍如何利用 Leanback androi ...

  6. android 点击字体,图片背景效果一起变换Demo

    android  点击字体,图片背景效果一起变换Demo 运行效果: 点击前: 点击后: 这里我就不贴上所有的代码了,说一下他的思路: 1.分别为账号.背景图片(红色区域为一个linealayout, ...

  7. 云炬Android开发笔记 14 个人中心、图片裁剪、图片上传、收货地址、消息推送、权限管理等功能开发与一键式封装

    阅读目录 0.优化 1.个人中心页面的布局 2. 订单列表逻辑梳理与实现 3. 个人信息的更新实现 3.1 个人信息页面的布局 3.3 数据的转化  5.3 图片库中图片选择事件的处理  5.4 相机 ...

  8. [Android] 触屏setOnTouchListener实现图片缩放、移动、绘制和添加水印

        前一篇文章讲述了Android实现图片Matrix矩阵类缩放.旋转.对比度.亮度.饱和度处理,但是真正的图片软件都是使用触屏实现图片缩放.移动.添加水印等功能,所以该篇文章主要通过setOnT ...

  9. android 根据文件Uri获取图片url

    今天,简单讲讲android里如何通过文件的Uri获取文件的路径url. 昨天,我讲了如何调用手机的文件管理器获取文件资源,但是文件管理器返回的是文件的Uri,我们需要通过Uri获取文件的url才可以 ...

最新文章

  1. 他们让云撸猫变成现实,台湾大学开发手持VR设备解救吸猫人
  2. angularjs通过ng-change和watch两种方式实现对表单输入改变的监控
  3. bootstrap .col-md-6 文字居中问题处理
  4. ubuntu编写python脚本_python在ubuntu中的几种方法(小结)
  5. Mobile-LPR——面向移动端的准商业级车牌识别库
  6. 内存映射与DMA笔记
  7. Matlab Tricks(二十四)—— 将一副图像逆时针旋转 180°
  8. Linux 网络编程基础(一) ---------------客户端/服务器的简单实现
  9. 利用 SQL Monitor 查看语句运行状态步骤
  10. IOS VFL语言(页面布局)
  11. ubuntu16.04 将cuda10.1降为cuda10.0
  12. 《概率论基础教程》总结2 随机变量、期望、方差
  13. Nginx面试题及答案
  14. 一个大一计算机学生的学期总结(2022下半年)
  15. 问题 : 找出直系亲属
  16. 知识图谱推理:现代的方法与应用
  17. 云和恩墨荣获阿里云云合计划2021年度优秀伙伴技术先锋奖
  18. 【常见的优化算法介绍】
  19. 1.7 使用不同设备类型的iOS模拟器 [原创iOS开发-Xcode教程]
  20. java -p_javap使用实例图解

热门文章

  1. 【GitHub】用Bash编写的 Linux 资源监视器
  2. CentOS 7 怎样安装或升级最新的内核?
  3. netty大白话--helloword(一)
  4. 在c语言中逗号运算符若不带括号,详解shell脚本括号区别--$()、$「 」、$「 」 、$(()) 、「 」 、「[ 」]...
  5. mac地址修改_Mac 地址是什么?Mac 地址的修改及妙用!
  6. jquery ajax json传递数组,jQuery ajax 传递JSON数组到Spring Controller
  7. 如何在家搭建oracle,oracle基本操作,自己亲手做过了
  8. [专栏精选]Unity中动态构建NavMesh
  9. .NET Core版本揭秘
  10. 使用WinAPI替代System.IO.Directory