Android自訂View之一:初探執行個體

來源:互聯網
上載者:User

Android自訂View實現很簡單

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

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

在使用到自訂View的xml布局檔案中需要加入xmlns:首碼="http://schemas.android.com/apk/res/你的自訂View所在的包路徑".

在使用自訂屬性的時候,使用首碼:屬性名稱,如my:textColor="#FFFFFFF"。

執行個體:

 

view plaincopy to clipboardprint?
  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(10, 10, 100, 100, mPaint); //繪製矩形   
  52.            
  53.         mPaint.setColor(Color.BLUE);   
  54.         canvas.drawText("我是被畫出來的", 10, 120, mPaint);   
  55.     }   
  56. }  

package demo.view.my;<br />import android.content.Context;<br />import android.content.res.TypedArray;<br />import android.graphics.Canvas;<br />import android.graphics.Color;<br />import android.graphics.Paint;<br />import android.graphics.Paint.Style;<br />import android.util.AttributeSet;<br />import android.view.View;<br />/**<br /> * 這個是自訂的TextView.<br /> * 至少需要重載構造方法和onDraw方法<br /> * 對於自訂的View如果沒有自己獨特的屬性,可以直接在xml檔案中使用就可以了<br /> * 如果含有自己獨特的屬性,那麼就需要在建構函式中擷取屬性檔案attrs.xml中自訂屬性的名稱<br /> * 並根據需要設定預設值,放在在xml檔案中沒有定義。<br /> * 如果使用自訂屬性,那麼在應用xml檔案中需要加上新的schemas,<br /> * 比如這裡是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"<br /> * 其中xmlns後的“my”是自訂的屬性的首碼,res後的是我們自訂View所在的包<br /> * @author Administrator<br /> *<br /> */<br />public class MyView extends View {</p><p>Paint mPaint; //畫筆,包含了畫幾何圖形、文本等的樣式和顏色資訊<br />public MyView(Context context) {<br />super(context);</p><p>}</p><p>public MyView(Context context, AttributeSet attrs){<br />super(context, attrs);<br />mPaint = new Paint();<br />//TypedArray是一個用來存放由context.obtainStyledAttributes獲得的屬性的數組<br />//在使用完成後,一定要調用recycle方法<br />//屬性的名稱是styleable中的名稱+“_”+屬性名稱<br />TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);<br />int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供預設值,放置未指定<br />float textSize = array.getDimension(R.styleable.MyView_textSize, 36);<br />mPaint.setColor(textColor);<br />mPaint.setTextSize(textSize);</p><p>array.recycle(); //一定要調用,否則這次的設定會對下次的使用造成影響<br />}</p><p>public void onDraw(Canvas canvas){<br />super.onDraw(canvas);<br />//Canvas中含有很多畫圖的介面,利用這些介面,我們可以畫出我們想要的圖形<br />//mPaint = new Paint();<br />//mPaint.setColor(Color.RED);<br />mPaint.setStyle(Style.FILL); //設定填充<br />canvas.drawRect(10, 10, 100, 100, mPaint); //繪製矩形</p><p>mPaint.setColor(Color.BLUE);<br />canvas.drawText("我是被畫出來的", 10, 120, mPaint);<br />}<br />}<br />

 

相應的屬性檔案:

 

view plaincopy to clipboardprint?
  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>  

<?xml version="1.0" encoding="utf-8"?><br /><resources><br /><declare-styleable name="MyView"><br /><attr name="textColor" format="color"/><br /><attr name="textSize" format="dimension"/><br /></declare-styleable><br /></resources>

 

在布局檔案中的使用:

 

view plaincopy to clipboardprint?
  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>  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.