Android UI开发专题

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

近期很多网友对Android用户界面的设计表示很感兴趣,对于AndroidUI开发自绘控件和游戏制作而言掌握好绘图基础是必不可少的。本次专题分10节来讲述,有关OpenGLES相关的可能将放到以后再透露。本次主要涉及以下四个包的相关内容:  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的相关资源均通过该类来管理。

intgetColor(intid)对应res/values/colors.xml

DrawablegetDrawable(intid)对应res/drawable/

XmlResourceParsergetLayout(intid)对应res/layout/

StringgetString(intid)和CharSequencegetText(intid)对应res/values/strings.xml

InputStreamopenRawResource(intid)对应res/raw/

voidparseBundleExtra(StringtagName,AttributeSetattrs,BundleoutBundle)对应res/xml/

String[]getStringArray(intid)res/values/arrays.xml

  float getDimension(int id) res/values/dimens.xml

二、android.graphics.Bitmap

作为位图操作类,Bitmap提供了很多实用的方法,常用的我们总结如下:

booleancompress(Bitmap.CompressFormatformat,intquality,OutputStreamstream)压缩一个Bitmap对象根据相关的编码、画质保存到一个OutputStream中。其中第一个压缩格式目前有JPG和PNG

voidcopyPixelsFromBuffer(Buffersrc)从一个Buffer缓冲区复制位图像素

voidcopyPixelsToBuffer(Bufferdst)将当前位图像素内容复制到一个Buffer缓冲区

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

staticBitmapcreateBitmap(Bitmapsrc)

staticBitmapcreateBitmap(int[]colors,intwidth,intheight,Bitmap.Configconfig)

staticBitmapcreateBitmap(int[]colors,intoffset,intstride,intwidth,intheight,Bitmap.Configconfig)

staticBitmapcreateBitmap(Bitmapsource,intx,inty,intwidth,intheight,Matrixm,booleanfilter)

staticBitmapcreateBitmap(intwidth,intheight,Bitmap.Configconfig)

staticBitmapcreateBitmap(Bitmapsource,intx,inty,intwidth,intheight)

staticBitmapcreateScaledBitmap(Bitmapsrc,intdstWidth,intdstHeight,booleanfilter)//创建一个可以缩放的位图对象

finalintgetHeight()获取高度

finalintgetWidth()获取宽度

finalbooleanhasAlpha()是否有透明通道

voidsetPixel(intx,inty,intcolor)设置某像素的颜色

  int getPixel(int x, int y) 获取某像素的颜色,android开发网提示这里返回的int型是color的定义

三、android.graphics.BitmapFactory

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

staticBitmapdecodeByteArray(byte[]data,intoffset,intlength)//从字节数组创建

staticBitmapdecodeByteArray(byte[]data,intoffset,intlength,BitmapFactory.Optionsopts)

staticBitmapdecodeFile(StringpathName,BitmapFactory.Optionsopts)//从文件创建,路径要写全

staticBitmapdecodeFile(StringpathName)

staticBitmapdecodeFileDescriptor(FileDescriptorfd,RectoutPadding,BitmapFactory.Optionsopts)//从输入流句柄创建

staticBitmapdecodeFileDescriptor(FileDescriptorfd)

staticBitmapdecodeResource(Resourcesres,intid)//从Android的APK文件资源中创建,android123提示是从/res/的drawable中

staticBitmapdecodeResource(Resourcesres,intid,BitmapFactory.Optionsopts)

staticBitmapdecodeResourceStream(Resourcesres,TypedValuevalue,InputStreamis,Rectpad,BitmapFactory.Optionsopts)

staticBitmapdecodeStream(InputStreamis)//从一个输入流中创建

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

Canvas(GLgl)

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

booleanclipPath(Pathpath)

booleanclipPath(Pathpath,Region.Opop)

booleanclipRect(floatleft,floattop,floatright,floatbottom)

booleanclipRect(Rectrect)

booleanclipRect(floatleft,floattop,floatright,floatbottom,Region.Opop)

booleanclipRect(Rectrect,Region.Opop)

booleanclipRect(RectFrect)

booleanclipRect(RectFrect,Region.Opop)

booleanclipRect(intleft,inttop,intright,intbottom)

booleanclipRegion(Regionregion,Region.Opop)

booleanclipRegion(Regionregion)

voidconcat(Matrixmatrix)

voiddrawARGB(inta,intr,intg,intb)

voiddrawArc(RectFoval,floatstartAngle,floatsweepAngle,booleanuseCenter,Paintpaint)

