Implementation of the cpu usage curve of android !, Android Curve
Recently, I made an effect: Add "about mobile phone" in the mobile phone settings to display the cpu usage curve of the current mobile phone! Shows the implementation result:
The first item about mobile phones is what I want to achieve! Let's talk about how this curve view (cpu_speedcurve_view) is implemented today!
Pay attention to the following points:
(1) because the cpu_speedcurve_view I designed not only shows the dynamic curve, but also needs to use textview to Display cpu-related information! Therefore, I chose cpu_speedcurve_view to inherit a viewgroup. Here I chose FrameLayout.
(3) how to draw a curve?
Here, I used the drawPath of Canvas to draw it in the public void draw (canvas Canvas) of cpu_speedcurve_view. The 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(10, 10, 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 code above, we can see that there are actually two paths, from which we can see that it is just a curve! Why are there two paths here?
private Path m_path_0;private Path m_path_1;
Indeed, I have defined two paths because the curve length of a Path is not wireless and always overflows! So I designed two curves (Path). When m_path_0 is used for a period of time, another curve m_path_1 is started to show overlapping until the length of m_path_1 exceeds the width of the view display, clear m_path_0. The entire operation mechanism is repeated in this way.
(2) For the dynamic display of curves, I can define a Handler's timed automatic sending information to update cpu running data, and finally use invalidate (); to refresh the curve:
private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 000: m_CpuTracker.update();TotalCpuPercent = m_CpuTracker.getTotalCpuPercent();int h = view_h - 2;//int CpuPercent_po = (int) (h * TotalCpuPercent);if(flag_num < 400){if(m_path_0 == null){m_path_0 = new Path();m_path_0.moveTo(view_w+4, h - TotalCpuPercent);}if(flag_num > 200){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 < 600){m_path_0 = null;if(m_path_1 == null){m_path_1 = new Path();m_path_1.moveTo(view_w+4, h - TotalCpuPercent);}}else if(flag_num < 800){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; } } };
As you can see from the code above: first obtain the percentage of the current cpu usage, and then calculate the curve height through this percentage! Finally, the curve is drawn and saved through moveTo in the path.
(3) How can I get the cpu usage?
You can use ProcessCpuTracker provided by android.
m_CpuTracker = new ProcessCpuTracker(false);
In fact, ProcessCpuTracker reads cpu information (user time/nice time/sys time/idle time/iowait time) in/proc/stat to calculate the percentage of cpu usage!
(4) In the end, remember to stop running protected void onDetachedFromWindow!
In this way, the problem is basically solved! To implement the above, you need to define a PreferenceGroup:
PreferenceGroup mCPUStatusPref = (PreferenceGroup) findPreference("cpu_key"); mCPUStatusPref.setLayoutResource(R.layout.cpu_curve_preference);
In layout: cpu_curve_preference, the above effect can be displayed!
<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:paddingStart="@*android:dimen/preference_item_padding_side" android:paddingEnd="?android:attr/scrollbarSize" android:background="?android:attr/selectableItemBackground" android:paddingTop="6dip" android:paddingBottom="6dip"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingTop="6dip" android:paddingBottom="6dip"> <TextView android: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:textAppearance="?android:attr/textAppearanceMedium" android:ellipsize="marquee" android:fadingEdge="horizontal" /> <TextView android:id="@android:id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/usage_type_cpu_foreground" android:layout_below="@android:id/title" android:layout_alignStart="@android:id/title" android:visibility="gone" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="?android:attr/textColorSecondary" android:maxLines="4" /> </RelativeLayout> <com.android.settings.widget.cpu_speedcurve_view android:layout_width="wrap_content" android:layout_height="match_parent" android:minWidth="180dip" android:background="@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>