android:绘图 (android.graphics包)

在这里插入图片描述

android:绘图 View:组件,理解为画布 Drawable:所有可见对象的描述,理解为:素材类 Bitmap:图片类 Canvas:画笔 Paint:画笔样式与颜色、特效的集合

近期很多网友对Android用户界面的设计表示很感兴趣,对于Android UI开发自绘控件和游戏制作而言掌握好绘图基础是必不可少的。本次专题分10节来讲述,有关OpenGL ES相关的可能将放到以后再透露。本次主要涉及以下四个包的相关内容: android.content.res 资源类 android.graphics 底层图形类 android.view 显示类 android.widget 控件类

一、android.content.res.Resources

对于Android平台的资源类android.content.res.Resources可能很多网友比较陌生,一起来看看SDK上是怎么介绍的吧,Contains classes for accessing application resources, such as raw asset files, colors, drawables, media or other other files in the package, plus important device configuration details (orientation, input types, etc.) that affect how the application may behave.平时用到的二进制源文件raw、颜色colors、图形drawables和多媒体文件media的相关资源均通过该类来管理。

int getColor(int id) 对应res/values/colors.xml Drawable getDrawable(int id) 对应res/drawable/ XmlResourceParser getLayout(int id) 对应res/layout/ String getString(int id) 和CharSequence getText(int id) 对应res/values/strings.xml InputStream openRawResource(int id) 对应res/raw/ void parseBundleExtra (String tagName, AttributeSet attrs, Bundle outBundle) 对应res/xml/

String[] getStringArray(int id) res/values/arrays.xml float getDimension(int id) res/values/dimens.xml

二、android.graphics.Bitmap

作为位图操作类,Bitmap提供了很多实用的方法,常用的我们总结如下: boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) 压缩一个Bitmap对象根据相关的编码、画质保存到一个OutputStream中。其中第一个压缩格式目前有JPG和PNG void copyPixelsFromBuffer(Buffer src) 从一个Buffer缓冲区复制位图像素 void copyPixelsToBuffer(Buffer dst) 将当前位图像素内容复制到一个Buffer缓冲区

我们看到创建位图对象createBitmap包含了6种方法在目前的Android 2.1 SDK中,当然他们使用的是API Level均为1,所以说从Android 1.0 SDK开始就支持了,所以大家可以放心使用。

static Bitmap createBitmap(Bitmap src) static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config) static Bitmap createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config) static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) static Bitmap createBitmap(int width, int height, Bitmap.Config config) static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) //创建一个可以缩放的位图对象

final int getHeight() 获取高度 final int getWidth() 获取宽度 final boolean hasAlpha() 是否有透明通道 void setPixel(int x, int y, int color) 设置某像素的颜色 int getPixel(int x, int y) 获取某像素的颜色,android开发网提示这里返回的int型是color的定义

三、android.graphics.BitmapFactory

作为Bitmap对象的I/O类,BitmapFactory类提供了丰富的构造Bitmap对象的方法,比如从一个字节数组、文件系统、资源ID、以及输入流中来创建一个Bitmap对象,下面本类的全部成员,除了decodeFileDescriptor外其他的重载方法都很常用。

static Bitmap decodeByteArray(byte[] data, int offset, int length) //从字节数组创建

static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)

static Bitmap decodeFile(String pathName, BitmapFactory.Options opts) //从文件创建,路径要写全

static Bitmap decodeFile(String pathName)

static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts) //从输入流句柄创建

static Bitmap decodeFileDescriptor(FileDescriptor fd)

static Bitmap decodeResource(Resources res, int id) //从Android的APK文件资源中创建,android123提示是从/res/的drawable中

static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options opts)

static Bitmap decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts)

static Bitmap decodeStream(InputStream is) //从一个输入流中创建

static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)

四、android.graphics.Canvas

从J2ME MIDLET时我们就知道Java提供了Canvas类,而目前在Android平台中,它主要任务为管理绘制过程,The Canvas class holds the “draw” calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).