voiddrawBitmap(Bitmapbitmap,Matrixmatrix,Paintpaint)

voiddrawBitmap(int[]colors,intoffset,intstride,floatx,floaty,intwidth,intheight,booleanhasAlpha,Paintpaint)

voiddrawBitmap(Bitmapbitmap,Rectsrc,Rectdst,Paintpaint)

voiddrawBitmap(Bitmapbitmap,floatleft,floattop,Paintpaint)

voiddrawBitmap(int[]colors,intoffset,intstride,intx,inty,intwidth,intheight,booleanhasAlpha,Paintpaint)

voiddrawBitmap(Bitmapbitmap,Rectsrc,RectFdst,Paintpaint)

voiddrawBitmapMesh(Bitmapbitmap,intmeshWidth,intmeshHeight,float[]verts,intvertOffset,int[]colors,intcolorOffset,Paintpaint)

voiddrawCircle(floatcx,floatcy,floatradius,Paintpaint)

voiddrawColor(intcolor)

voiddrawColor(intcolor,PorterDuff.Modemode)

voiddrawLine(floatstartX,floatstartY,floatstopX,floatstopY,Paintpaint)

voiddrawLines(float[]pts,Paintpaint)

voiddrawLines(float[]pts,intoffset,intcount,Paintpaint)

voiddrawOval(RectFoval,Paintpaint)

voiddrawPaint(Paintpaint)

voiddrawPath(Pathpath,Paintpaint)

voiddrawPicture(Picturepicture,RectFdst)

voiddrawPicture(Picturepicture,Rectdst)

voiddrawPicture(Picturepicture)

voiddrawPoint(floatx,floaty,Paintpaint)

voiddrawPoints(float[]pts,intoffset,intcount,Paintpaint)

voiddrawPoints(float[]pts,Paintpaint)

voiddrawPosText(char[]text,intindex,intcount,float[]pos,Paintpaint)

voiddrawPosText(Stringtext,float[]pos,Paintpaint)

voiddrawRGB(intr,intg,intb)

voiddrawRect(RectFrect,Paintpaint)

voiddrawRect(floatleft,floattop,floatright,floatbottom,Paintpaint)

voiddrawRect(Rectr,Paintpaint)

voiddrawRoundRect(RectFrect,floatrx,floatry,Paintpaint)

voiddrawText(Stringtext,intstart,intend,floatx,floaty,Paintpaint)

voiddrawText(char[]text,intindex,intcount,floatx,floaty,Paintpaint)

voiddrawText(Stringtext,floatx,floaty,Paintpaint)

voiddrawText(CharSequencetext,intstart,intend,floatx,floaty,Paintpaint)

voiddrawTextOnPath(Stringtext,Pathpath,floathOffset,floatvOffset,Paintpaint)

voiddrawTextOnPath(char[]text,intindex,intcount,Pathpath,floathOffset,floatvOffset,Paintpaint)

voiddrawVertices(Canvas.VertexModemode,intvertexCount,float[]verts,intvertOffset,float[]texs,inttexOffset,int[]colors,intcolorOffset,short[]indices,intindexOffset,intindexCount,Paintpaint)

staticvoidfreeGlCaches()

booleangetClipBounds(Rectbounds)

finalRectgetClipBounds()

intgetDensity()

DrawFiltergetDrawFilter()

GLgetGL()

intgetHeight()

voidgetMatrix(Matrixctm)

finalMatrixgetMatrix()

intgetSaveCount()

intgetWidth()

booleanisOpaque()

booleanquickReject(Pathpath,Canvas.EdgeTypetype)

booleanquickReject(floatleft,floattop,floatright,floatbottom,Canvas.EdgeTypetype)

booleanquickReject(RectFrect,Canvas.EdgeTypetype)

voidrestore()

voidrestoreToCount(intsaveCount)

finalvoidrotate(floatdegrees,floatpx,floatpy)

voidrotate(floatdegrees)

intsave()

intsave(intsaveFlags)

intsaveLayer(floatleft,floattop,floatright,floatbottom,Paintpaint,intsaveFlags)

intsaveLayer(RectFbounds,Paintpaint,intsaveFlags)

intsaveLayerAlpha(floatleft,floattop,floatright,floatbottom,intalpha,intsaveFlags)

intsaveLayerAlpha(RectFbounds,intalpha,intsaveFlags)

finalvoidscale(floatsx,floatsy,floatpx,floatpy)

voidscale(floatsx,floatsy)

voidsetBitmap(Bitmapbitmap)

voidsetDensity(intdensity)

voidsetDrawFilter(DrawFilterfilter)

