我的android绘图学习笔记(二)

来源:互联网 发布:linux 打包apk 编辑:程序博客网 时间:2024/05/25 16:03

然后试一下从ArrayList里面读数据,画折线图

package com.test;import java.util.ArrayList;import java.util.Iterator;import android.app.Activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.os.Bundle;import android.view.View;public class testPaint extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  MyView myview = new MyView(this);  setContentView(myview); } private class MyView extends View {  @Override  protected void onDraw(Canvas canvas) {   super.onDraw(canvas);   canvas.drawColor(Color.WHITE);   Paint paint =new Paint();   paint.setAntiAlias(true);   paint.setColor(Color.BLACK);   Paint paint1=new Paint();   paint1.setColor(Color.RED);   canvas.drawLine(30, 160, 300, 160, paint);   canvas.drawLine(30, 160, 30, 30, paint);   Path path=new Path();   path.moveTo(30, 30);   path.lineTo(25,35);   path.lineTo(30, 25);   path.lineTo(35, 35);   canvas.drawPath(path, paint);      Path path1=new Path();   path1.moveTo(300, 160);   path1.lineTo(295,155);   path1.lineTo(305, 160);   path1.lineTo(295, 165);   canvas.drawPath(path1, paint);      paint.setTextSize(12);   canvas.drawText(getResources().getString(R.string.date), 280, 180,     paint);   canvas.drawText(getResources().getString(R.string.product), 1, 40, paint);   ArrayList<myPoint> list=new ArrayList();   myPoint po = new myPoint(30,160);   list.add(po);   myPoint po1 = new myPoint(100,120);   list.add(po1);   myPoint po2 = new myPoint(220,80);   list.add(po2);   int lastX = 30;   int lastY = 160;   Iterator<myPoint> it = list.iterator();   for(int i=0;i<list.size();i++)   {   po=it.next();       canvas.drawLine(lastX, lastY, po.m_x, po.m_y, paint);   lastX = po.m_x;   lastY = po.m_y;   }  }  public MyView(Context context) {   super(context);  }   }}


运行效果