最近做一個效果:在手機設置裏面“關於手機”裏面添加一項來顯示當前手機cpu使用率的曲線!其實現效果如下圖所示:
上圖關於手機的第一項就是我要實現的效果!今天來講講這個曲線的view(cpu_speedcurve_view)是如何實現的!
首先要注意以下幾點:
(1)由於我設計的cpu_speedcurve_view不僅僅顯示動態的曲線,還需要用textview顯示一些cpu相關信息!所以,我選擇cpu_speedcurve_view繼承一個viewgroup,這裏我選擇的是FrameLayout
(3)曲線的怎麼繪製?
這裏我是在cpu_speedcurve_view的public void draw(Canvas canvas)裏面用Canvas的drawPath來繪製的。具體代碼如下:
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);
}
}
}
}
}
從上面代碼可以看到,這裏居然有兩個Path,從上面的效果圖可以看出只是一條曲線呀!爲什麼這裏有兩個Path呢? private Path m_path_0;
private Path m_path_1;
的確我定義了兩個Path,那是因爲一個Path的曲線長度不是無線的,總是會溢出的!所以我就設計了兩個曲線(Path),當m_path_0使用一段時間後,就啓動另一個曲線m_path_1,使其重疊顯示,直到m_path_1長度超過這個view顯示的寬度時候,把m_path_0給清除掉。整個運行的機制就是這樣反覆。
(2)對於曲線的動態顯示,我的辦法是定義一個Handler定時的自動發送信息來更新cpu運行的數據,最後用invalidate();來刷新曲線:
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;
}
}
};
從上面的代碼可以知道:首先獲取當前cpu使用的百分率,再通過這個百分率來計算出曲線高度!最後通過path的moveTo來完成曲線的繪製保存。
(3)如何獲取cpu的使用率呢?
用android提供的ProcessCpuTracker就可以了。
m_CpuTracker = new ProcessCpuTracker(false);
實際上在ProcessCpuTracker裏面就是在/proc/stat 去讀取cpu的信息(user time /nice time/sys time/idle time/iowait time等)來計算出使用率的百分比的!
(4)最後記得 在這個裏面 protected void onDetachedFromWindow()停止運行就可以了!
就這樣問題就基本解決了!如果要實現如上的效果圖的則需要定義一個PreferenceGroup:
PreferenceGroup mCPUStatusPref = (PreferenceGroup) findPreference("cpu_key");
mCPUStatusPref.setLayoutResource(R.layout.cpu_curve_preference);
在layout: cpu_curve_preference就可以佈局成上面的效果了!<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>