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

然后试一下从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);
  }
  
 }
}


运行效果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值