[問題] 在layout上繪圖 不能刷新

作者: iphone5566s (哀鳳最終款)   2016-10-14 17:38:06
我有用到achartengine這個library
class變數
List<double[]> x = new ArrayList<double[]>(); // 點的x坐標
List<double[]> y = new ArrayList<double[]>(); // 點的y坐標
double[] xaxi = {};
double[] yaxi = {};
接notification
private void setHandler(){
GlobalService.handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg){
super.handleMessage(msg);
if(msg.what==1){
Bundle data = msg.getData();
notification = data.getString("Notification");
TempText.setText(notification+ "°C");
// 數值X,Y坐標值輸入
xaxi = insertElement(xaxi,xaxi.length,xaxi.length);
x.add(xaxi);
double newNotification = Double.parseDouble(notification);
yaxi = insertElement(yaxi,newNotification,yaxi.length);
y.add(yaxi);
點擊按鈕刷圖
private View.OnClickListener onWriteClickListener = new
View.OnClickListener(){
@Override
public void onClick(View v) {
drawPicture();
}
};
畫圖
public void drawPicture(){
LinearLayout drawLayout =
(LinearLayout)findViewById(R.id.gridLinearLayout);
// drawLayout.removeAllViews();
String[] titles = new String[] { "溫度曲線"}; // 定義折線的名稱
XYMultipleSeriesDataset dataset = buildDatset(titles, x, y); // 儲存座
標值;
int[] colors = new int[] { Color.BLACK};// 折線的顏色
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE}; // 折線點
的形狀
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles,
true);
setChartSettings(renderer, "Average Temperature", "Time",
"Temperature", 0, 12, 0, 25, Color.BLACK);// 定義折線圖
View chart = ChartFactory.getLineChartView(this, dataset, renderer);
drawLayout.addView(chart,new LayoutParams(LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
}
問題一(按鈕更新):
我點按鈕之後,只會出現第一次的座標,也就是"一點"
我如果把XY都放進去按鈕裡面更新的話,每按一次的確可以點出下一點
為什麼我把變數放出去之後(為了讓不點按鈕的時候也能做紀錄),就變成無法刷新了
* X跟Y ARRAY確實有在更新,但圖只有最原始的一點
問題二(接收notifiy更新):
我把畫圖放在Notifiy的話
情況就跟問題一一樣,不會做更新
也就是目前只有把所有變數都放到按鈕啟動才能畫線
作者: ginnyhuang (ginnyhuang)   2016-10-18 13:39:00
有任何錯誤或例外訊息嗎?更新 UI 要在 UI thread?確認一下handler是跑在哪個thread,試試runOnUiThread

Links booklink

Contact Us: admin [ a t ] ucptt.com