该类主要提供了三种构造方法,分别为构造一个空的Canvas、从Bitmap中构造和从GL对象中创建,如下

Canvas() Canvas(Bitmap bitmap) Canvas(GL gl)

同时Canvas类的一些字段保存着重要的绘制方法定义,比如Canvas.HAS_ALPHA_LAYER_SAVE_FLAG 保存时需要alpha层,对于Canvas类提供的方法很多,每个都很重要,下面我们一一作介绍

boolean clipPath(Path path) boolean clipPath(Path path, Region.Op op) boolean clipRect(float left, float top, float right, float bottom) boolean clipRect(Rect rect) boolean clipRect(float left, float top, float right, float bottom, Region.Op op) boolean clipRect(Rect rect, Region.Op op) boolean clipRect(RectF rect) boolean clipRect(RectF rect, Region.Op op) boolean clipRect(int left, int top, int right, int bottom) boolean clipRegion(Region region, Region.Op op) boolean clipRegion(Region region)

void concat(Matrix matrix)

void drawARGB(int a, int r, int g, int b) void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) void drawBitmap(int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint) void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) void drawBitmap(int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha, Paint paint) void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint)

void drawCircle(float cx, float cy, float radius, Paint paint) void drawColor(int color) void drawColor(int color, PorterDuff.Mode mode) void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) void drawLines(float[] pts, Paint paint) void drawLines(float[] pts, int offset, int count, Paint paint) void drawOval(RectF oval, Paint paint) void drawPaint(Paint paint) void drawPath(Path path, Paint paint)

void drawPicture(Picture picture, RectF dst) void drawPicture(Picture picture, Rect dst) void drawPicture(Picture picture) void drawPoint(float x, float y, Paint paint) void drawPoints(float[] pts, int offset, int count, Paint paint) void drawPoints(float[] pts, Paint paint) void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) void drawPosText(String text, float[] pos, Paint paint) void drawRGB(int r, int g, int b) void drawRect(RectF rect, Paint paint) void drawRect(float left, float top, float right, float bottom, Paint paint) void drawRect(Rect r, Paint paint) void drawRoundRect(RectF rect, float rx, float ry, Paint paint) void drawText(String text, int start, int end, float x, float y, Paint paint) void drawText(char[] text, int index, int count, float x, float y, Paint paint) void drawText(String text, float x, float y, Paint paint) void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint) void drawVertices(Canvas.VertexMode mode, int vertexCount, float[] verts, int vertOffset, float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, int indexOffset, int indexCount, Paint paint) static void freeGlCaches() boolean getClipBounds(Rect bounds) final Rect getClipBounds() int getDensity() DrawFilter getDrawFilter() GL getGL() int getHeight() void getMatrix(Matrix ctm) final Matrix getMatrix() int getSaveCount() int getWidth() boolean isOpaque() boolean quickReject(Path path, Canvas.EdgeType type) boolean quickReject(float left, float top, float right, float bottom, Canvas.EdgeType type) boolean quickReject(RectF rect, Canvas.EdgeType type) void restore() void restoreToCount(int saveCount) final void rotate(float degrees, float px, float py) void rotate(float degrees) int save() int save(int saveFlags) int saveLayer(float left, float top, float right, float bottom, Paint paint, int saveFlags) int saveLayer(RectF bounds, Paint paint, int saveFlags) int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int saveFlags) int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) final void scale(float sx, float sy, float px, float py) void scale(float sx, float sy) void setBitmap(Bitmap bitmap) void setDensity(int density) void setDrawFilter(DrawFilter filter) void setMatrix(Matrix matrix) void setViewport(int width, int height) void skew(float sx, float sy) void translate(float dx, float dy)

五、android.graphics.Color

有关Android平台上表示颜色的方法有很多种,Color提供了常规主要颜色的定义比如Color.BLACK和Color.GREEN等等,我们平时创建时主要使用以下静态方法

static int argb(int alpha, int red, int green, int blue) 构造一个包含透明对象的颜色 static int rgb(int red, int green, int blue) 构造一个标准的颜色对象 static int parseColor(String colorString) 解析一种颜色字符串的值,比如传入Color.BLACK