voidsetMatrix(Matrixmatrix)

voidsetViewport(intwidth,intheight)

voidskew(floatsx,floatsy)

  void translate(float dx, float dy)

五、android.graphics.Color

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

staticintargb(intalpha,intred,intgreen,intblue)构造一个包含透明对象的颜色

staticintrgb(intred,intgreen,intblue)构造一个标准的颜色对象

staticintparseColor(StringcolorString)解析一种颜色字符串的值,比如传入Color.BLACK

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

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

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

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

voidreset()//重置一个matrix对象。

voidset(Matrixsrc)//复制一个源矩阵,和本类的构造方法Matrix(Matrixsrc)一样

booleanisIdentity()//返回这个矩阵是否定义(已经有意义)

voidsetRotate(floatdegrees)//指定一个角度以0,0为坐标进行旋转

voidsetRotate(floatdegrees,floatpx,floatpy)//指定一个角度以px,py为坐标进行旋转

voidsetScale(floatsx,floatsy)//缩放

voidsetScale(floatsx,floatsy,floatpx,floatpy)//以坐标px,py进行缩放

voidsetTranslate(floatdx,floatdy)//平移

voidsetSkew(floatkx,floatky,floatpx,floatpy)//以坐标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类我们可以理解为画笔、画刷的属性定义,本类常用的方法如下:

voidreset()//重置

voidsetARGB(inta,intr,intg,intb)或voidsetColor(intcolor)均为设置Paint对象的颜色

voidsetAntiAlias(booleanaa)//是否抗锯齿,需要配合voidsetFlags(Paint.ANTI_ALIAS_FLAG)来帮助消除锯齿使其边缘更平滑。

ShadersetShader(Shadershader)//设置阴影,Shader类是一个矩阵对象,如果为NULL将清除阴影。

voidsetStyle(Paint.Stylestyle)//设置样式,一般为FILL填充,或者STROKE凹陷效果。

voidsetTextSize(floattextSize)//设置字体大小

voidsetTextAlign(Paint.Alignalign)//文本对齐方式

TypefacesetTypeface(Typefacetypeface)//设置字体,通过Typeface可以加载Android内部的字体,一般为宋体对于中文,部分ROM可以自己添加比如雅黑等等

  void setUnderlineText(boolean underlineText) //是否设置下划线,需要撇和void setFlags (Paint.UNDERLINE_TEXT_FLAG) 方法。

四、android.graphics.Rect

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

booleancontains(intleft,inttop,intright,intbottom)

booleancontains(intx,inty)

  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方法可以直接指定一个字体名称和样式,比如

staticTypefacecreate(Typefacefamily,intstyle)

staticTypefacecreate(StringfamilyName,intstyle)

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

finalbooleanisBold()

finalbooleanisItalic()

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

staticTypefacecreateFromAsset(AssetManagermgr,Stringpath)

staticTypefacecreateFromFile(Filepath)

staticTypefacecreateFromFile(Stringpath)

  有关Android平台的图形、图像我们在前两节中已经整理出来,下次我们将首先讲述下NinePatch的实例应用。

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

本次我们主要讲解Android平台下的各种Drawable,这里在SDK的android.graphics.drawable包下面可以看到有各种Drawable类多达十几种,它们到底之间有什么关系和区别呢?

一、AnimationDrawable

顾名思义该类主要表示动画的图形类,可以实现逐帧播放的效果,下面代码示例如下

1.定义一个cwj_animation.xml放到res/drawable目录下,其中定义的属性duration为延时,单位为毫秒,而oneshot属性表示是否仅播放一次,内容为:

1<animation-listandroid:id="selected"android:oneshot="false">

2<itemandroid:drawable="@drawable/cwj0"android:duration="30"

/>

3<itemandroid:drawable="@drawable/cwj1"android:duration="30"

/>

4<itemandroid:drawable="@drawable/cwj2"android:duration="30"

/>

5<itemandroid:drawable="@drawable/cwj3"android:duration="30"

/>

6<itemandroid:drawable="@drawable/cwj4"android:duration="30"

/>

7<itemandroid:drawable="@drawable/cwj5"android:duration="30"

/>

8</animation-list>

9

10

       2.在java中调用也很简单

ImageViewimg=(ImageView)findViewById(R.id.cwj_image);//首先声明一个ImageView对象在xml布局文件中

img.setBackgroundResource(R.drawable.cwj_animation);//我们刚才的animation定义的xml文件

AnimationDrawableframeAnimation=(AnimationDrawable)img.getBackground();//构造AnimationDrawable对象

