继承TextView简单画一个尺子

 1 import android.content.Context;
 2 import android.graphics.Canvas;
 3 import android.graphics.Color;
 4 import android.graphics.Paint;
 5 import android.util.AttributeSet;
 6 import android.view.Gravity;
 7 import android.widget.TextView;
 8 
 9 // 给尺子加上刻度。
10 public class CM extends TextView {
11 
12     public CM(Context context) {
13         super(context);
14         init();
15     }
16 
17     public CM(Context context, AttributeSet attrs) {
18         super(context, attrs);
19         init();
20     }
21 
22     private void init() {
23         // 设置文字居底
24         setGravity(Gravity.BOTTOM);
25     }
26 
27     @Override
28     public void draw(Canvas canvas) {
29         super.draw(canvas);
30    
31         // 得到一毫米
32         float mmWidth = ((float) getWidth()) / 10;
33         Paint p = new Paint();
34         p.setColor(Color.WHITE);  // 更改画笔颜色
35         float top = 2;   // 标识 
36         for (int i = 0; i < 10; i++) {
37             if (i % 2 == 0) {
38                 /**
39                  * i * mmWidth,第几个小方块乘以毫米的宽度。
40                  * top,距离上面有一个两毫米的距离。
41                  * i * mmWidth + mmWidth, 右面
42                  * top + 50,  下面
43                  */
44                 canvas.drawRect(i * mmWidth, top, i * mmWidth + mmWidth, top
45                         + 50, p);
46             }
47         }
48     }
49 }
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="com.myruler.MainActivity" >
 7     <com.myruler.CM
 8         android:layout_width="10mm"
 9         android:layout_height="10mm"
10         android:layout_gravity="center_vertical"
11         android:background="#ff0000"
12         android:text="0cm"
13         android:textColor="#ffffffff"
14         android:textSize="10pt" />
16     <com.myruler.CM
17         android:layout_width="10mm"
18         android:layout_height="10mm"
19         android:layout_gravity="center_vertical"
20         android:background="#ff9900"
21         android:text="1cm"
22         android:textColor="#ffffffff"
23         android:textSize="10pt" />
25     <com.myruler.CM
26         android:layout_width="10mm"
27         android:layout_height="10mm"
28         android:layout_gravity="center_vertical"
29         android:background="#cccc00"
30         android:text="2cm"
31         android:textColor="#ffffffff"
32         android:textSize="10pt" />
34     <com.myruler.CM
35         android:layout_width="10mm"
36         android:layout_height="10mm"
37         android:layout_gravity="center_vertical"
38         android:background="#00ff00"
39         android:text="3cm"
40         android:textColor="#ffffffff"
41         android:textSize="10pt" />
43     <com.myruler.CM
44         android:layout_width="10mm"
45         android:layout_height="10mm"
46         android:layout_gravity="center_vertical"
47         android:background="#00cccc"
48         android:text="4cm"
49         android:textColor="#ffffffff"
50         android:textSize="10pt" />
52     <com.myruler.CM
53         android:layout_width="10mm"
54         android:layout_height="10mm"
55         android:layout_gravity="center_vertical"
56         android:background="#00ccff"
57         android:text="5cm"
58         android:textColor="#ffffffff"
59         android:textSize="10pt" />
61     <com.myruler.CM
62         android:layout_width="10mm"
63         android:layout_height="10mm"
64         android:layout_gravity="center_vertical"
65         android:background="#00cc00"
66         android:text="6cm"
67         android:textColor="#ffffffff"
68         android:textSize="10pt" />
70 </LinearLayout>
原文地址:https://www.cnblogs.com/androidsj/p/4476729.html