Android绘制柱状图和圆形图以及折线图_安卓多种统计图表源码
2014-10-09 14:02:50  By: dwtedx

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

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


核心代码

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

若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力

想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)

或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)

如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教

为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)

感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛


快速评论


技术评论

  • 该技术还没有评论、赶快抢沙发吧...
DD记账
top
+