frameAnimation.start()//开始播放动画

3.AnimationDrawable类还提供了一些常用的方法如下:

voidstop()停止

voidaddFrame(Drawableframe,intduration)添加一帧,类似xml中的布局

DrawablegetFrame(intindex)返回某帧的Drawable图形

intgetNumberOfFrames()返回总共动画帧数

booleanisOneShot()是否仅播放一次

  boolean isRunning() 是否正在播放

二、BitmapDrawable

在Android平台中对于缩放、变形的Bitmap对象由BitmapDrawable类表示,其构造方法也很简单,由于该类继承于android.graphics.drawable.Drawable,相对Drawable而言提供了更多的有关位图的操作方法,主要的构造方法如下:

BitmapDrawable()//直接构造一个空的对象,这样方式不推荐使用,SDK标记为deprecated.未来可能无法使用。

BitmapDrawable(Resourcesres)//从资源中构造

BitmapDrawable(Bitmapbitmap)//从Bitmap对象直接构造,但也是不推荐,而是希望用下一种

BitmapDrawable(Resourcesres,Bitmapbitmap)//从bitmap中创建设置初始的分辨率从res中

BitmapDrawable(Stringfilepath)//从具体文件路径构造,也不推荐使用,而是下一种更好

BitmapDrawable(Resourcesres,Stringfilepath)//同上

BitmapDrawable(InputStreamis)//从输入流中构造,同样推荐下面的方法

BitmapDrawable(Resourcesres,InputStreamis)//同上

在BitmapDrawable类中相对于Drawable类主要新增了以下几种方法,均比较实用:

finalBitmapgetBitmap()获取一个Bitmap对象

intgetOpacity()//获取透明度

voidsetAntiAlias(booleanaa)//是否抗锯齿

voidsetTargetDensity(Canvascanvas)//设置目标Canvas密度

  void setTargetDensity(DisplayMetrics metrics)

三、ClipDrawable

ColorDrawable

Drawable

GradientDrawable

InsetDrawable

LayerDrawable

LevelListDrawable

NinePatchDrawable

PaintDrawable

PictureDrawable

RotateDrawable

ScaleDrawable

ShapeDrawable

StateListDrawable

TransitionDrawable

  以上的类型在常见的开发一般较少出现,主要是基类构造使用,Android内部的多个Widget基础控件使用了,感兴趣的网友可以查看开源GIT中的相关内容。

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

      很多时候想要设计漂亮的Android UI,使用Android自带的控件无法满足我们的需要就要考虑自绘控件,在Android界面显示类View,可以通过继承扩展重写相关方法来实现我们的图形绘制。

首先我们需要了解下View类的底层实现,在SDK中我们可以看到View直接继承于Java的基类Object,实现了图形绘制和按键事件Drawable.CallbackKeyEvent.Callback的相关方法,我们自绘时主要实现其内部的onDraw方法,相关的界面计算可以重写onMeasure方法,对于相关的按键可以重载onKeyDown、onKeyUp以及onTouchEvent等,下面android开发网就以一个实例来表示。

publicclasscwjViewextendsView

{

publiccwjView(Contextcontext)

{

this(context,null);

}

publiccwjView(Contextcontext,AttributeSetattrs)

{

this(context,attrs,0);

}

publiccwjView(Contextcontext,AttributeSetattrs,intdefStyle)

{

super(context,attrs,defStyle);

//这里是本类的构造,相关初始化可以在这里添加代码

}

@Override

protectdvoidonDraw(Canvascanvas)

{

super(canvas);

//绘图的关键,可以看到已经包含了一个canvas句柄,可以直接通过我们前面讲到的Canvas类进行相关的操作,完整的例子,大家可以参考AndroidSDK中例子Snake贪食蛇游戏的实现。

}

}

  有关View类的更新,我们直接通过调用invalidate(int l,int r,int r,int b)来更新一个Rect矩形区域,或更新全部,同时在线程中我们使用需要调用postInvalidate来更新界面。

Android UI开发(五)Bitmap和Canvas实例

     在Android UI开发专题的前五节我们讲到的东西主要是基础和理论内容,从本次Android123将通过实例代码来演示,本次主要是Bitmap和Canvas类的使用,根据要求缩放Bitmap对象并返回新的Bitmap对象。centerToFit方法一共有4个参数,返回一个Bitmap类型,第一个参数为原始的位图对象,width和height分别为新的宽和高,而Context是用来加载资源的上下文实例。

