How Android draws a histogram-------custom view

Source: Internet
Author: User
Tags drawtext



The effect of the histogram on the sliding of the Seekbar is realized:

Graphic effect:




To customize the view's code:

Package Com.example.coustomviewdemo;import Android. R.color;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rect;import Android.graphics.paint.align;import Android.graphics.paint.fontmetrics;import Android.util.attributeset;import Android.view.view;public Class Statscsview extends View {public Statscsview (context context) {super (context);//TODO Auto-generated constructor Stubinit (context, NULL);} Public Statscsview (context context, AttributeSet Attrs) {Super (context, attrs);//TODO Auto-generated constructor Stubinit (context, attrs);} Axis Axis brush: Private Paint axislinepaint;//axis horizontal interior dashed brush private paint hlinepaint;//Draw text Brush private paint titlepaint;//Rectangle Brush Style information for the histogram private paint recpaint;private void init (context context, AttributeSet attrs) {axislinepaint = new Paint (); Hlinepaint = new Paint (); titlepaint = new Paint (); recpaint = new Paint (); Axislinepaint.setcolor (Color.dkgray); Hlinepaint.setcolor(Color.ltgray); Titlepaint.setcolor (Color.Black);} 7 private int[] THISYEAR;//7 bar Private int[] lastyear;/** * With the new own data need to redraw the view sub-class.  * * Called when the main thread refreshes the control: * This.invalidate (); The meaning of the expiration.  * This.postinvalidate (); A method call that can update a view by a child thread. * * *///updata this year datapublic void Updatethisdata (int[] thisdata) {thisyear = Thisdata;//this.invalidate ();//meaning of expiration.  This.postinvalidate (); A method call that can update a view by a child thread. }//updata last year datapublic void Updatelastdata (int[] lastdata) {lastyear = Lastdata;//this.invalidate ();//meaning of expiration.  This.postinvalidate (); A method call that can update a view by a child thread. }private string[] ytitlesstrings = new string[]{"80000", "60000", "40000", "20000", "0"};p rivate string[] xtitles = new string[]{"1", "2", "3", "4", "5", "6", "7"}, @Overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method Stubsuper.ondraw (canvas); int width = getwidth (); int height = getheight ();//1 draw coordinate lines: canvas.drawline (+, x, +, Axi00;//height of the left peripheral: int hperheight = Leftheight/4;hlinepaint.settextalign (Align.center); for (int i=0;i<4;i++) { Canvas.drawline (20+i*hperheight, width-10, 20+i*hperheight, hlinepaint);} 3 draw Y-week coordinates fontmetrics Metrics =titlepaint.getfontmetrics (); int descent = (int) metrics.descent; Titlepaint.settextalign (Align.right); for (int i=0;i<ytitlesstrings.length;i++) {Canvas.drawtext (yTitlesStrings [i], 20+i*hperheight+descent, titlepaint);} 4 plot X week do coordinate int xaxislength = Width-110;int Columcount = Xtitles.length+1;int step = xaxislength/columcount;for (int i= 0;i<columcount-1;i++) {Canvas.drawtext (xtitles[i], 100+step* (i+1), titlepaint);} 5 Draw Rectangle if (thisyear! = null && thisyear.length >0) {int thiscount = thisyear.length;for (int i=0;i<thiscount ; i++) {int value = Thisyear[i];int num = 8-value/10000; Recpaint.setcolor (0XFF1078CF); Rect rect = new rect (); rect.left = + Step * (i+1)-10;rect.right = + Step * (i+1) + 10;//current relative height: int RH = (lef Theight * num)/8; rect.top = RH + 20;rect.bottom = canvas.drawrect (rect, recpaint);}} if (lastyear! = null && lastyear.length >0) {int thiscount = lastyear.length;for (int i=0;i<thiscount;i++) { int value = Lastyear[i];int num = 8-value/10000; Recpaint.setcolor (0xffaa1122); Rect rect = new rect (); rect.left = + Step * (i+1)-10;rect.right = + Step * (i+1) + 10;//current relative height: int RH = (lef Theight * num)/8; rect.top = RH + 20;rect.bottom =; Canvas.drawrect (rect, recpaint);}}}

Activity:

Package Com.example.coustomviewdemo;import Android.app.activity;import Android.os.bundle;import Android.widget.seekbar;import Android.widget.seekbar.onseekbarchangelistener;public class StatscsActivity extends Activity Implementsonseekbarchangelistener {private SeekBar seekbar;private Statscsview statscsview;public Statscsactivity () {//TODO auto-generated constructor stub} @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.sycts); SeekBar = ( SeekBar) This.findviewbyid (r.id.seekbar); Statscsview = (Statscsview) This.findviewbyid (R.ID.STATSCSVIEW1);// Seekerbarseekbar.setonseekbarchangelistener (this);}  Private int[] lastData0 = new int[] {70000, 10000, 20000, 40000, 50000,80000, 40000};p rivate int[] thisData0 = new int[] {40000, 10000, 10000, 20000, 30000,50000, 30000};p rivate int[] lastData1 = new int[] {70000, 60000, 60000, 40000, 5000 0,80000, 80000};p rivate int[] thisData1 = new INt[] {40000, 30000, 30000, 20000, 30000,50000, 30000};p rivate int[] lastData2 = new int[] {70000, 50000, 70000, 80000, 80000,80000, 70000};p rivate int[] thisData2 = new int[] {40000, 10000, 40000, 40000, 30000,40000, 10000};p rivate int[] LASTDATA3 = new int[] {70000, 80000, 70000, 40000, 50000,80000, 40000};p rivate int[] thisData3 = new int[] {10000, 1000 0, 10000, 20000, 30000,10000, 30000}; @Overridepublic void onprogresschanged (SeekBar SeekBar, int progress,boolean Fromus ER) {//TODO auto-generated method Stubint cc = Progress/4;switch (cc) {case 0:statscsview.updatethisdata (LASTDATA0); STA Tscsview.updatelastdata (THISDATA0); Break;case 1:statscsview.updatethisdata (LASTDATA1); Statscsview.updatelastdata (thisData1); Break;case 2:statscsview.updatethisdata (LASTDATA2); Statscsview.updatelastdata (THISDATA2); Break;case 3:statscsview.updatethisdata (LASTDATA3); Statscsview.updatelastdata (thisData3); break;default:break;}} @Overridepublic void Onstarttrackingtouch (SeekBar SeekBar) {//TODO auto-generated method stub} @Overridepublic void Onstoptrackingtouch (SeekBar SeekBar) {//TODO auto-generated method stub }}


Main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <seekbar android:id= "@+id/seekbar" android:layout_width= "Match_parent" android:layout _height= "wrap_content" android:max= "/> <linearlayout android:orientation=" Horizontal "android:layout_width=" Match_parent "android:layout_height=" Wrap_content "android:layout_m arginleft= "10DP" android:gravity= "center" > <textview android:layout_width=            "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "center" android:text= "1"/> <textview android:layout_width= "0DP" android:layou            t_height= "Wrap_content"android:layout_weight= "1" android:gravity= "center" android:text= "2"/> <textview Android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" Android oid:gravity= "Center" android:text= "3"/> <textview android:layout_width= "0d            P "android:layout_height=" Wrap_content "android:layout_weight=" 1 "android:gravity=" center " android:text= "4"/> <textview android:layout_width= "0DP" Android:layout_h            eight= "Wrap_content" android:layout_weight= "1" android:gravity= "Center" android:text= "5"            /> <textview android:layout_width= "0DP" android:layout_height= "Wrap_content"             android:layout_weight= "1" android:gravity= "center" android:text= "6"/> <textview Android:layout_wIdth= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "center" android:text= "7"/> <textview android:layout_width= "0DP" Android:            layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "center" android:text= "8"            /> <textview android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "center" android:text= "9"/> <textv Iew android:layout_width= "0DP" android:layout_height= "Wrap_content" Android:layout_weigh t= "1" android:gravity= "center" android:text= "ten"/> <textview android:layou T_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "center "Android:text= "One"/> <textview android:layout_width= "0DP" android:layout_height= "WRAP_"                    Content "android:layout_weight=" 1 "android:gravity=" center "android:text="/> </LinearLayout> <com.example.coustomviewdemo.statscsview android:id= "@+id/statscsview 1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "/></linearlayout>




How Android draws a histogram-------custom view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.