android自定义折线图——之简单的温度折线图

              最近做了一个类似天气预告的demo,其中有一个自定义折线图,在网上找了很多例子,没有一个合适的,于是只能参考别人的例子,根据自己的想法写了一套demo.

           在做自定义控件之前,先熟悉一下paint和canvas的简单使用。

           链接:http://blog.csdn.net/qq_33776306/article/details/52290729

              大概的思路无非就是先画一个横纵坐标,然后根据点的X轴Y轴的坐标,进行画点画线画文字。

          最后的效果图呢如下


          然后接下来看定义的折线图控件了

<span style="color:#009900;">package com.yang.draw;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;


class MyChartView extends View{
	
    public static final int RECT_SIZE = 10;      
    //枚举实现坐标桌面的样式风格
    public static enum Mstyle
    {
    	Line,Curve
    }

    private Mstyle mstyle=Mstyle.Line;
    private Point[] mPoints = new Point[100];  
      
    Context context;
    int bheight=0;
    HashMap<Double, Double> map;
    ArrayList<Double> dlk;
    int totalvalue=30;
    int pjvalue=5;
    String xstr,ystr="";//横纵坐标的属性
    int margint=15;
    int marginb=40;
    int c=0;
    int resid=0;
    Boolean isylineshow;
   
    /**
    * 
    */
    public void SetTuView(HashMap<Double, Double> map,int totalvalue,int pjvalue,String xstr,String ystr,Boolean isylineshow) 
    {
        this.map=map;
        this.totalvalue=totalvalue;
        this.pjvalue=pjvalue;
        this.xstr=xstr;
        this.ystr=ystr;
        this.isylineshow=isylineshow;
   }  

    public MyChartView(Context ct)
    {
    	super(ct);
		this.context=ct;
    }
    
    public MyChartView(Context ct, AttributeSet attrs)
	{
		super( ct, attrs );
		this.context=ct;
	}
    
    public MyChartVi
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 中创建平滑的折线图,可以通过以下步骤实现: 1. 创建一个继承自 View 的自定义视图,并在其中实现绘制折线图的逻辑。 2. 在自定义视图的 onDraw 方法中,使用 Path 对象绘制折线图的路径。 3. 在绘制路径之前,可以使用贝塞尔曲线算法将路径进行平滑处理,以确保折线图的线条更加平滑。 4. 在绘制路径之后,使用 Paint 对象设置线条的颜色、宽度等样式属性。 5. 最后,将自定义视图添加到布局中并显示出来。 以下是一个简单的示例代码,用于绘制平滑的折线图: ```java public class SmoothLineChartView extends View { private Path mPath; private Paint mPaint; public SmoothLineChartView(Context context) { super(context); init(); } public SmoothLineChartView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public SmoothLineChartView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPath = new Path(); mPaint = new Paint(); mPaint.setColor(Color.BLUE); mPaint.setStrokeWidth(5); mPaint.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制平滑的折线图 drawSmoothLineChart(canvas); } private void drawSmoothLineChart(Canvas canvas) { // TODO: 获取折线图数据,计算平滑曲线 // 绘制平滑曲线 canvas.drawPath(mPath, mPaint); } } ``` 在以上代码中,我们创建了一个继承自 View 的自定义视图 SmoothLineChartView,并在其中实现了绘制平滑的折线图的逻辑。具体实现中,我们使用了 Path 对象来绘制折线图的路径,并使用 Paint 对象设置线条的颜色、宽度等样式属性。在绘制路径之前,我们可以使用贝塞尔曲线算法将路径进行平滑处理,以确保折线图的线条更加平滑。最后,我们将自定义视图添加到布局中并显示出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值