1BitmapcenterToFit(Bitmapbitmap,intwidth,intheight,Contextcontext){

2

3  finalintbitmapWidth=bitmap.getWidth();//获取原始bitmap的宽度

4

5  finalintbitmapHeight=bitmap.getHeight();

6

7  if(bitmapWidth<width||bitmapHeight<height){

8

9  intcolor=context.getResources().getColor(R.color.window_background);//从资源读取背景色

10

11  Bitmapcentered=Bitmap.createBitmap(bitmapWidth<width?width:bitmapWidth,

12

13  bitmapHeight<height?height:bitmapHeight,Bitmap.Config.RGB_565);

14

15  centered.setDensity(bitmap.getDensity());

16

17  Canvascanvas=

newCanvas(centered);

18

19  canvas.drawColor(color);//先绘制背景色

20

21  canvas.drawBitmap(bitmap,(width-bitmapWidth)/

2.0f,(height-bitmapHeight)/

2.0f,null);//通过Canvas绘制Bitmap

22

23  bitmap=centered;

24

25  }

26

27  returnbitmap;//返回新的bitmap

28

29  }

30

31

本段代码从Android 2.1开始将会应用在全新的Home主屏上,同时相关的ImageView的适应屏幕大小的setScaleType(fitCenter) 方法类似,仅仅是我们制定了未来的大小。

GraphableButton类实现Android UI开发

      从Android 1.6开始,系统设置中的电池使用记录提供了一种简单的自绘Button按钮演示-GraphableButton类,通过GraphableButton我们可以很清晰的了解到前几次Android123讲到的UI开发要点。

1  publicclassGraphableButtonextendsButton{//从Button类继承

2

3  privatestaticfinalStringTAG=

"GraphableButton";

4

5  staticPaint[]sPaint=

newPaint[2];//定义两种颜色

6

7  static{

8

9  sPaint[0]=

newPaint();

10

11  sPaint[0].setStyle(Paint.Style.FILL);

12

13  sPaint[0].setColor(0xFF0080FF);

14

15  sPaint[1]=

newPaint();

16

17  sPaint[1].setStyle(Paint.Style.FILL);

18

19  sPaint[1].setColor(0xFFFF6060);

20

21  }

22

23  double[]mValues;

24

25  publicGraphableButton(Contextcontext,AttributeSetattrs){

26

27  super(context,attrs);

28

29  }

30

31  publicvoidsetValues(double[]values,doublemaxValue){//设置显示范围,下文提到

32

33  mValues=values.clone();

34

35  for(inti=

0;i<values.length;i++){

36

37  mValues/=maxValue;

38

39  }

40

41  }

42

43  @Override

44

45  publicvoidonDraw(Canvascanvas){//重写onDraw直接绘制

46

47  Log.i(TAG,"onDraw:w="

+getWidth()+

",h="

+getHeight());

48

49  intxmin=getPaddingLeft();

50

51  intxmax=getWidth()-getPaddingRight();

52

53  intymin=getPaddingTop();

54

55  intymax=getHeight()-getPaddingBottom();

56

57  intstartx=xmin;

58

59  for(inti=

0;i<mValues.length;i++){

60

61  intendx=xmin+(int)(mValues*(xmax-xmin));

62

63  canvas.drawRect(startx,ymin,endx,ymax,sPaint);//通过canvas绘制范围

64

65  //该方法原型drawRect(floatleft,floattop,floatright,floatbottom,Paintpaint)

66

67  startx=endx;

68

69  }

70

71  super.onDraw(canvas);

72

73  }

74

75  }

76

调用方法很简单,和普通的Button没有什么区别,这里我们仅仅多定义了setValues方法,Android开发网提醒网哟注意布局文件xml中如何定义,在最下文

1  privateGraphableButtonmButtons;

2

3  mButtons=(GraphableButton)findViewById(R.id.button0);

4

5  mButtons.setOnClickListener(this);//设置一个按下事件监听

6

7  mButtons.setVisibility(View.INVISIBLE);//设置当前按钮不可见

8

9  mButtons.setText("android123.com欢迎您");

10

11  mButtons.setValues(0,100);

12

13  mButtons.setVisibility(View.VISIBLE);//设置按钮可见

14

下面在layout.xml中如何写呢,这里要写上自己程序完整的package name才能正确被adt识别,相关的具体定义如下:

1  android:id="@+id/button7"

2

3  android:layout_width="fill_parent"

4

5  android:layout_height="0dp"

6

7  android:layout_marginLeft="4dp"

8

9  android:layout_marginRight="4dp"

10

11  android:layout_marginBottom="4dp"

12

13  android:layout_weight="1"

/>

相关推荐