Coder Social home page Coder Social logo

viewondrawshape-demo's Introduction

ViewOnDrawShape-Demo

在View上绘制图形主要是,继承View,然后重写它的onDraw(Canvas canvas)方法,在其中Canvas 上利用Paint来进行绘画

Demo运行效果

Demo运行效果

Canvas

Canvas是画布的意思,负责承载我们要进行绘制的各种图形,主要有如下的一些绘制图形接口

  • drawArc 绘制弧
  • drawBitmap 绘制位图
  • drawCircle 绘制圆形
  • drawLine 绘制线
  • drawOval 绘制椭圆
  • drawPath 绘制路径
  • drawPoint 绘制一个点
  • drawPoints 绘制多个点
  • drawRect 绘制矩形
  • drawRoundRect 绘制圆角矩形
  • drawText 绘制字符串
  • drawTextOnPath 沿着路径绘制字符串

Paint

Paint是颜料的意思,主要用来设置绘图的风格,主要有如下的一些设置接口

  • setARGP/setColor 设置颜色
  • setAlpha 设置透明度
  • setAntiAlias 设置是否有抗锯齿
  • setShader 设置画笔的填充效果
  • setShadowLayer 设置阴影
  • setStyle 设置画笔的风格
  • setStrokeWidth 设置空心边框的宽度
  • setTextSize 设置绘制文本时文字的大小

Path

Path是指路径的意思,你可以使用圆形,矩形,弧,或者几个点的连线来生成一条Path。

坐标体系

从左上角开始,向右为x轴正轴方向,向下为y轴正轴方向。在onDraw中坐标,大小的单位都是px。

绘制图形

在View中通过重写onDraw()绘制图形的方法很简单。

首先重写onDraw方法,以绘制圆形举例。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //新建一个Paint对象
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(getResources().getColor(android.R.color.holo_green_dark));
    //在Canvas上绘制圆形
    canvas.drawCircle(150, 150, 100, paint);
}

然后在xml文件中就可以使用我们的CircleView了

<com.lz69.viewondraw_demo.view.CircleView
    android:layout_width="match_parent"
    android:layout_height="100dp"/>

Demo链接

https://github.com/lz69/ViewOnDrawShape-Demo

viewondrawshape-demo's People

Contributors

lz69 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.