本类返回的均为一个×××类似 绿色为0xff00ff00,红色为0xffff0000。我们将这个DWORD型看做AARRGGBB,AA代表Aphla透明色,后面的就不难理解,每个分成WORD整好为0-255。

今天我们继续介绍Android平台底层绘图类的相关内容,在Android UI开发专题(一) 之界面设计中我们介绍了有关Android平台资源使用以及Bitmap相关类的操作,接下来将会以实例的方式给大家演示各种类的用处以及注意点。今天我们继续了解android.graphics包中比较重要的绘图类。

一、 android.graphics.Matrix

有关图形的变换、缩放等相关操作常用的方法有:

void reset() // 重置一个matrix对象。 void set(Matrix src) //复制一个源矩阵,和本类的构造方法 Matrix(Matrix src) 一样 boolean isIdentity() //返回这个矩阵是否定义(已经有意义)

void setRotate(float degrees) //指定一个角度以0,0为坐标进行旋转

void setRotate(float degrees, float px, float py) //指定一个角度以px,py为坐标进行旋转

void setScale(float sx, float sy) // 缩放

void setScale(float sx, float sy, float px, float py) //以坐标px,py进行缩放

void setTranslate(float dx, float dy) //平移

void setSkew (float kx, float ky, float px, float py) //以坐标px,py进行倾斜

void setSkew (float kx, float ky) //倾斜

二、android.graphics.NinePatch

NinePatch是Android平台特有的一种非矢量图形自然拉伸处理方法,可以帮助常规的图形在拉伸时不会缩放,实例中Android开发网提示大家对于Toast的显示就是该原理,同时SDK中提供了一个工具名为Draw 9-Patch,有关该工具的使用方法可以参考我们经发布的 Draw 9-Patch使用方法介绍一文。由于该类提供了高质量支持透明的缩放方式,所以图形格式为PNG,文件命名方式为.9.png 的后缀比如android123.9.png。

三、android.graphics.Paint

Paint类我们可以理解为画笔、画刷的属性定义,本类常用的方法如下:

void reset() //重置 void setARGB(int a, int r, int g, int b) 或 void setColor(int color) 均为设置Paint对象的颜色

void setAntiAlias(boolean aa) //是否抗锯齿,需要配合void setFlags (Paint.ANTI_ALIAS_FLAG) 来帮助消除锯齿使其边缘更平滑。

Shader setShader(Shader shader) //设置阴影,Shader类是一个矩阵对象,如果为NULL将清除阴影。 void setStyle(Paint.Style style) //设置样式,一般为 FILL 填充,或者STROKE凹陷效果。 void setTextSize(float textSize) //设置字体大小 void setTextAlign(Paint.Align align) //文本对齐方式 Typeface setTypeface(Typeface typeface) //设置字体,通过Typeface可以加载Android内部的字体,一般为宋体对于中文,部分ROM可以自己添加比如雅黑等等 void setUnderlineText(boolean underlineText) //是否设置下划线,需要撇和void setFlags (Paint.UNDERLINE_TEXT_FLAG) 方法。

四、android.graphics.Rect

Rect我们可以理解为矩形区域,类似的还有Point一个点,Rect类除了表示一个矩形区域位置描述外,android123提示主要可以帮助我们计算图形之间是否碰撞(包含)关系,对于Android游戏开发比较有用,其主要的成员contains包含了三种重载方法,来判断包含关系

boolean contains(int left, int top, int right, int bottom) boolean contains(int x, int y) boolean contains(Rect r)

五、android.graphics.Region

Region在Android平台中表示一个区域和Rect不同的是,它表示的是一个不规则的样子,可以是椭圆、多边形等等,而Rect仅仅是矩形。同样Region的boolean contains(int x, int y) 成员可以判断一个点是否在该区域内

六、android.graphics.Typeface

Typeface类是帮助描述一个字体对象,在TextView中通过使用setTypeface方法来制定一个输出文本的字体,其直接构造调用成员create方法可以直接指定一个字体名称和样式,比如

