Android 创建自定义View


创建自定义的View能够从根本上塑造你的应用程序的外观,你可以通过创建自定义视图的方式去满足用户独特的需求。你可以继承View类或者是SurfaceView类。View类提供了一个Canvas对象,你可以去使用这个对象的很多画图的方法以及Paint对象去绘制你的自定义视图。当然你可以通过覆盖screen touch, key press等的UI事件,对这些事件进行响应,与用户进行交互。SurfaceView类提供了一个Surface对象用来支持使用后台独立线程来对用户自定义视图进行绘制的这么一个核心的对象,并且可以使用openGL技术进行3D绘图。主要用于用户自定义视图更新频繁或者是显示一些复杂的3D效果的场景,在游戏开发当中经常使用。本篇博客我们介绍继承View类的自定义视图,在后面的博客当中,我们将为大家讲解继承SurfaceView的自定义View的构建方法。

步骤一、创建一个新的可视界面

View类默认展现100像素*100像素的区域,如果需要改变这个视图的大小,你需要去重写onMeasure()和onDraw()方法。onDraw()方法用来在Canvas上绘图,onMeasure()方法用来计算在给定的边界情况下,我们自定义的视图的高度和宽度各是多少。Skeleton Code:

  1. public class MyView extends View {  
  2.   
  3.     public MyView(Context context) {  
  4.         super(context);  
  5.     }  
  6.   
  7.     public MyView(Context context, AttributeSet attrs, int defStyle) {  
  8.         super(context, attrs, defStyle);  
  9.     }  
  10.   
  11.     public MyView(Context context, AttributeSet attrs) {  
  12.         super(context, attrs);  
  13.     }  
  14.       
  15.     @Override  
  16.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  17.         int measuredHeight = measureHeight(heightMeasureSpec);  
  18.         int measuredWidth = measureWidth(widthMeasureSpec);  
  19.         //Must make this call to setMeasuredDimension   
  20.         //or you will cause a runtime exception when the control is laid out   
  21.         setMeasuredDimension(measuredWidth, measuredHeight);  
  22.     }  
  23.   
  24.     private int measureWidth(int widthMeasureSpec) {  
  25.         int specMode = MeasureSpec.getMode(widthMeasureSpec);  
  26.         int specSize = MeasureSpec.getSize(widthMeasureSpec);  
  27.         //calculate the width   
  28.         return specSize;  
  29.     }  
  30.   
  31.     private int measureHeight(int heightMeasureSpec) {  
  32.         int specMode = MeasureSpec.getMode(heightMeasureSpec);  
  33.         int specSize = MeasureSpec.getSize(heightMeasureSpec);  
  34.         //calculate the height   
  35.         return specSize;  
  36.     }  
  37.       
  38.     @Override  
  39.     protected void onDraw(Canvas canvas) {  
  40.         //draw your user interface   
  41.     }  
  42.   
  43. }  

注意在我们重写的onMeasure()方法当中,必须调用setMeasuredDimension()方法。不然的话,有的自定义视图在使用的时候将会抛出运行时异常。

步骤二、绘制你的自定义视图

onDraw()方法是奇迹发生的地方。Android提供了一系列的工具帮助我们开发者使用种类繁多的Paint(画笔)对象在Canvas(画布)上绘制我们希望的视图。Canvas对象提供了一些辅助的方法用来绘制一些原生的2D图像,比如说:圆形,直线,矩形,文本以及图形图像等等。有了这些辅助的方法和辅助的工具,你绘制的视图的复杂性和详细情况仅仅取决于屏幕的大小以及处理器的渲染能力。

注意:在Android平台之上写出高效代码的最重要的技巧就是避免对象的重复创建和回收。对于任何在onDraw()方法当中创建的对象,只要屏幕一刷新,此对象就会被回收。所以把onDraw()方法中所用到的对象最好都定义为成员变量,在构造器当中可以对其初始化。

  1. protected void onDraw(Canvas canvas) {  
  2.     //get the size of the control based on the last call to onMeasure()   
  3.     int height = getMeasuredHeight();  
  4.     int width = getMeasuredWidth();  
  5.       
  6.     //Find the center   
  7.     int px = width/2;  
  8.     int py = height/2;    
  9.       
  10.     //create the new paint brushes   
  11.     //note:For efficiency this should be done in the view's constructor   
  12.     Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  13.     mTextPaint.setColor(Color.WHITE);  
  14.       
  15.     //define the string   
  16.     String displayText = "Hello World!";  
  17.       
  18.     //measure the width of the string   
  19.     float textWidth = mTextPaint.measureText(displayText);  
  20.       
  21.     canvas.drawText(displayText, px - textWidth/2, py, mTextPaint);  
  22.     super.onDraw(canvas);  
  23. }  
  • 1
  • 2
  • 下一页

相关内容