canvas教程

Android自定义组件系列【9】Canvas绘制折线图(2)

字号+ 作者:H5之家 来源:H5之家 2016-11-11 11:00 我要评论( )

package com.example.testcanvasdraw;import java.util.ArrayList;import java.util.List;import java.util.Random;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color

 

package com.example.testcanvasdraw; import java.util.ArrayList; import java.util.List; import java.util.Random; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.View; /** * * @author 阳光小强 * */ public class MyView extends View { private int XPoint = 60; private int YPoint = 260; private int XScale = 8; // 刻度长度 private int YScale = 40; private int XLength = 380; private int YLength = 240; private int MaxDataSize = XLength / XScale; private List<Integer> data = new ArrayList<Integer>(); private String[] YLabel = new String[YLength / YScale]; private Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0x1234) { MyView.this.invalidate(); } }; }; public MyView(Context context, AttributeSet attrs) { super(context, attrs); for (int i = 0; i < YLabel.length; i++) { YLabel[i] = (i + 1) + "M/s"; } new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (data.size() >= MaxDataSize) { data.remove(0); } data.add(new Random().nextInt(4) + 1); handler.sendEmptyMessage(0x1234); } } }).start(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias(true); // 去锯齿 paint.setColor(Color.BLUE); // 画Y轴 canvas.drawLine(XPoint, YPoint - YLength, XPoint, YPoint, paint); // Y轴箭头 canvas.drawLine(XPoint, YPoint - YLength, XPoint - 3, YPoint - YLength + 6, paint); // 箭头 canvas.drawLine(XPoint, YPoint - YLength, XPoint + 3, YPoint - YLength + 6, paint); // 添加刻度和文字 for (int i = 0; i * YScale < YLength; i++) { canvas.drawLine(XPoint, YPoint - i * YScale, XPoint + 5, YPoint - i * YScale, paint); // 刻度 canvas.drawText(YLabel[i], XPoint - 50, YPoint - i * YScale, paint);// 文字 } // 画X轴 canvas.drawLine(XPoint, YPoint, XPoint + XLength, YPoint, paint); // 绘折线 /* * if(data.size() > 1){ for(int i=1; i<data.size(); i++){ * canvas.drawLine(XPoint + (i-1) * XScale, YPoint - data.get(i-1) * * YScale, XPoint + i * XScale, YPoint - data.get(i) * YScale, paint); } * } */ paint.setColor(Color.RED); paint.setStrokeWidth(5); Paint paint2 = new Paint(); paint2.setColor(Color.BLUE); paint2.setStyle(Paint.Style.FILL); if (data.size() > 1) { Path path = new Path(); Path path4 = new Path(); path.moveTo(XPoint, YPoint - data.get(0) * YScale); path4.moveTo(XPoint, YPoint); for (int i = 0; i < data.size(); i++) { path.lineTo(XPoint + i * XScale, YPoint - data.get(i) * YScale); path4.lineTo(XPoint + i * XScale, YPoint - data.get(i) * YScale); } path4.lineTo(XPoint + (data.size() - 1) * XScale, YPoint); canvas.drawPath(path, paint); canvas.drawPath(path4, paint2); } } }

 

 

延伸阅读:

返回到首页 返回到编程大巴

  • Win32
  • OpenGL
  • ASP.NET MVC
  • cocos2d-x
  • WCF
  • Linq
  • Android
  • QT
  • MFC
  • JQuery
  •  

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • Android仿微信二维码和条形码

      Android仿微信二维码和条形码

      2016-11-11 10:00

    • 学习Canvas 画圆锥,并且作为一个统计图

      学习Canvas 画圆锥,并且作为一个统计图

      2016-11-05 14:03

    • Android中SurfaceView和view画出触摸轨迹

      Android中SurfaceView和view画出触摸轨迹

      2016-11-02 12:00

    • Android自定义view实现动画数字圆圈

      Android自定义view实现动画数字圆圈

      2016-10-23 11:01

    网友点评
    .