[问题] 在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