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

Android编程开发之在Canvas中利用Path绘制基本图形(圆形,矩形,椭圆,三角形等)

程序员文章站 2023-12-20 11:59:04
本文实例讲述了android编程开发之在canvas中利用path绘制基本图形的方法。分享给大家供大家参考,具体如下: 在android中绘制基本的集合图形,本程序就是自...

本文实例讲述了android编程开发之在canvas中利用path绘制基本图形的方法。分享给大家供大家参考,具体如下:

在android中绘制基本的集合图形,本程序就是自定义一个view组件,程序重写该view组件的ondraw(canvase)方法,然后在该canvas上绘制大量的基本的集合图形。

直接上代码:

1.自定义的view组件代码:

package com.infy.configuration;
import android.content.context;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.lineargradient;
import android.graphics.paint;
import android.graphics.path;
import android.graphics.rectf;
import android.graphics.shader;
import android.util.attributeset;
import android.view.view;
public class myview extends view{
    public myview(context context, attributeset attrs) {
      super(context, attrs);
    }
    @override
    protected void ondraw(canvas canvas) {
      // todo auto-generated method stub
      super.ondraw(canvas);
      //把整张画布绘制成白色
      canvas.drawcolor(color.white);
      paint paint = new paint();
      //去锯齿
      paint.setantialias(true);
      paint.setcolor(color.blue);
      paint.setstyle(paint.style.stroke);
      paint.setstrokewidth(3);
      //绘制圆形
      canvas.drawcircle(40, 40, 30, paint);
      //绘制正方形
      canvas.drawrect(10, 80, 70, 140, paint);
      //绘制矩形
      canvas.drawrect(10, 150, 70, 190, paint);
      rectf rel = new rectf(10,240,70,270);
      //绘制椭圆
      canvas.drawoval(rel, paint);
      //定义一个path对象,封闭一个三角形
      path path1 = new path();
      path1.moveto(10, 340);
      path1.lineto(70, 340);
      path1.lineto(40, 290);
      path1.close();
      //根据path进行绘制,绘制三角形
      canvas.drawpath(path1, paint);
      //定义一个path对象,封闭一个五角星
      path path2 = new path();
      path2.moveto(27, 360);
      path2.lineto(54, 360);
      path2.lineto(70, 392);
      path2.lineto(40, 420);
      path2.lineto(10, 392);
      path2.close();
      //根据path进行绘制,绘制五角星
      canvas.drawpath(path2, paint);
      //设置填丛风格后进行绘制
      paint.setstyle(paint.style.fill);
      paint.setcolor(color.red);
      canvas.drawcircle(120, 40, 30, paint);
      //绘制正方形
      canvas.drawrect(90, 80, 150, 140, paint);
      //绘制矩形
      canvas.drawrect(90, 150, 150, 190, paint);
      //绘制圆角矩形
      rectf re2 = new rectf(90,200,150,230);
      canvas.drawroundrect(re2, 15, 15, paint);
      //绘制椭圆
      rectf re21 = new rectf(90, 240, 150, 270);
      canvas.drawoval(re21, paint);
      path path3 = new path();
      path3.moveto(90, 340);
      path3.lineto(150, 340);
      path3.lineto(120, 290);
      path3.close();
      //绘制三角形
      canvas.drawpath(path3,paint);
      //绘制五角形
      path path4 = new path();
      path4.moveto(106, 360);
      path4.lineto(134, 360);
      path4.lineto(150, 392);
      path4.lineto(120, 420);
      path4.lineto(90, 392);
      path4.close();
      canvas.drawpath(path4, paint);
      //设置渐变器后绘制
      //为paint设置渐变器
      shader mshasder = new lineargradient(0, 0, 40, 60, new int[]{color.red,color.green,color.blue,color.yellow}, null, shader.tilemode.repeat);
      paint.setshader(mshasder);
      //设置阴影
      paint.setshadowlayer(45, 10, 10, color.gray);
      //绘制圆形
      canvas.drawcircle(200, 40, 30, paint);
      //绘制正方形
      canvas.drawrect(170, 80, 230, 140, paint);
      //绘制矩形
      canvas.drawrect(170, 150, 230, 190, paint);
      //绘制圆角的矩形
      rectf re31 = new rectf();
      canvas.drawroundrect(re31, 15, 15, paint);
      //绘制椭圆
      rectf re32 =new rectf();
      canvas.drawoval(re32, paint);
      //根据path,绘制三角形
      path path5 = new path();
      path5.moveto(170, 340);
      path5.lineto(230, 340);
      path5.lineto(200, 290);
      path5.close();
      canvas.drawpath(path5, paint);
      //根据path,进行绘制五角形
      path path6 = new path();
      path6.moveto(186, 360);
      path6.lineto(214, 360);
      path6.lineto(230, 392);
      path6.lineto(200, 420);
      path6.lineto(170, 392);
      path6.close();
      canvas.drawpath(path6, paint);
    }
}

2. 使用一个基本的activity来实现自定义的myview组件,

定义一个zidingyiviewtes的activity:

package com.infy.configuration;
import android.app.activity;
import android.os.bundle;
public class zidingyiviewtes extends activity{
    private myview myview =null;
    @override
    protected void oncreate(bundle savedinstancestate) {
      // todo auto-generated method stub
      super.oncreate(savedinstancestate);
      myview = new myview(this, null);
      setcontentview(myview);
    }
}

android的canvas既可以绘制简单的集合图形,也可以直接将一个bitmap绘制到画布上。
最后附上效果图(多了一个椭圆):

 Android编程开发之在Canvas中利用Path绘制基本图形(圆形,矩形,椭圆,三角形等)

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: