1 ImportAndroid.content.Context;2 ImportAndroid.graphics.Canvas;3 ImportAndroid.graphics.Color;4 ImportAndroid.graphics.Paint;5 ImportAndroid.util.AttributeSet;6 Importandroid.view.Gravity;7 ImportAndroid.widget.TextView;8 9 //add a scale to the ruler. Ten Public classCmextendsTextView { One A PublicCM (Context context) { - Super(context); - init (); the } - - PublicCM (Context context, AttributeSet attrs) { - Super(context, attrs); + init (); - } + A Private voidinit () { at //Set Text base - setgravity (gravity.bottom); - } - - @Override - Public voidDraw (canvas canvas) { in Super. Draw (canvas); - to //Get a millimeter + floatMmwidth = ((float) getwidth ())/10; -Paint p =NewPaint (); theP.setcolor (Color.White);//Change Brush Color * floattop = 2;//Identification $ for(inti = 0; I < 10; i++) {Panax Notoginseng if(i% 2 = = 0) { - /** the * I * mmwidth, the number of small squares multiplied by the width of millimeters. + * Top, with a distance of two mm from the top. A * I * mmwidth + mmwidth, right the * top + 50, below + */ -Canvas.drawrect (i * mmwidth, top, I * mmwidth +mmwidth, Top $+ 50, p); $ } - } - } the}
1 <LinearLayoutxmlns: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.CM8 Android:layout_width= "10mm"9 Android:layout_height= "10mm"Ten android:layout_gravity= "Center_vertical" One Android:background= "#ff0000" A Android:text= "0cm" - Android:textcolor= "#ffffffff" - android:textsize= "10pt" /> - <Com.myruler.CM - Android:layout_width= "10mm" - Android:layout_height= "10mm" + android:layout_gravity= "Center_vertical" - Android:background= "#ff9900" + Android:text= "1cm" A Android:textcolor= "#ffffffff" at android:textsize= "10pt" /> - <Com.myruler.CM - Android:layout_width= "10mm" - Android:layout_height= "10mm" - android:layout_gravity= "Center_vertical" in Android:background= "#cccc00" - Android:text= "2cm" to Android:textcolor= "#ffffffff" + android:textsize= "10pt" /> the <Com.myruler.CM * Android:layout_width= "10mm" $ Android:layout_height= "10mm"Panax Notoginseng android:layout_gravity= "Center_vertical" - Android:background= "#00ff00" the Android:text= "3cm" + Android:textcolor= "#ffffffff" A android:textsize= "10pt" /> + <Com.myruler.CM - Android:layout_width= "10mm" $ Android:layout_height= "10mm" $ android:layout_gravity= "Center_vertical" - Android:background= "#00cccc" - Android:text= "4cm" the Android:textcolor= "#ffffffff" - android:textsize= "10pt" /> the <Com.myruler.CM - Android:layout_width= "10mm" Wu Android:layout_height= "10mm" - android:layout_gravity= "Center_vertical" About Android:background= "#00ccff" $ Android:text= "5cm" - Android:textcolor= "#ffffffff" - android:textsize= "10pt" /> A <Com.myruler.CM + Android:layout_width= "10mm" the Android:layout_height= "10mm" - android:layout_gravity= "Center_vertical" $ Android:background= "#00cc00" the Android:text= "6cm" the Android:textcolor= "#ffffffff" the android:textsize= "10pt" /> - </LinearLayout>
Inherit TextView simply draw a ruler