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(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).

时间: 2024-09-08 18:10:29

Android UI开发专题(一) 之界面设计的相关文章

Android UI开发专题(二) 之绘图基础

在Android UI开发专题(一) 之界面设计中我们介绍了有关Android平台资源使用以及Bitmap相关类的操作,接下来将会以实例的方式给大家演示各种类的用处以及注意点.今天我们继续了解android.graphics包中比较重要的绘图类. 一. android.graphics.Matrix 有关图形的变换.缩放等相关操作常用的方法有: void reset() // 重置一个matrix对象. void set(Matrix src) //复制一个源矩阵,和本类的构造方法 Matrix

Android UI开发专题(四) View自绘控件

  很多时候想要设计漂亮的Android UI,使用Android自带的控件无法满足我们的需要就要考虑自绘控件,在Android界面显示类View,可以通过继承扩展重写相关方法来实现我们的图形绘制. 首先我们需要了解下View类的底层实现,在SDK中我们可以看到View直接继承于Java的基类Object,实现了图形绘制和按键事件Drawable.Callback KeyEvent.Callback的相关方法,我们自绘时主要实现其内部的onDraw方法,相关的界面计算可以重写onMeasure方

Android UI开发专题(三) 各种Drawable

  本次我们主要讲解Android平台下的各种Drawable,这里在SDK的android.graphics.drawable包下面可以看到有各种Drawable类多达十几种,它们到底之间有什么关系和区别呢? 一.AnimationDrawable 顾名思义该类主要表示动画的图形类,可以实现逐帧播放的效果,下面代码示例如下 1. 定义一个cwj_animation.xml 放到res/drawable 目录下,其中定义的属性duration为延时,单位为毫秒,而oneshot属性表示是否仅播放

Android简明开发教程六:用户界面设计

Activity是Android应用用户界面的基本组成部件.但Activity本身并不提供用户界面(User Interface).从程序结构层次上 来说,一个Android应用是类android.app.Application的一个实例, Application中可以包含多个android.app.Activity实例. 每个Activity 带一个Window类,这个类在Android平台上没有提供太多功能,主要可以用来控制标题栏(屏幕顶端).比如设置 UI全屏显示可以使用如下代码: req

Android UI开发 View自绘控件 分享_Android

很多时候想要设计漂亮的Android UI,使用Android自带的控件无法满足我们的需要就要考虑自绘控件,在Android界面显示类View,可以通过继承扩展重写相关方法来实现我们的图形绘制. 首先我们需要了解下View类的底层实现,在SDK中我们可以看到View直接继承于Java的基类Object,实现了图形绘制和按键事件 Drawable.Callback KeyEvent.Callback的相关方法,我们自绘时主要实现其内部的onDraw方法,相关的界面计算可以重写onMeasure方法

Android UI开发 View自绘控件 分享

很多时候想要设计漂亮的Android UI,使用Android自带的控件无法满足我们的需要就要考虑自绘控件,在Android界面显示类View,可以通过继承扩展重写相关方法来实现我们的图形绘制. 首先我们需要了解下View类的底层实现,在SDK中我们可以看到View直接继承于Java的基类Object,实现了图形绘制和按键事件 Drawable.Callback KeyEvent.Callback的相关方法,我们自绘时主要实现其内部的onDraw方法,相关的界面计算可以重写onMeasure方法

Android UI开发神兵利器之设计资源

UI设计资源推荐又来了,纯干货,不注水~ 各种背景资源图库 http://subtlepatterns.com/ ICON资源 https://www.iconfinder.com/ HOLO Color生成器 http://android-holo-colors.com/

Android UI开发详解之模板控件的复用

Android的UI设计一直是Android程序员比较苦恼的一件事,本文主要讲解如何将一些模板类控件进行复用,从而简化UI的开发. 如图: 我们很多程序的界面中,顶部的TopBar是不变的,所以,我们可以做一个公用的控件模板,每次使用时,只要设置相应的参数,就能生成这样一个TopBar. 模板控件实现方法: package com.xys.multiplexedmodule; import android.content.Context; import android.content.res.T

Android UI开发神兵利器之Angrytools

最近很多人在问我,个人App开发者如何去设计UI. 其实这是个人开发者最头痛的问题,搞技术的人,确实没法做到面面俱到,不可能花大量的时间去切图,去做原型设计,去做美工. 当然,虽然我们设计不出那么复杂,精巧的UI,但是简单的东西,我们在没有美工的基础上,通过一些手段,也是可以做的不错的,从本文开始,我们将介绍一些关于Android界面开发的神兵利器,正是这些大神们开发的工具,让Coder也能做出一些不是那么见不得人的设计. Angrytools,我们今天的主角,我想当初作者也是被UI弄的Angr