Many times in 1.Android to draw a custom control, you need to use the paint and canvas of the two classes.
2.:
3. Directly on the code:
1 Public classBatteryviewextendsView {2 3 PrivatePaint mbatterypait;4 PrivatePaint Mpowerpaint;5 Private floatMbatterystroke = 2.0f;6 7 /**8 * High width of the screen9 * Ten * @paramContext One */ A Private intMeasurewidth; - Private intMeasureheight; - the /** - * Battery Parameters - * - * @paramContext + */ - Private floatMbatteryheight = 30.0f;//Battery Height + Private floatMbatterywidth = 60.0f;//the width of the battery A Private floatMcapheight = 15.0f; at Private floatMcapwidth = 5.0f; - - /** - * Battery charge - * - * @paramContext in */ - Private floatmpowerpadding = 5.0f; to Private floatMpowerheight = Mbatteryheight-Mbatterystroke +-mpowerpadding; - Private floatMpowerwidth = Mbatterywidth-mbatterystroke-mpowerpadding the* 2;//total width of the battery body * Private floatMPower =0f; $ Panax Notoginseng /** - * Rectangle the */ + PrivateRECTF MBATTERYRECTF; A PrivateRECTF MCAPRECTF; the PrivateRECTF MPOWERRECTF; + - PublicBatteryview (Context context) { $ Super(context); $ Initview (); - } - the PublicBatteryview (Context context, AttributeSet attrs) { - Super(context, attrs);Wuyi Initview (); the } - Wu PublicBatteryview (context context, AttributeSet attrs,intDefstyle) { - Super(context, attrs, defstyle); About Initview (); $ } - - Private voidInitview () { - /** A * Set the battery brush + */ theMbatterypait =NewPaint (); - Mbatterypait.setcolor (color.gray); $ mbatterypait.setstrokewidth (mbatterystroke); the Mbatterypait.setstyle (style.stroke); theMbatterypait.setantialias (true); the /** the * Power Brush - */ inMpowerpaint =NewPaint (); the Mpowerpaint.setcolor (color.red); the Mpowerpaint.setstyle (Style.fill); About mpowerpaint.setstrokewidth (mbatterystroke); theMpowerpaint.setantialias (true); the /** the * Set the battery rectangle + */ -MBATTERYRECTF =NewRECTF (mcapwidth, 0, Mcapwidth +Mbatterywidth, the mbatteryheight);Bayi the /** the * Set the battery cover rectangle - */ - theMCAPRECTF =NewRECTF (0, MBATTERYHEIGHT/2-MCAPHEIGHT/2, theMcapwidth, MBATTERYHEIGHT/2 + MCAPHEIGHT/2); the the /** - * Set the power of the rectangle the */ the theMPOWERRECTF =NewRECTF (Mbatterywidth + mcapwidth-mpowerpadding94-Mpowerwidth, mpowerpadding, Mbatterywidth +Mcapwidth the-Mpowerpadding, Mbatteryheight-mpowerpadding); the the }98 About @Override - protected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {101Measureheight =measurespec.getsize (heightmeasurespec);102Measurewidth =measurespec.getsize (widthmeasurespec);103 104 setmeasureddimension (Widthmeasurespec, heightmeasurespec); the }106 107 @Override108 protected voidOnDraw (canvas canvas) {109 Super. OnDraw (canvas); the Canvas.save ();111Canvas.translate (MEASUREWIDTH/2, MEASUREHEIGHT/2); theCanvas.drawroundrect (MBATTERYRECTF, 2.0f, 2.0f, mbatterypait);113Canvas.drawroundrect (MCAPRECTF, 2.0f, 2.0f, mbatterypait); the Canvas.drawrect (MPOWERRECTF, mpowerpaint); the Canvas.restore (); the }117}
Show this battery shape in XML:
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 android:orientation= "vertical"6 Tools:context=". Mainactivity " >7 8 <Com.example.battery.view.BatteryView9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Match_parent" /> One A </LinearLayout>
Android draw a battery out (paint and canvas)