如何能让企业做网站的打算,最有名的免费建站平台排行榜,wordpress template hierarchy,信息网推广宣传方案怎么写绘制的基本要素:
onDraw(Canvas)#xff1a;是用来重写的
Canvas#xff1a;实际执行绘制的
Paint#xff1a;调整粗细和颜色等
坐标系#xff1a;以屏幕左上角为原点#xff0c;向右、向下为正向数值的坐标系
尺寸单位#xff1a;在绘制过程中所有的尺寸单位都是px…绘制的基本要素:
onDraw(Canvas)是用来重写的
Canvas实际执行绘制的
Paint调整粗细和颜色等
坐标系以屏幕左上角为原点向右、向下为正向数值的坐标系
尺寸单位在绘制过程中所有的尺寸单位都是px像素在绘制阶段是直接跟屏幕打交道的
举例我们画一条线
package com.example.viewtest.viewimport android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.Viewclass TestView(context: Context, attrs: AttributeSet) : View(context, attrs) {private val paint Paint(Paint.ANTI_ALIAS_FLAG)override fun onDraw(canvas: Canvas) {super.onDraw(canvas)canvas.drawLine(100f, 100f, 300f, 300f, paint)}
}画个圆
package com.example.viewtest.viewimport android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.example.viewtest.ext.dpclass TestView(context: Context, attrs: AttributeSet) : View(context, attrs) {private val paint Paint(Paint.ANTI_ALIAS_FLAG)override fun onDraw(canvas: Canvas) {super.onDraw(canvas)canvas.drawCircle(width / 2f, height / 2f, 100.dp, paint)}
}接下来讲Pathpath不是用来绘制路径的绘制路径只是其中一个功能而已他是用来绘制图形的
使用Path绘制一个圆
package com.example.viewtest.viewimport android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import com.example.viewtest.ext.dpclass TestView(context: Context, attrs: AttributeSet) : View(context, attrs) {private val paint Paint(Paint.ANTI_ALIAS_FLAG)private val path Path()// 在尺寸发生改变的时候初始化 pathoverride fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {super.onSizeChanged(w, h, oldw, oldh)path.reset()// 最后一个参数DirectionCW顺时针CCW逆时针path.addCircle(width / 2f, height / 2f, 100.dp, Path.Direction.CW)}override fun onDraw(canvas: Canvas) {super.onDraw(canvas)
// canvas.drawLine(100f, 100f, 300f, 300f, paint)
//
// canvas.drawCircle(width / 2f, height / 2f, 100.dp, paint)canvas.drawPath(path, paint)}
}这里主要讲一下DirectionDirection主要是用来处理两个图形相交处的样子通过Direction的配合使用可以使图形相交地方之处实现实心还是镂空的效果这里有一个标准
如果要确定一个点是在内部还是外部那么就从这个点像任意方向发送一条射线这条线遇到左旋逆时针路径就1右旋路径就-1最终结果无论正负只要不为0那么这就是一个在内部的点
除此之外一般使用path.fillType属性他的默认值是Path.FillType.WINDING使用path.fillType是另一套计算方式也就是说不管左旋还是右旋一个点只记录和path的相交次数奇数为内部偶数为外部
镂空效果直接设置为 path.fillType Path.FillType.EVEN_ODD
举个例子一个圆和一个方相交
package com.example.viewtest.viewimport android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import com.example.viewtest.ext.dpclass TestView(context: Context, attrs: AttributeSet) : View(context, attrs) {private val paint Paint(Paint.ANTI_ALIAS_FLAG)private val path Path()// 在尺寸发生改变的时候初始化 pathoverride fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {super.onSizeChanged(w, h, oldw, oldh)path.reset()// 最后一个参数DirectionCW顺时针CCW逆时针path.addCircle(width / 2f, height / 2f, 100.dp, Path.Direction.CW)path.addRect(width / 2f - 100.dp, height / 2f, width / 2f 100.dp, height / 2f 2 * 100.dp, Path.Direction.CCW)// 第二个参数的作用是是否要自动闭合pathMeasure PathMeasure(path, false)pathMeasure.length// 给定一个长度会返回当前长度所处位置的切点也就是正弦值pathMeasure.getPosTan()path.fillType Path.FillType.EVEN_ODD}override fun onDraw(canvas: Canvas) {super.onDraw(canvas)
// canvas.drawLine(100f, 100f, 300f, 300f, paint)
//
// canvas.drawCircle(width / 2f, height / 2f, 100.dp, paint)canvas.drawPath(path, paint)}
}pathMeasure PathMeasure(path, false)
第二个参数的作用是是否要自动闭合如果一个图形假设是个半圆那么测量的就是这个半圆的长度如果是true就是半圆起始点连接线的长度