Recently do an effect: in the phone settings, "about the phone" inside add an item to show the current phone CPU usage curve! The implementation effect is as follows:
The first thing about mobile phone is the effect I want to achieve! Today's how this curve view (Cpu_speedcurve_view) is implemented!
The first thing to note is the following points:
(1) because I designed the Cpu_speedcurve_view not only to show the dynamic curve, but also need to use TextView to display some CPU-related information! So, I choose Cpu_speedcurve_view inherit a viewgroup, here I choose is framelayout
(3) How to draw the curve?
Here I draw with canvas drawpath in the Cpu_speedcurve_view public void draw (canvas canvas). The specific code is as follows:
public void Draw (canvas canvas) {super.draw (canvas);//mpaint.setcolor (Coordinatecolor);//mpaint.setstrokewidth ( MSTROKESIZE*2);//canvas.drawpath (M_path_coordinate, Mpaint),//canvas.cliprect (5, 5), if (Flag_start) {if (m_ Path_0 = = NULL && m_path_1 = = null) {//start_run_cputracker_to_show_curve ();} Else{mpaint.setcolor (Curvecolor); Mpaint.setstrokewidth (mstrokesize); if (m_path_0! = null) {//LOG.D ("Speedcurve", " Cpu_speedcurve_view Draw (M_path! = null) "), if (!m_path_0.isempty ()) {Canvas.drawpath (m_path_0, Mpaint);}} if (m_path_1! = null) {//LOG.D ("Speedcurve", "Cpu_speedcurve_view draw (M_path! = null)"), if (!m_path_1. IsEmpty ()) {Canvas.drawpath (m_path_1, Mpaint);}}}}
From the above code can see, here actually have two path, from above can see just a curve! Why is there a two path here?
Private path m_path_0;private path m_path_1;
I did define two paths, because the curve length of a path is not wireless and will always overflow! So I designed two curves (Path), and when the m_path_0 is used for a period of time, it starts another curve m_path_1, which overlaps the display until the m_path_1 length exceeds the width of the view display, and the m_path_0 is cleared off. The whole mechanism of the operation is this repeated.
(2) for the dynamic display of the curve, my approach is to define a handler timed automatic sending information to update the CPU running data, and finally with invalidate (); To refresh the curve:
Private Handler Mhandler = new Handler () {@Override public void Handlemessage (Message msg) {SWI TCH (msg.what) {case 000:m_cputracker.update (); Totalcpupercent = M_cputracker.gettotalcpupercent (); int h = view_h-2;//int Cpupercent_po = (int) (h * totalcpupercent); I F (Flag_num < +) {if (m_path_0 = = null) {m_path_0 = new path (); M_path_0.moveto (view_w+4, h-totalcpupercent);} if (Flag_num >) {if (m_path_1 = = null) {m_path_1 = new path (); M_path_1.moveto (view_w+4, h-totalcpupercent);}} else{m_path_1 = null;}} else if (Flag_num <) {m_path_0 = null;if (m_path_1 = = null) {m_path_1 = new path (); M_path_1.moveto (View_w+4, H-totalc pupercent);}} else if (Flag_num <) {if (m_path_0 = = null) {m_path_0 = new path (); M_path_0.moveto (view_w+4, h-totalcpupercent);}} Else{flag_num = 0;} if (m_path_0! = null) {M_path_0.lineto (view_w+4, h-totalcpupercent); Matrix.settranslate ( -4,0); M_path_0.transform ( Matrix);} if (m_path_1! = null) {M_path_1.lineto (view_w+4, H -totalcpupercent); Matrix.settranslate ( -4,0); m_path_1.transform (matrix);} LOG.D ("Speedcurve", "Cpu_speedcurve_view handlemessage msg.what=000 flag_num=" +flag_num);//LOG.D ("Speedcurve", " Cpu_speedcurve_view handlemessage totalcpupercent= "+totalcpupercent+" view_h= "+view_h+" getCurCpuFreq () = "+Cpu_info _manager.getcurcpufreq ()); if (Flag_start) {mhandler.sendemptymessagedelayed (000,300); invalidate (); flag_num++;} Else{stop_run_cputracker_to_show_curve ();} Break Case 111:break; } } };
From the above code can know: first get the current percentage of CPU usage, and then by this percentage to calculate the height of the curve! Finally, through the path of the MoveTo to complete the drawing of the curve save.
(3) How to get CPU usage?
Use Android to provide the Processcputracker.
M_cputracker = new Processcputracker (false);
In fact, inside the Processcputracker is the/proc/stat to read the CPU information (user Time/nice Time/sys time/idle time/iowait time, etc.) to calculate the percentage of utilization!
(4) Finally remember in this inside protected void Ondetachedfromwindow () stop to run on it!
So the problem is basically solved! If you want to implement the above, you need to define a preferencegroup:
Preferencegroup mcpustatuspref = (preferencegroup) findpreference ("Cpu_key");
In the layout:cpu_curve_preference can be layout into the above effect!
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:minheight= "85dip" android:gravity= "center_vertical" Android:paddingS Tart= "@*android:dimen/preference_item_padding_side" android:paddingend= "? Android:attr/scrollbarsize" Android: Background= "? Android:attr/selectableitembackground" android:paddingtop= "6dip" android:paddingbottom= "6dip" > &L T Relativelayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layou t_weight= "1" android:paddingtop= "6dip" android:paddingbottom= "6dip" > <textview Andro Id:id= "@+android:id/title" android:text= "@string/xunhu_setting_cpu_info" android:layout_width= "Wrap_ Content "android:layout_height=" Wrap_content "android:singleline=" true "Android:textappea Rance= "? Android:attr/textappearanCemedium "android:ellipsize=" marquee "android:fadingedge=" horizontal "/> <textview Android:id= "@android: Id/summary" android:layout_width= "Wrap_content" android:layout_height= "W Rap_content "android:text=" @string/usage_type_cpu_foreground "android:layout_below=" @android: id/titl E "android:layout_alignstart=" @android: Id/title "android:visibility=" Gone "Android:textap Pearance= "? Android:attr/textappearancesmall" android:textcolor= "? Android:attr/textcolorsecondary" an droid:maxlines= "4"/> </RelativeLayout> <com.android.settings.widget.cpu_speedcurve_view android: Layout_width= "Wrap_content" android:layout_height= "match_parent" android:minwidth= "180dip" Android:ba ckground= "@drawable/cpu_curve_bg" android:layout_marginbottom= "8dip" android:layout_margintop= "8dip" > <textview AndroiD:id= "@+android:id/cpu_speedcurve_view_title" android:text= "@string/xunhu_setting_cpu_info_util" Android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:singleline= "true" android:ellipsize= "marquee" android:fadingedge= "Horizontal"/> </com.android.settings.widget.cpu_speedcurve_view></linearlayout >
Android CPU Usage curve effect implementation!