android自定义绘图

Canvas绘图

圆角矩形

需要使用RectF对象来描述矩形区域
RectF rect = new RectF(left, top, right, bottom);
//绘制圆角矩形(矩形区域,圆角x方向半径,圆角y方向半径,画笔)
canvas.drawRoundRect(rect, 20, 20, p);

画椭圆

//椭圆绘制
 RectF rect = new RectF(50,20,250,120);
 canvas.drawOval(rect,paint);

画扇形或者弧线

//画扇形/弧线
RectF oval =new RectF(50,130,150,230);
canvas.drawArc(oval,-90,135,false,paint);
第二参数表示起始角度,第三个参数表示总跨越度数
第四个参数true表示扇形,false表示弧线

绘制任意的路径

//画任意的图形
Path p = new Path();
//设置起始点
p.moveTo(50,240);
//从上一个点画线到当前点
p.lineTo(450,250);
p.lineTo(300,500);
//从上一个点画弧线前面两个参数表示确定弧度的点,后面两个参数表示弧度终点
p.quadTo(400,750,50,600);
//将最后一个点连接到起始点
p.close();
canvas.drawPath(p,paint);

文本绘制

FontMetrics文本的高度描述信息
Paint.FontMetrics fm = paint.getFontMetrics();
fm.top  文本顶部边界(包括间距)
fm.ascent 文本顶部(不包括间距)
fm.descent 文本基准线
fm.bottom 文本底部

基于基准线画文本

 Path path = new Path();
 path.moveTo(300,500);
 path.quadTo(500,400,10,800);
 canvas.drawTextOnPath(str,path,0,0,paint);
第三个参数表示文本的水平偏移
第四个参数表示文本垂直偏移

文本边框以及参考边框的绘制

Rect r = new Rect();
//获取文本边框
paint.getTextBounds(str,0,str.length(),r);
int x1 = R1.centerX()-r.centerX();
int y1 = R1.centerY() - r.centerY();
canvas.drawText(str,x1,y1,paint);
视图测量的处理

如果针对视图的宽高有自定义的运算则,需要重写onMeasure方法处理视图的测量

 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //获取宽方向的测量模式
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);

    int width = MeasureSpec.getSize(widthMeasureSpec);

    ...
    //如果宽高确定了需要setMeasuredDimension才有作用
    setMeasuredDimension(width,height);
}

关于Mode

int mode = MeasureSpec.getMode(widthMeasureSpec);
主要有3类:
MeasureSpec.EXACTLY:表示精确度,固定的值,如match_parent 100dp 200dp等等

MeasureSpec.AT_MOST:视图想要多大就多大,值不确定,如wrap_content

MeasureSpec.UNSPECIFIED:未指定,不确定,开发者可以不用处理该类型
自定义属性

需要在values下定义attrs.xml

 <declare-styleable name="myStyleable">
    <attr name="max" format="integer"/>
    <attr name="progress" format="integer"/>
    <attr name="textSize" format="dimension"/>
    <attr name="smallBgColor" format="color"/>
    <attr name="progressColor" format="color"/>
</declare-styleable>

其中fomat表示属性的取值类型

integer:整数
dimension:尺寸 dp dip sp px
color:颜色
string:字符串
float:浮点数 android:rating="1.5"
referrence:引用 @id/btn @drawable/pic

解析属性

public XykjProgressBar(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    //解析自定义属性
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myStyleable);
    textSize = ta.getDimensionPixelSize(R.styleable.myStyleable_textSize, 0);
    max = ta.getInteger(R.styleable.myStyleable_max,100);
    progress = ta.getInteger(R.styleable.myStyleable_progress,0);
    smallBgColor = ta.getColor(R.styleable.myStyleable_smallBgColor,0xff3F51B5);
    progressColor = ta.getColor(R.styleable.myStyleable_progressColor,0xffFF4081);
    //释放资源
    ta.recycle();
}

使用属性

需要在布局中声明前缀的命名空间
xmlns:app="http://schemas.android.com/apk/res-auto"

<com.xykj.myprogressbar.XykjProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="200dp"
    android:layout_height="200dp"
    app:textSize="30sp"
    app:smallBgColor="#ff000000"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值