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 @Override28 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, top45 + 50, p);46 }47 }48 }49 }
17 16 25 34 43 52 61 70