Android中的自绘View的那些事儿(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyz_zyx/article/details/78299182

我们曾经在 《Android中的自定义View》文章中简单提到自定义View中自绘的基本用法,就是重写onDraw方法,并利用Paint和Canvas来进行绘制。今天这篇文章将会在onDraw上来讲解一些常用的绘制知识。好了,那我们就先来了解一下Paint和Canvas这两个类。

Paint

Paint就是画笔,在绘制过程中,Paint决定了绘出的方式,包括笔的粗细、颜色,等。Paint有很多设置的方法,下面列出一些比较常用的set方法:

设置绘制的透明度和颜色

setARGB(int a, int r, int g, int b)

setAlpha(int a)

setColor(int color)

设置是否使用抗锯齿。若设为true,会使绘出的图形更加圆滑,但也会消耗一定的资源,等价于setFlags(Paint.ANTI_ALIAS_FLAG);

setAntiAlias(boolean aa); 

设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰。若设为true,等价于setFlags(Paint.DITHER_FLAG);

setDither(boolean dither); 

设置画笔的样式,值有:Style.FILL(填充)、Style.STROKE(描边)和 Style.FILL_AND_STROKE(同时填充和描边,用得比较少)

setStyle(Paint.Style style); 

当setStyle为STROKE或FILL_OR_STROKE时,设置画笔的粗细度

setStrokeWidth(float width); 

当setStyle为STROKE或FILL_OR_STROKE时,设置画笔的线冒样式,值有:Cap.BUTT、Cap.ROUND(圆形样式)和 Cap.SQUARE(方形样式)

setStrokeCap(Paint.Cap cap); 


当setStyle为STROKE或FILL_OR_STROKE时,设置线段连接处样式,值有:Join.MITER(结合处为锐角)、Join.ROUND(结合处为圆弧)、Join.BEVEL(结合处为直线) 

setStrokeJoin(Paint.Join join) 


设置路径样式,其中传值是PathEffect的子类

setPathEffect(PathEffect effect) ; 


设置图像效果,使用Shader可以绘制出各种渐变效果

setShader(Shader shader); 

设置绘制文字的字号大小

setTextSize(float textSize); 

设置绘制文字的Typeface对象,即字体风格,包括粗体,斜体以及衬线体,非衬线体等

setTypeface(Typeface typeface); 

设置绘制文字模拟粗体效果,注意是模拟的,在小字体上效果会非常差

setFakeBoldText(boolean fakeBoldText); 

设置绘制文字斜体效果,skewX为倾斜弧度

setTextSkewX(float skewX); 

设置绘制文字带有下划线的效果

setUnderlineText(boolean underlineText);

设置绘制文字带有删除线的效果

setStrikeThruText(booleanstrikeThruText); 

设置绘制文字的对齐方向

setTextAlign(Paint.Align align); 

设置绘制文字的间隔,默认值:0

setLetterSpacing(float letterSpacing);

设置绘制文字x轴的缩放比例,可以实现文字的拉伸的效果,默认值:1

setTextScaleX(float scaleX);

Canvas

Canvas就是画布,简单说就是在上面呈现出我们想画的东西。Canvas提供了很多绘制方法,下面列出一些比较常用的draw方法:

绘制整个画布的背景颜色

drawARGB(int a, int r, int g, int b)

drawRGB(int r, int g, int b)

drawColor(int color)

drawColor(int color, PorterDuff.Mode mode)

绘制圆弧。其中,参数startAngle表示弧开始的角度位置(默认是3点钟位置);参数sweepAngle表示弧的饱和角度(取值0~360,360表示全闭,它是顺时针扫描角度);参数useCenter若为true,则表示弧两个边跟圆心相连,为false,则表示弧两个边直接相连

drawArc(float left, float top, float right,float bottom, float startAngle, float sweepAngle, boolean useCenter, Paintpaint)

drawArc(RectF oval, float startAngle, floatsweepAngle, boolean useCenter, Paint paint)

绘制圆。其中,参数cx和cy表示圆心坐标;参数radius表示半径长度

drawCircle(float cx, float cy, floatradius, Paint paint)

绘制直线或多样直线

drawLine(float startX, float startY, floatstopX, float stopY, Paint paint)

drawLines(float[] pts, int offset, intcount, Paint paint)

drawLines(float[] pts, Paint paint)

绘制椭圆

drawOval(float left, float top, floatright, float bottom, Paint paint)

drawOval(RectF oval, Paint paint)

绘制点

drawPoint(float x, float y, Paint paint)

drawPoints(float[] pts, Paint paint)

drawPoints(float[] pts, int offset, intcount, Paint paint)

绘制矩形

drawRect(float left, float top, floatright, float bottom, Paint paint)

drawRect(Rect r, Paint paint)

drawRect(RectF rect, Paint paint)

绘制圆角矩形。其中,参数rx和ry分别表示x和y方向上的圆角半径

drawRoundRect(RectF rect, float rx, floatry, Paint paint)

drawRoundRect(float left, float top, floatright, float bottom, float rx, float ry, Paint paint)

绘制文字

drawText(CharSequence text, int start, intend, float x, float y, Paint paint)

drawText(String text, float x, float y,Paint paint)

drawText(char[] text, int index, int count,float x, float y, Paint paint)

drawText(String text, int start, int end,float x, float y, Paint paint)

绘制Bitmap

drawBitmap(Bitmap bitmap, Matrix matrix,Paint paint)

drawBitmap(int[] colors, int offset, intstride, float x, float y, int width, int height, boolean hasAlpha, Paint paint)

drawBitmap(int[] colors, int offset, intstride, int x, int y, int width, int height, boolean hasAlpha, Paint paint)

drawBitmap(Bitmap bitmap, Rect src, Rectdst, Paint paint)

drawBitmap(Bitmap bitmap, Rect src, RectFdst, Paint pain

drawBitmap(Bitmap bitmap, float left, floattop, Paint paint)

绘制Picture

drawPicture(Picture picture, RectF dst)

drawPicture(Picture picture)

drawPicture(Picture picture, Rect dst)

绘制路径,可绘制出不规则多边形

drawPath(Path path, Paint paint)

示例

public class MyView extends View {

    private Paint mPaint;

    public MyView(Context context) {
        this(context, null, 0);
    }
    public MyView(Context context, AttributeSetattrs) {
        this(context, attrs, 0);
    }
    public MyView(Context context, AttributeSetattrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(0xffff0000);
        mPaint.setStyle(Paint.Style.FILL);
    }

    // 重写onDraw
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制一个颜色背景
        canvas.drawColor(0xffc3e8ff);

        // 绘制圆弧
        canvas.drawArc(20, 20, 220, 220, -90, 270, true, mPaint);
        canvas.drawArc(240, 20, 440, 220, -90, 270, false, mPaint);

        // 绘制椭圆
        canvas.drawOval(460, 20, 700, 220, mPaint);

        // 绘制圆
        canvas.drawCircle(120, 340, 100, mPaint);

        // 绘制直线
        canvas.drawLine(240, 240, 440, 440, mPaint);

        // 绘制点
        canvas.drawPoint(560, 340, mPaint);

        // 绘制矩形
        canvas.drawRect(20, 460, 320, 660, mPaint);

        // 绘制圆角矩形
        canvas.drawRoundRect(360, 460, 560, 660, 15, 50, mPaint);

        // 绘制路径(这里是梯角形)
        Path path = new Path();
        path.moveTo(90, 680);
        path.lineTo(20, 880);
        path.lineTo(350, 880);
        path.lineTo(180, 680);
       path.close();       // 在Paint为STROKE(描边)时,最后一点是否与第一个点相连,在FILL(填充)无区别
        canvas.drawPath(path, mPaint);

        // 绘制文字
        mPaint.setTextSize(50);
        mPaint.setUnderlineText(true);
        canvas.drawText("Hello World", 370, 800, mPaint);
    }
}

运行程序在屏蔽将绘制成这样:


猜你喜欢

转载自blog.csdn.net/lyz_zyx/article/details/78299182