关于android:android绘图-androidgraphics包

3次阅读

共计 10336 个字符,预计需要花费 26 分钟才能阅读完成。

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)

本文转自 https://blog.51cto.com/xyzlmn/816808,如有侵权,请分割删除。

正文完
 0