欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

Android绘制柱状图和圆形图以及折线图_安卓多种统计图表源码

程序员文章站 2022-03-01 15:00:56
...

本例子利用自定义View层实现柱状图、圆形图、折线图的图表绘制

可以运用在一些评分统计类的App功能中、先来看一下运行效果吧

Android绘制柱状图和圆形图以及折线图_安卓多种统计图表源码


核心代码

public class HomeArc extends View {
 
    private Paint paint_black, paint_white;
    private RectF rectf;
    private float tb;
    private int blackColor = 0x70000000; // 底黑色
    private int whiteColor = 0xddffffff; // 白色
    private int score;
    private float arc_y = 0f;
    private int score_text;
 
    public HomeArc(Context context, int score) {
        super(context);
        init(score);
    }
 
    public void init(int score) {
        this.score = score;
        Resources res = getResources();
        tb = res.getDimension(R.dimen.historyscore_tb);
        paint_black = new Paint();
        paint_black.setAntiAlias(true);
        paint_black.setColor(blackColor);
        paint_black.setStrokeWidth(tb * 0.2f);
        paint_black.setStyle(Style.STROKE);
        paint_white = new Paint();
        paint_white.setAntiAlias(true);
        paint_white.setColor(whiteColor);
        paint_white.setTextSize(tb*6.0f);
        paint_white.setStrokeWidth(tb * 0.2f);
        paint_white.setTextAlign(Align.CENTER);
        paint_white.setStyle(Style.STROKE);
        rectf = new RectF();
        rectf.set(tb * 0.5f, tb * 0.5f, tb * 18.5f, tb * 18.5f);
        setLayoutParams(new LayoutParams((int) (tb * 19.5f), (int) (tb * 19.5f)));
        this.getViewTreeObserver().addOnPreDrawListener(
			new OnPreDrawListener() {
				public boolean onPreDraw() {
					new thread();
					getViewTreeObserver().removeOnPreDrawListener(this);
					return false;
				}
			});
    }
 
    protected void onDraw(Canvas c) {
        super.onDraw(c);
        c.drawArc(rectf, -90, 360, false, paint_black);
        c.drawArc(rectf, -90, arc_y, false, paint_white);
        c.drawText("" + score_text, tb * 9.7f, tb * 11.0f, paint_white);
    }
}


thread内部类代码

class thread implements Runnable {
    private Thread thread;
    private int statek;
    int count;

    public thread() {
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        while (true) {
            switch (statek) {
            case 0:
                try {
                    Thread.sleep(200);
                    statek = 1;
                } catch (InterruptedException e) {
                }
                break;
            case 1:
                try {
                    Thread.sleep(15);
                    arc_y += 3.6f;
                    score_text++;
                    count++;
                    postInvalidate();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            }
            if (count >= score)
                break;
        }
    }
}


源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1c0AeWdM 密码: 9vep