static Typeface create(Typeface family, int style) static Typeface create(String familyName, int style)

同时使用isBold和isItalic方法可以判断出是否包含粗体或斜体的字型。

final boolean isBold() final boolean isItalic()

该类的创建方法还有从apk的资源或从一个具体的文件路径,其具体方法为

static Typeface createFromAsset(AssetManager mgr, String path) static Typeface createFromFile(File path) static Typeface createFromFile(String path)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/78496.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【c语言】字符函数与字符串函数(上)

大家好呀,今天给大家分享一下字符函数和字符串函数,说起字符函数和字符串函数大家会想到哪些呢??我想到的只有求字符串长度的strlen,拷贝字符串的strcpy,字符串比较相同的strcmp,今天,我要分享给大家的是我们一些其他的…

Git多版本并行开发实践

本文目的: 实现多个项目同时进行的git多版本管理工作流。 名词解释: feature-XXXX:特性分支指CCS中一个项目或者一个迭代,在该分支上开发,完成后,合并,最后,删除该分支,…

数据结构,线性表,顺序表基础。

1.线性表 线性表特征&#xff1a; 对非空表&#xff0c;a0是表头&#xff0c;无前驱a(n-1)是表尾&#xff0c;无后继其他的都有ai前驱a(i-1)与后继a(i1)。 2、顺序结构存储方式是线性表存储的一种方式&#xff0c;主要体现形式为数组。 #include<stdio.h> #include<st…

二、SQL,如何实现表的创建和查询

1、新建表格&#xff08;在当前数据库中新建一个表格&#xff09;&#xff1a; &#xff08;1&#xff09;基础语法&#xff1a; create table [表名]( [字段:列标签] [该列数据类型] comment [字段注释], [字段:列标签] [该列数据类型] comment [字段注释], ……&#xff0c…

2020年3月全国计算机等级考试真题(C语言二级)

2020年3月全国计算机等级考试真题&#xff08;C语言二级&#xff09; 第1题 有以下程序 void fun1 (char*p) { char*q; qp; while(*q!\0) { (*Q); q&#xff1b; } } main() { char a[]{"Program"},*p; p&a[3]; fun1(p); print…

由浅入深详解四种分布式锁

在多线程环境下&#xff0c;为了保证数据的线程安全&#xff0c;锁保证同一时刻&#xff0c;只有一个可以访问和更新共享数据。在单机系统我们可以使用synchronized锁或者Lock锁保证线程安全。synchronized锁是Java提供的一种内置锁&#xff0c;在单个JVM进程中提供线程之间的锁…

Hyper-v导致Vmware window无法启动崩溃记录

最近有几次vmware启动window10直接崩溃情况&#xff0c;显示蓝屏报错。一开始没在意&#xff0c;以为是因为固态硬盘错了几个字节导致的&#xff1f; 但后来想想不对啊。vmware用了也有10来年了&#xff0c;稳得一笔&#xff0c;在仔细思考了一下后发现打不开的win10这三个虚拟…

go_并发编程(1)

go并发编程 一、 并发介绍1&#xff0c;进程和线程2&#xff0c;并发和并行3&#xff0c;协程和线程4&#xff0c;goroutine 二、 Goroutine1&#xff0c;使用goroutine1&#xff09;启动单个goroutine2&#xff09;启动多个goroutine 2&#xff0c;goroutine与线程3&#xff0…

使用PDF文件入侵任何操作系统

提示&#xff1a;我们8月28号开学,所以我得快点更新了&#xff0c;不能拖了&#x1f625; 文章目录 前言一、打开终端总结 前言 PDF文件被广泛应用于共享信息&#xff0c;电子邮件&#xff0c;网站或文档或存储系统的真实链接 它可以用于恶意软件的载体。 不要问我什么意思&am…

【克罗恩病是银屑病及银屑病关节炎的因果风险因素】

