編輯:關於Android編程
最近做一個效果:在手機設置裡面“關於手機”裡面添加一項來顯示當前手機cpu使用率的曲線!其實現效果如下圖所示:
上圖關於手機的第一項就是我要實現的效果!今天來講講這個曲線的view(cpu_speedcurve_view)是如何實現的!<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ytfPyNKq16LS4tLUz8K8uLXjo7o8L3A+CjxwPqOoMaOp08nT2s7Syei8xrXEY3B1X3NwZWVkY3VydmVfdmlld7K7vfa99s/Uyr62r8ystcTH+s/fo6y7udDo0qrTw3RleHR2aWV3z9TKvtK70KljcHXP4LnY0MXPoqOhy/nS1KOsztLRodTxY3B1X3NwZWVkY3VydmVfdmlld7zMs9DSu7j2dmlld2dyb3Vwo6zV4sDvztLRodTxtcTKx0ZyYW1lTGF5b3V0PC9wPgo8cD6jqDOjqcf6z9+1xNT1w7S75tbGo788L3A+CjxwPiAgICAgICAgICAg1eLA787SysfU2mNwdV9zcGVlZGN1cnZlX3ZpZXe1xHB1YmxpYyB2b2lkIGRyYXcoQ2FudmFzIGNhbnZhcynA78Pm08NDYW52YXO1xGRyYXdQYXRowLS75tbGtcSho77fzOW0+sLryOfPwqO6PC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;"> 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;
(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就可以布局成上面的效果了!
本實例通過MediaPlayer播放一首音樂並通過AudioManager控制手機音頻,關於AudioManager的詳解可參照:Android開發之AudioManag
接觸過自定義控件的開發者一看,笑了,立馬關了網頁。但是…你真的知道怎麼繪制居中文本嗎?我不會?開玩笑,不就是:X=控件寬度/2 - 文本寬度/2;Y=控件高
當我們看到別人的APP裡面或者寫的Demo裡面的一些很牛逼得動畫效果的時候,是否會有那麼一種沖動我要看看它的源代碼到底是怎麼寫的,為毛我就是寫不出這樣的動畫效果呢,為毛我
首先看一下activity返回數據的結構圖以前我們啟動另外一個activity用的是startActivity(Intent intent)方法 而若想打開另外一個act