近期很多网友对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 foraccessing application resources, such as raw asset files, colors, drawables, media or other other files in thepackage, plus important device configuration details (orientation, input types,etc.) that affect how the application may behave.平时用到的二进制源文件raw、颜色colors、图形drawables和多媒体文件media的相关资源均通过该类来管理。
intgetColor(int id) 对应res/values/colors.xml
DrawablegetDrawable(int id) 对应res/drawable/
XmlResourceParsergetLayout(int id) 对应res/layout/
StringgetString(int id) 和CharSequence getText(int id) 对应res/values/strings.xml
InputStreamopenRawResource(int id) 对应res/raw/
void parseBundleExtra(String tagName, AttributeSet attrs, Bundle outBundle) 对应res/xml/
String[]getStringArray(int id) res/values/arrays.xml
floatgetDimension(int id) res/values/dimens.xml
二、android.graphics.Bitmap
作为位图操作类,Bitmap提供了很多实用的方法,常用的我们总结如下:
booleancompress(Bitmap.CompressFormat format, int quality, OutputStream stream) 压缩一个Bitmap对象根据相关的编码、画质保存到一个OutputStream中。其中第一个压缩格式目前有JPG和PNG
voidcopyPixelsFromBuffer(Buffer src) 从一个Buffer缓冲区复制位图像素
voidcopyPixelsToBuffer(Buffer dst) 将当前位图像素内容复制到一个Buffer缓冲区
我们看到创建位图对象createBitmap包含了6种方法在目前的Android 2.1 SDK中,当然他们使用的是API Level均为1,所以说从Android1.0 SDK开始就支持了,所以大家可以放心使用。
staticBitmap createBitmap(Bitmap src)
staticBitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config)
staticBitmap createBitmap(int[] colors, int offset, int stride, int width, intheight, Bitmap.Config config)
staticBitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrixm, boolean filter)
staticBitmap createBitmap(int width, int height, Bitmap.Config config)
staticBitmap createBitmap(Bitmap source, int x, int y, int width, int height)
staticBitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, booleanfilter) //创建一个可以缩放的位图对象
final intgetHeight() 获取高度
final intgetWidth() 获取宽度
finalboolean hasAlpha() 是否有透明通道
voidsetPixel(int x, int y, int color) 设置某像素的颜色
intgetPixel(int x, int y) 获取某像素的颜色,android开发网提示这里返回的int型是color的定义
三、android.graphics.BitmapFactory
作为Bitmap对象的I/O类,BitmapFactory类提供了丰富的构造Bitmap对象的方法,比如从一个字节数组、文件系统、资源ID、以及输入流中来创建一个Bitmap对象,下面本类的全部成员,除了decodeFileDescriptor外其他的重载方法都很常用。
staticBitmap decodeByteArray(byte[] data, int offset, int length) //从字节数组创建
staticBitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Optionsopts)
staticBitmap decodeFile(String pathName, BitmapFactory.Options opts) //从文件创建,路径要写全
staticBitmap decodeFile(String pathName)
staticBitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding,BitmapFactory.Options opts) //从输入流句柄创建
staticBitmap decodeFileDescriptor(FileDescriptor fd)
staticBitmap decodeResource(Resources res, int id) //从Android的APK文件资源中创建,android123提示是从/res/的drawable中
staticBitmap decodeResource(Resources res, int id, BitmapFactory.Options opts)
static BitmapdecodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad,BitmapFactory.Options opts)
staticBitmap decodeStream(InputStream is) //从一个输入流中创建
staticBitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)
四、android.graphics.Canvas
从J2ME MIDLET时我们就知道Java提供了Canvas类,而目前在Android平台中,它主要任务为管理绘制过程,The Canvas classholds the "draw" calls. To draw something, you need 4 basiccomponents: 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(Bitmapbitmap)
Canvas(GLgl)
同时Canvas类的一些字段保存着重要的绘制方法定义,比如Canvas.HAS_ALPHA_LAYER_SAVE_FLAG保存时需要alpha层,对于Canvas类提供的方法很多,每个都很重要,下面我们一一作介绍
booleanclipPath(Path path)
booleanclipPath(Path path, Region.Op op)
booleanclipRect(float left, float top, float right, float bottom)
booleanclipRect(Rect rect)
booleanclipRect(float left, float top, float right, float bottom, Region.Op op)
booleanclipRect(Rect rect, Region.Op op)
booleanclipRect(RectF rect)
booleanclipRect(RectF rect, Region.Op op)
booleanclipRect(int left, int top, int right, int bottom)
booleanclipRegion(Region region, Region.Op op)
booleanclipRegion(Region region)
voidconcat(Matrix matrix)
voiddrawARGB(int a, int r, int g, int b)
voiddrawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,Paint paint)
voiddrawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
voiddrawBitmap(int[] colors, int offset, int stride, float x, float y, int width,int height, boolean hasAlpha, Paint paint)
voiddrawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
voiddrawBitmap(Bitmap bitmap, float left, float top, Paint paint)
voiddrawBitmap(int[] colors, int offset, int stride, int x, int y, int width, intheight, boolean hasAlpha, Paint paint)
voiddrawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
voiddrawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, intvertOffset, int[] colors, int colorOffset, Paint paint)
voiddrawCircle(float cx, float cy, float radius, Paint paint)
voiddrawColor(int color)
voiddrawColor(int color, PorterDuff.Mode mode)
voiddrawLine(float startX, float startY, float stopX, float stopY, Paint paint)
voiddrawLines(float[] pts, Paint paint)
voiddrawLines(float[] pts, int offset, int count, Paint paint)
void drawOval(RectFoval, Paint paint)
voiddrawPaint(Paint paint)
voiddrawPath(Path path, Paint paint)
voiddrawPicture(Picture picture, RectF dst)
voiddrawPicture(Picture picture, Rect dst)
voiddrawPicture(Picture picture)
voiddrawPoint(float x, float y, Paint paint)
voiddrawPoints(float[] pts, int offset, int count, Paint paint)
voiddrawPoints(float[] pts, Paint paint)
voiddrawPosText(char[] text, int index, int count, float[] pos, Paint paint)
voiddrawPosText(String text, float[] pos, Paint paint)
voiddrawRGB(int r, int g, int b)
voiddrawRect(RectF rect, Paint paint)
voiddrawRect(float left, float top, float right, float bottom, Paint paint)
voiddrawRect(Rect r, Paint paint)
voiddrawRoundRect(RectF rect, float rx, float ry, Paint paint)
voiddrawText(String text, int start, int end, float x, float y, Paint paint)
voiddrawText(char[] text, int index, int count, float x, float y, Paint paint)
voiddrawText(String text, float x, float y, Paint paint)
voiddrawText(CharSequence text, int start, int end, float x, float y, Paint paint)
voiddrawTextOnPath(String text, Path path, float hOffset, float vOffset, Paintpaint)
voiddrawTextOnPath(char[] text, int index, int count, Path path, float hOffset,float vOffset, Paint paint)
voiddrawVertices(Canvas.VertexMode mode, int vertexCount, float[] verts, intvertOffset, float[] texs, int texOffset, int[] colors, int colorOffset, short[]indices, int indexOffset, int indexCount, Paint paint)
static voidfreeGlCaches()
booleangetClipBounds(Rect bounds)
final RectgetClipBounds()
intgetDensity()
DrawFiltergetDrawFilter()
GL getGL()
intgetHeight()
voidgetMatrix(Matrix ctm)
final MatrixgetMatrix()
intgetSaveCount()
intgetWidth()
booleanisOpaque()
booleanquickReject(Path path, Canvas.EdgeType type)
booleanquickReject(float left, float top, float right, float bottom, Canvas.EdgeTypetype)
booleanquickReject(RectF rect, Canvas.EdgeType type)
voidrestore()
voidrestoreToCount(int saveCount)
final voidrotate(float degrees, float px, float py)
voidrotate(float degrees)
int save()
int save(intsaveFlags)
intsaveLayer(float left, float top, float right, float bottom, Paint paint, intsaveFlags)
intsaveLayer(RectF bounds, Paint paint, int saveFlags)
intsaveLayerAlpha(float left, float top, float right, float bottom, int alpha, intsaveFlags)
intsaveLayerAlpha(RectF bounds, int alpha, int saveFlags)
final voidscale(float sx, float sy, float px, float py)
void scale(floatsx, float sy)
voidsetBitmap(Bitmap bitmap)
voidsetDensity(int density)
voidsetDrawFilter(DrawFilter filter)
voidsetMatrix(Matrix matrix)
voidsetViewport(int width, int height)
voidskew(float sx, float sy)
voidtranslate(float dx, float dy)
五、android.graphics.Color
有关Android平台上表示颜色的方法有很多种,Color提供了常规主要颜色的定义比如Color.BLACK和Color.GREEN等等,我们平时创建时主要使用以下静态方法
static intargb(int alpha, int red, int green, int blue) 构造一个包含透明对象的颜色
static intrgb(int red, int green, int blue) 构造一个标准的颜色对象
static intparseColor(String colorString) 解析一种颜色字符串的值,比如传入Color.BLACK
本类返回的均为一个整形类似 绿色为0xff00ff00,红色为0xffff0000。我们将这个DWORD型看做AARRGGBB,AA代表Aphla透明色,后面的就不难理解,每个分成WORD整好为0-255。
Android UI开发专题(一) 之界面设计
最新推荐文章于 2021-05-27 15:03:47 发布