android 自定义view初级

来源:互联网 发布:教师个人网络研修计划 编辑:程序博客网 时间:2024/05/24 06:31

android自定义view简单流程

①首先自定义view的属性,在res/values/下建立一个xml文件,在里面定义我们的属性和声明我们的样式

例子:

      <attr name="titleText" format="string"/>

      <attr name="titleTextColor" format="color"/>

      <attr name="titleTextSize" format="dimension"/>


      <declare-styleable name="CustomTitleView">

           <attr name="titleText"/>

           <attr name="titleTextColor"/>

           <attr name="titleTextSize"/>

       <declare-styleable> 

在这里我们定义了字体,颜色,字体大小,format是指该属性的取值类型:

     一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag

②接下来我们要在布局中声明我们自定义的View

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

          xmlns:tools="http://schemas.android.com/tools"  

         xmlns:custom="http://schemas.android.com/apk/res/com.example.customview01"  

          android:layout_width="match_parent"  

  1.      android:layout_height="match_parent" >

      <com.example.customview.view.CustomTitleView  //包名加上自定义的名字

           android:layout_width="200dp"

           android:layout_height="100dp"

           custom:titleText="3712"  

           custom:titleTextColor="#ff0000"  

           custom:titleTextSize="40sp" />

    一定要引入 xmlns:custom="http://schemas.android.com/apk/res/com.example.customview01

③在View的构造方法中,获得我们自定义的样式

  1.          * 文本 
  2.      */  
  3.     private String mTitleText;  
  4.     /** 
  5.      * 文本的颜色 
  6.      */  
  7.     private int mTitleTextColor;  
  8.     /** 
  9.      * 文本的大小 
  10.      */  
  11.     private int mTitleTextSize;  
  12.   
  13.     /** 
  14.      * 绘制时控制文本绘制的范围 
  15.      */  
  16.     private Rect mBound;  
  17.     private Paint mPaint;  
  18.   
  19.     public CustomTitleView(Context context, AttributeSet attrs)  
  20.     {  
  21.         this(context, attrs, 0);  
  22.     }  
  23.   
  24.     public CustomTitleView(Context context)  
  25.     {  
  26.         this(context, null);  
  27.     }  
  28.   
  29.     /** 
  30.      * 获得我自定义的样式属性 
  31.      *  
  32.      * @param context 
  33.      * @param attrs 
  34.      * @param defStyle 
  35.      */  
  36.     public CustomTitleView(Context context, AttributeSet attrs, int defStyle)  
  37.     {  
  38.         super(context, attrs, defStyle);  
  39.         /** 
  40.          * 获得我们所定义的自定义样式属性 
  41.          */  
  42.         TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0);  
  43.         int n = a.getIndexCount();  
  44.         for (int i = 0; i < n; i++)  
  45.         {  
  46.             int attr = a.getIndex(i);  
  47.             switch (attr)  
  48.             {  
  49.             case R.styleable.CustomTitleView_titleText:  
  50.                 mTitleText = a.getString(attr);  
  51.                 break;  
  52.             case R.styleable.CustomTitleView_titleTextColor:  
  53.                 // 默认颜色设置为黑色  
  54.                 mTitleTextColor = a.getColor(attr, Color.BLACK);  
  55.                 break;  
  56.             case R.styleable.CustomTitleView_titleTextSize:  
  57.                 // 默认设置为16sp,TypeValue也可以把sp转化为px  
  58.                 mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(  
  59.                         TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));  
  60.                 break;  
  61.   
  62.             }  
  63.   
  64.         }  
  65.         a.recycle();  
  66.   
  67.         /** 
  68.          * 获得绘制文本的宽和高 
  69.          */  
  70.         mPaint = new Paint();  
  71.         mPaint.setTextSize(mTitleTextSize);  
  72.         // mPaint.setColor(mTitleTextColor);  
  73.         mBound = new Rect();  
  74.         mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);  
  75.   
  76.     }  




      重写了3个构造方法,默认的布局文件调用的是两个参数的构造方法,所以记得让所有的构造调用我们的三个参数的构造,

   我们在三个参数的构造中获得自定义属性。

④我们重写onDraw,onMesure调用系统提供的:

  1. @Override  
  2.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  
  3.     {  
  4.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  5.     }  
  6.   
  7.     @Override  
  8.     protected void onDraw(Canvas canvas)  
  9.     {  
  10.         mPaint.setColor(Color.YELLOW);  
  11.         canvas.drawRect(00, getMeasuredWidth(), getMeasuredHeight(), mPaint);  
  12.   
  13.         mPaint.setColor(mTitleTextColor);  
  14.         canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);  
  15.     }  





0 0
原创粉丝点击