香煎大白刁:Android 中自定义View的应用(1)

来源:百度文库 编辑:九乡新闻网 时间:2024/04/20 20:04:34
大家好我们今天的教程是在Android 教程中自定义View 的学习,对于初学着来说,他们习惯了Android 传统的页面布局方式,如下代码:view plaincopy to clipboardprint?
 
  1.      
  2.     android:orientation="vertical"    
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="fill_parent"    
  5.     >     
  6.     android:layout_width="fill_parent"      
  7.     android:layout_height="wrap_content"      
  8.     android:text="@string/hello"    
  9.     />     
  10.     
  11.  
  12.     android:orientation="vertical" 
  13.     android:layout_width="fill_parent" 
  14.     android:layout_height="fill_parent" 
  15.     > 
  16.     android:layout_width="fill_parent"   
  17.     android:layout_height="wrap_content"   
  18.     android:text="@string/hello" 
  19.     /> 
  20.    
当然上面的布局方式可以帮助我们完成简单应用的开发了,但是如果你想写一个复杂的应用,这样就有点牵强了,大家不信可以下源码都研究看看,高手写的布局方式,如上面的布局高手通常是这样写的:view plaincopy to clipboardprint?
 
  1.      
  2.      
  3.          
  4.     
  5.  
  6.  
  7.   
  8.    
view plaincopy to clipboardprint?
其中A extends LinerLayout, B extends TextView. 
其中A extends LinerLayout, B extends TextView.为了帮助大家更容易理解,我写了一个简单的Demo ,具体步骤如下:首先新建一个Android 工程 命名为ViewDemo .然后自定义一个View 类,命名为MyView(extends View) .代码如下:
  1. view plaincopy to clipboardprint?  
  2. package com.android.tutor;     
  3. import android.content.Context;     
  4. import android.graphics.Canvas;     
  5. import android.graphics.Color;     
  6. import android.graphics.Paint;     
  7. import android.graphics.Rect;     
  8. import android.graphics.Paint.Style;     
  9. import android.util.AttributeSet;     
  10. import android.view.View;     
  11. public class MyView extends View {     
  12.     private Paint mPaint;     
  13.     private Context mContext;     
  14.     private static final String mString = "Welcome to Mr Wei's blog";     
  15.          
  16.     public MyView(Context context) {     
  17.         super(context);     
  18.          
  19.     }     
  20.     public MyView(Context context,AttributeSet attr)     
  21.     {     
  22.         super(context,attr);     
  23.          
  24.     }     
  25.     @Override    
  26.     protected void onDraw(Canvas canvas) {     
  27.         // TODO Auto-generated method stub     
  28.         super.onDraw(canvas);     
  29.              
  30.         mPaint = new Paint();     
  31.              
  32.         //设置画笔颜色     
  33.         mPaint.setColor(Color.RED);     
  34.         //设置填充     
  35.         mPaint.setStyle(Style.FILL);     
  36.              
  37.         //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标     
  38.         canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);     
  39.              
  40.         mPaint.setColor(Color.BLUE);     
  41.         //绘制文字     
  42.         canvas.drawText(mString, 10, 110, mPaint);     
  43.     }     
  44. }    
  45. package com.android.tutor;  
  46. import android.content.Context;  
  47. import android.graphics.Canvas;  
  48. import android.graphics.Color;  
  49. import android.graphics.Paint;  
  50. import android.graphics.Rect;  
  51. import android.graphics.Paint.Style;  
  52. import android.util.AttributeSet;  
  53. import android.view.View;  
  54. public class MyView extends View {  
  55.  private Paint mPaint;  
  56.  private Context mContext;  
  57.  private static final String mString = "Welcome to Mr Wei's blog";  
  58.    
  59.  public MyView(Context context) {  
  60.   super(context);  
  61.    
  62.  }  
  63.  public MyView(Context context,AttributeSet attr)  
  64.  {  
  65.   super(context,attr);  
  66.    
  67.  }  
  68.  @Override  
  69.  protected void onDraw(Canvas canvas) {  
  70.   // TODO Auto-generated method stub  
  71.   super.onDraw(canvas);  
  72.     
  73.   mPaint = new Paint();  
  74.     
  75.   //设置画笔颜色  
  76.   mPaint.setColor(Color.RED);  
  77.   //设置填充  
  78.   mPaint.setStyle(Style.FILL);  
  79.     
  80.   //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标  
  81.   canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);  
  82.     
  83.   mPaint.setColor(Color.BLUE);  
  84.   //绘制文字  
  85.   canvas.drawText(mString, 10, 110, mPaint);  
  86.  }  
  87. }  
  88.    
  89. 然后将我们自定义的View 加入到main.xml 布局文件中,代码如下:  
  90. view plaincopy to clipboardprint?  
  91.      
  92.     android:orientation="vertical"    
  93.     android:layout_width="fill_parent"    
  94.     android:layout_height="fill_parent"    
  95.     >     
  96.     android:layout_width="fill_parent"      
  97.     android:layout_height="wrap_content"      
  98.     android:text="@string/hello"    
  99.     />     
  100.     android:layout_width="fill_parent"      
  101.     android:layout_height="fill_parent"      
  102. />     
  103.     
  104.  
  105.     android:orientation="vertical" 
  106.     android:layout_width="fill_parent" 
  107.     android:layout_height="fill_parent" 
  108.     > 
  109.     android:layout_width="fill_parent"   
  110.     android:layout_height="wrap_content"   
  111.     android:text="@string/hello" 
  112.     /> 
  113.  android:layout_width="fill_parent"   
  114.     android:layout_height="fill_parent"   
  115. /> 
  116.    
最后执行之,效果如下图: OK,大功告成,今天就写到这里,开始做饭了,老婆孩子等我做饭了,lol~