克罗恩病是银屑病及银屑病关节炎的因果风险因素 ①纳入463372名欧洲人&#xff0c;包括12882例IBD患者、5621例银屑病患者、2063例银屑病关节炎患者&#xff1b;②单变量孟德尔随机化&#xff08;MR&#xff09;分析表明&#xff0c;基于遗传因素预测的IBD与较高的银屑病和银屑…

LangChain源码逐行解密之系统(二)

LangChain源码逐行解密之系统 20.2 serapi.py源码逐行剖析 我们可以看一下Google查询的例子,在LangChain中有多种实现的方式。 如图20-5所示,在utilities的serpapi.py代码文件中实现了SerpAPIWrapper。 图20- 5 utilities的serpapi.py的SerpAPIWrapper 在langchain目录的se…

力扣:64. 最小路径和(Python3)

题目&#xff1a; 给定一个包含非负整数的 m x n 网格 grid &#xff0c;请找出一条从左上角到右下角的路径&#xff0c;使得路径上的数字总和为最小。 说明&#xff1a;每次只能向下或者向右移动一步。 来源&#xff1a;力扣&#xff08;LeetCode&#xff09; 链接&#xff1a…

dll修复精灵怎么下载,vcruntime140.dll丢失该如何修复

vcruntime140.dll是Microsoft Visual C Redistributable中的一个动态链接库&#xff08;DLL&#xff09;文件。它是一种运行时库&#xff08;Runtime Library&#xff09;&#xff0c;用于支持使用Microsoft Visual C编写的程序的正常运行。作为一个DLL文件&#xff0c;vcrunti…

进行 200 瓦太阳能 (PV) 模块设计以测量太阳能光伏阵列的电压、电流和功率、综合负荷频率和电压控制系统的方法研究(Simulink实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

Ceph如何操作底层对象数据

1.基本原理介绍 1.1 ceph中的对象(object) 在Ceph存储中&#xff0c;一切数据最终都会以对象(Object)的形式存储在硬盘&#xff08;OSD&#xff09;上&#xff0c;每个的Object默认大小为4M。 通过rados命令&#xff0c;可以查看一个存储池中的所有object信息&#xff0c;例如…

使用 PyTorch 进行高效图像分割:第 2 部分

一、说明 这是由 4 部分组成的系列的第二部分&#xff0c;旨在使用 PyTorch 中的深度学习技术从头开始逐步实现图像分割。本部分将重点介绍如何实现基线图像分割卷积神经网络&#xff08;CNN&#xff09;模型。 图 1&#xff1a;使用 CNN 运行图像分割的结果。按从上到下的顺序…

【无标题】QT应用编程: QtCreator配置Git版本控制(码云)

QT应用编程: QtCreator配置Git版本控制(码云) 感谢&#xff1a;DS小龙哥的文章&#xff0c;这篇主要参考小龙哥的内容。 https://cloud.tencent.com/developer/article/1930531?areaSource102001.15&traceIdW2mKALltGu5f8-HOI8fsN Qt Creater 自带了git支持。但是一直没…

Github下载任意版本的VsCode

下载历史版本VsCode(zip) 下载链接由三部分组成&#xff1a; 固定部分commit idVSCode-win32-x64-版本号.zip 固定部分&#xff1a; https://vscode.cdn.azure.cn/stable/ Commit id&#xff1a; 打开 vscode的GitHub&#xff1a;[https://github.com/microsoft/vscode/r…

echarts 柱状图-折线图-饼图的基础使用

上图示例图表展示相关配置&#xff1a; var myChart echarts.init(this.$refs.firstMain);myChart.setOption({legend: { // 图例设置top: "15%",type: "scroll",orient: "vertical",//图例列表的布局朝向。left: "right",pageIconCo…

记录一个编译TubeTK时的报错:at_check问题

在使用如下命令安装TubeTK的cuda_nms时&#xff0c;报了一个错误&#xff0c;记录一下这个错误和解决办法 (base) redmeryredmery:~/Desktop/MOT/TubeTK/post_processing/nms$ python setup.py build_ext --inplace因为这个命令是在/home/redmery/Desktop/MOT/TubeTK/install/…
最新文章