Android使用View绘制折线图,饼状图,柱状图等

Android中利用View进行绘制折线图,饼状图,柱状图:

绘制这些图形时,主要用到的知识点为:

1.画笔的设置

2.画布的设置

3.让该类继承View,并复写onDraw方法即可

 

由于源码 较多,不进行代码粘贴了

下面进行截图看看:

1.折线图:

 

2.饼状图

 

3.柱状图

 

 

下面介绍几个方法关于绘制图形的:

可以参考这个api

http://www.android-doc.com/reference/android/graphics/Canvas.html

这里面方法很多:

//画一个点
        Paint paint=new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(100);//设置点的宽度
        Canvas canvas=new Canvas(bitmap);
        canvas.drawPoint(100, 100, paint);
        chooseView.setImageBitmap(bitmap);
      
//画一条直线
        Paint paint=new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(10);//设置点的宽度
        float startX=10;
        float startY=20;
        float stopX=100;
        float stopY=200;
        Canvas canvas=new Canvas(bitmap);
        canvas.drawLine(startX, startY, stopX, stopY, paint);
        chooseView.setImageBitmap(bitmap);
       

     
  //画一个矩形
        Paint paint=new Paint();
        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        Canvas canvas=new Canvas(bitmap);
        int left=10;
        int top=10;
        int right=100;
        int bottom=100;
        Rect rect=new Rect(left, top, right, bottom);
        canvas.drawRect(rect, paint);
//        canvas.drawRect(left, top, right, bottom, paint);
       chooseView.setImageBitmap(bitmap);

     
//椭圆
        Paint paint=new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        int left=10;
        int top=20;
        int right=60;
        int bottom=100;
        RectF oval=new RectF(left, top, right, bottom);
        Canvas canvas=new Canvas(bitmap);
        canvas.drawOval(oval, paint);
        chooseView.setImageBitmap(bitmap);
       

       
      
//圆
        Paint paint=new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        Canvas canvas=new Canvas(bitmap);
        int cx=100;
        int cy=100;
        int radius=20;
        canvas.drawCircle(cx, cy, radius, paint);
        chooseView.setImageBitmap(bitmap);
       

      
//路径
        Paint paint=new Paint();
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
       
        //路
        Path path=new Path();
        path.moveTo(20, 20);
        path.lineTo(100, 200);
        path.lineTo(204, 164);
        path.lineTo(250, 175);
        path.lineTo(20, 20);
        Canvas canvas=new Canvas(bitmap);
        canvas.drawPath(path, paint);
        chooseView.setImageBitmap(bitmap);
       

      
      
//绘制字体
        Paint paint=new Paint();
        paint.setColor(Color.YELLOW);
        paint.setTextSize(40);
        paint.setTypeface(Typeface.DEFAULT_BOLD);//设置字体
        //引用外部字体
        //Typeface typeface=Typeface.createFromAsset(getAssets(), "newFont.ttf");
        //paint.setTypeface(typeface);
       
        Canvas canvas=new Canvas(bitmap);
        canvas.drawText("fuck you!", 50, 100, paint);
        chooseView.setImageBitmap(bitmap);
       

       
//把字画在线上
        Paint paint=new Paint();
        paint.setColor(Color.CYAN);
        paint.setTextSize(20);
        paint.setTypeface(Typeface.DEFAULT);
       
        Path path=new Path();
        path.moveTo(20, 20);
        path.lineTo(100, 150);
        path.lineTo(200, 220);
       
        Canvas canvas=new Canvas(bitmap);
        canvas.drawTextOnPath("Fuck you this text is on path!", path, 0, 0, paint);
       chooseView.setImageBitmap(bitmap);

=============================

 

 

 

 

源码下载

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值