自定义控件

<转 http://www.eoeandroid.com/forum.php?mod=viewthread&tid=306320>


  很多公司都会问到自定义控件的问题,自定义控件无非三种方式:1.继承view 2.继承原有的控件 在原有控件的基础上进行修改 3.组件拼装组合 后两种

暂且不说,原理的问题都需要知道,在此,小弟我分享通过继承View,而写的TextView,千万别小看他奥 ,这个可是基础,再次我也写的很详细

   第一步:自定义属性 自定义属性就是在res/values/目录下创建一个attrs的xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <declare-styleable name="MyTestView"> 
  4.         <attr name="textColor" format="color"/> 
  5.         <attr name="textSize" format="dimension"/> 
  6.         <attr name="text" format="string"></attr>
  7.         <attr name="background" format="reference|color"/>
  8.     </declare-styleable> 
  9.     
  10. </resources>
复制代码
<declare-styleable name="MyTestView"> 这个标签是必须要有的里面的name就是我自定义控件的名称,前面的标签就是一个字母也不能错
    里面的format http://blog.chinaunix.net/uid-20484604-id-2980081.html这是参考
     第二部:写一个类继承View
  1. package com.example.demo;

  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.FontMetrics;
  8. import android.graphics.Typeface;
  9. import android.text.TextPaint;
  10. import android.util.AttributeSet;
  11. import android.view.View;

  12. //
  13. public class MyTextView extends View
  14. {
  15.     //定义画笔
  16.     private TextPaint mPaint = null;
  17.     //要绘制的内容
  18.     private String text = null;
  19.     //背景
  20.     private int background;
  21.     
  22.     public MyTextView(Context context, AttributeSet attrs)
  23.     {
  24.         super(context, attrs);
  25.         //绘制文本  Paint.ANTI_ALIAS_FLAG这个属性来帮助消除锯齿使其边缘更平滑
  26.         mPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
  27.         //获取属性的数组 在自定义view的代码中引入自定义属性,修改构造函数
  28.         //context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置

  29.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTestView);
  30.         
  31.         //获取设置的颜色和文字大小 这里的获取其实就是在main.xml中获取的见下面代码,至于 0XFFFFFFFF ,14是defalut默认值
  32.         int textColor = a.getColor(R.styleable.MyTestView_textColor, 0XFFFFFFFF);
  33.         float textSize = a.getDimension(R.styleable.MyTestView_textSize, 14);
  34.         text = a.getString(R.styleable.MyTestView_text);
  35.         mPaint.setTextSize(textSize);
  36.         mPaint.setColor(textColor);
  37.         background = Color.WHITE;
  38.         a.recycle(); //记住这一部千万不要忘记 
  39.     }
  40.     
  41.     @Override
  42.     protected void onDraw(Canvas canvas)
  43.     {
  44.         super.onDraw(canvas);
  45.         //清屏幕
  46.         canvas.drawColor(background);
  47.         mPaint.setTypeface(Typeface.DEFAULT);//特别强调一点这个方法是设置字体样式Typeface.DEFAULT:默认字体 Typeface.DEFAULT_BOLD:加粗字体 Typeface.MONOSPACE:monospace字体。
  48.        // Typeface.SANS_SERIF:sans字体   // Typeface.SERIF:serif字体
  49.             
  50.         //需要根据文字长度控制换行
  51.         //测量文字的长度
  52.         float  trueSize = mPaint.measureText(text);
  53.         //设置一行像素
  54.         int lineW = this.getWidth();
  55.         //计算共绘制多少行
  56.         int count = 0;
  57.         if(trueSize>(int)trueSize){
  58.             count =((int)trueSize/lineW)+1;
  59.         }else{
  60.             count = (int)trueSize/lineW;
  61.         }
  62.         FontMetrics fm = mPaint.getFontMetrics();       
  63.         int hSize = (int)Math.ceil(fm.descent - fm.ascent)/2;
  64.         //开始索引
  65.         int startIndex = 0;
  66.         String lineText = null;
  67.         for(int i=0;i<=count;i++){
  68.             //计算坐标
  69.             int x =2;
  70.             int y = hSize*i+2+30;
  71.             if(i!=count){
  72.                 int endIndex=subStringLength(text, lineW, mPaint);
  73.                 lineText = text.substring(0,endIndex);
  74.                 canvas.drawText(lineText, x, y, mPaint);
  75.                 text = text.substring(endIndex);
  76.             }else{
  77.                 lineText = text.substring(startIndex);
  78.                 canvas.drawText(lineText, x, y, mPaint);
  79.                 break;
  80.             }
  81.         }
  82.                    

  83.                        
  84.     }
  85.     public static int subStringLength(String value,int maxL,TextPaint paint){
  86.         int currentInext = 0;
  87.         for(int i=0;i<value.length();i++){
  88.             //获取一个字符
  89.             String temp = value.substring(0,i+1);
  90.             //测量真是长度
  91.             float valueLength = paint.measureText(temp);
  92.             if(valueLength > maxL){
  93.                 currentInext = i;
  94.                 break;
  95.             }
  96.         }
  97.         return currentInext;
  98.     }
  99.     
  100. }
复制代码
之后在就可以拿来用了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:mview = "http://schemas.android.com/apk/res/com.example.demo"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >

   <com.example.demo.MyTextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       mview:textColor="@android:color/darker_gray"
       mview:textSize="20sp"
       mview:text="hello world"
       />

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值