搜档网
当前位置:搜档网 › Android UI开发专题(一) 之界面设计

Android UI开发专题(一) 之界面设计

Android UI开发专题(一) 之界面设计
Android UI开发专题(一) 之界面设计

近期很多网友对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(https://www.sodocs.net/doc/e113231711.html,pressFormat 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移动平台开发 学院:信息工程学院 班级: 学号: 姓名:

实训内容: 一.1.Andriod的简介 Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统、中间件、用户界面和应用软件组成,号称是首个为移动终端打造的真正开放和完整的移动软件。目前,最新版本为Android 2.4 Gingerbread 和Android 3.0 Honeycomb。 Android是基于Linux开放性内核的操作系统,是Google公司在2007年11月5日公布的手机操作系统。 Android早期由原名为"Android"的公司开发,谷歌在2005年收购"Android.Inc"后,继续对Android系统开发运营,它采用了软件堆层(software stack,又名软件叠层)的架构,主要分为三部分。底层Linux内核只提供基本功能,其他的应用软件则由各公司自行开发,部分程序以Java编写。2011年初数据显示,仅正式上市两年的操作系统Android已经超越称霸十年的塞班系统,使之跃居全球最受欢迎的智能手机平台。现在,Android系统不但应用于智能手机,也在平板电脑市场急速扩张,在智能MP4方面也有较大发展。采用Android系统主要厂商包括台湾的HTC,(第一台谷歌的手机G1由HTC生产代工)美国摩托罗拉,SE等,中国大陆厂商如:魅族(M9),华为、中兴、联想、蓝魔等。 2.Android构架图 二.1软件下载 Android SDK,网址是https://www.sodocs.net/doc/e113231711.html,. JDK的下载地址https://www.sodocs.net/doc/e113231711.html,/javase/downloads/widget/jdk6.jsp。Eclipse的下载网址是https://www.sodocs.net/doc/e113231711.html,/downloads/ 2.Android开发环境搭建

第4章 Android用户界面设计

视图组件的使用模式 常用组件 高级组件 提示框与警告对话框

就是Android应用程序的开发过程。一般过程是先通过XML布局文件或Java代码创建界面布局,设定组件显示样式,随后获取UI组件对象,并处理组件事件响应。 视图组件的定义 资源的访问 生成视图组件资源标识 视图组件的引用 视图组件的事件响应 组件的常用属性

1.1视图组件的定义 使用XML布局文件定义视图组件 使用Java代码定义视图组件(不推荐)

1.1视图组件的定义 使用XML布局文件定义视图组件 Android平台为大多数视图组件以及其子类提供了XML标记,可通过XML布局文件中的标记来定义视图组件。XML中的每个元素代表了一个组件,即元素名称对应相应的Java类。

1.1视图组件的定义