Android自定義View--01

源碼下載

Android自定義View實現很簡單

繼承View,重寫構造函數、onDraw,(onMeasure)等函數。

如果自定義的View需要有自定義的屬性,需要在values下建立attrs.xml。在其中定義你的屬性。

在使用到自定義View的xml佈局文件中需要加入xmlns:前綴="http://schemas.android.com/apk/res/你的自定義View所在的包路徑".

在使用自定義屬性的時候,使用前綴:屬性名,如my:textColor="#FFFFFFF"。

實例:

 

  1. package demo.view.my;   
  2. import android.content.Context;   
  3. import android.content.res.TypedArray;   
  4. import android.graphics.Canvas;   
  5. import android.graphics.Color;   
  6. import android.graphics.Paint;   
  7. import android.graphics.Paint.Style;   
  8. import android.util.AttributeSet;   
  9. import android.view.View;   
  10. /**  
  11.  * 這個是自定義的TextView.  
  12.  * 至少需要重載構造方法和onDraw方法  
  13.  * 對於自定義的View如果沒有自己獨特的屬性,可以直接在xml文件中使用就可以了  
  14.  * 如果含有自己獨特的屬性,那麼就需要在構造函數中獲取屬性文件attrs.xml中自定義屬性的名稱  
  15.  * 並根據需要設定默認值,放在在xml文件中沒有定義。  
  16.  * 如果使用自定義屬性,那麼在應用xml文件中需要加上新的schemas,  
  17.  * 比如這裏是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"  
  18.  * 其中xmlns後的“my”是自定義的屬性的前綴,res後的是我們自定義View所在的包  
  19.  * @author Administrator  
  20.  *  
  21.  */  
  22. public class MyView extends View {   
  23.        
  24.     Paint mPaint; //畫筆,包含了畫幾何圖形、文本等的樣式和顏色信息   
  25.     public MyView(Context context) {   
  26.         super(context);   
  27.            
  28.     }   
  29.        
  30.     public MyView(Context context, AttributeSet attrs){   
  31.         super(context, attrs);   
  32.         mPaint = new Paint();   
  33.         //TypedArray是一個用來存放由context.obtainStyledAttributes獲得的屬性的數組   
  34.         //在使用完成後,一定要調用recycle方法   
  35.         //屬性的名稱是styleable中的名稱+“_”+屬性名稱   
  36.         TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);   
  37.         int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默認值,放置未指定   
  38.         float textSize = array.getDimension(R.styleable.MyView_textSize, 36);   
  39.         mPaint.setColor(textColor);   
  40.         mPaint.setTextSize(textSize);   
  41.            
  42.         array.recycle(); //一定要調用,否則這次的設定會對下次的使用造成影響   
  43.     }   
  44.        
  45.     public void onDraw(Canvas canvas){   
  46.         super.onDraw(canvas);   
  47.         //Canvas中含有很多畫圖的接口,利用這些接口,我們可以畫出我們想要的圖形   
  48.         //mPaint = new Paint();   
  49.         //mPaint.setColor(Color.RED);   
  50.         mPaint.setStyle(Style.FILL); //設置填充   
  51.         canvas.drawRect(1010100100, mPaint); //繪製矩形   
  52.            
  53.         mPaint.setColor(Color.BLUE);   
  54.         canvas.drawText("我是被畫出來的"10120, mPaint);   
  55.     }   
  56. }  

 

相應的屬性文件:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="MyView">  
  4.         <attr name="textColor" format="color"/>  
  5.         <attr name="textSize" format="dimension"/>  
  6.     </declare-styleable>  
  7. </resources>  

 

在佈局文件中的使用:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.               xmlns:my="http://schemas.android.com/apk/res/demo.view.my"    
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8.        
  9.     <demo.view.my.MyView  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"    
  12.         my:textColor="#FFFFFFFF"    
  13.         my:textSize="22dp"  
  14.         />  
  15. </LinearLayout>  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章