Android 图形处理技术(一)

本文深入讲解了使用Paint和Canvas类进行绘图的方法,包括颜色设置、抗锯齿、阴影效果、文字对齐、路径绘制及图像重叠处理。同时介绍了如何在自定义View中运用这些技术实现图形绘制。

绘图类

Paint类(画笔)

实例化对象:

Paint paint = new Paint();

常用方法:

方法描述
setARGB(int a,int r,int g,int b)A(透明)、R(红色)、G(绿色)、B(蓝色) 设置颜色的参数 0-255
setColor(int color)参数color可以填写Color类提供的颜色常量来指定,依然可以用Color.rgb(int r,int g,int b);来指定
setAlpha(int a)指定透明度
setAntiAlias(boolean a)是否使用抗锯齿功能,使用之后,绘制图便变慢
setDither(boolean d)是否使用图像抖动处理 ,使用后图像会变的平滑、饱满、清晰
setPathEffect(PathEffect)设置图像绘制的路径
setShader(Shader s)设置渐变,参数有 LinearGradient 线性渐变、RadialGradient 径向渐变、SweepGradient 角度渐变
setShadowLayer(float radius,float dx,float dy,int color)设置阴影,radius角度(为 0 时无阴影)、dx x轴距离 、dy y轴距离、color 颜色
setStrokeCap(Paint.Join join )设置画笔转弯处的连接风格,参数有Join.BEVEL、Join.MITER、Join.ROUND
setStorkeWidth(folat w)设置笔触的宽度
setStyle(Paint.Style style)设置填充风格,参数为Style.FILL、Style.STROKE、Style.FILL_AND_STROKE
setTextAlign(Paint.Align a)设置文字对齐方式 Align.CENTER、Align.LEFT、Align.RIGHT
setTextSize(float size)设置文字的大小
setFakeBoldText(boolean f)设置是否为粗体
setXfermode(Xfermode x)设置图形重叠时的处理方式

Canvas类 (画布)

     通过Paint类与该类提供的方法,可以画出各种图形。

drawColor(Color color); //画布颜色

绘制几何图形

数组里放的类似于坐标

方法描述
drawCircle(flaot cx, float cy , float radius,Paint paint)
drawLine(float startX,float staytY,float stopX,float stopY,Paint paint)线
drawLines(float [] pts,Paint paint)多条线
drawOval(Rectf oval,Paint paint)椭圆
drawPoint(float x,float y,Paint paint)
drawPoints(float [] pts,Paint paint)多个点
drawRect(float left, float top , float right,float bottom , Paint paint)矩形
drawRoundRect(Rectf rect, float rx,float ry ,Paint paint)圆角矩形
translate(float x,float y)可以设置绘画的位置
drawArc(Rectf oval , float startAngle, float sweepAngle , boolean useCenter ,Paint paint)  //绘制弧形 

(true与false区别)例子:

 canvas.drawArc(new RectF(10,20,100,100),0,45,true,paint);

在这里插入图片描述

 canvas.drawArc(new RectF(50,50,100,100),0,150,false,paint);

在这里插入图片描述

绘制文本

drawText(String txt, float x, float y,Paint paint);

例子:

canvas.drawText("Android 你好",10,80,paint);

在这里插入图片描述

drawPosText(String txt,float [] pos ,Paint paint);//与之前不同,此方法要为每一个子指定坐标

例子:

canvas.drawPosText("你好",new float[]{10,150,80,80},paint);

在这里插入图片描述

绘制路径:

          绘制路径可以分为两部

(1)创建路径

创建路径,可以使用Path类,Path类包含一组矢量图的绘制方法,例如圆、矩形、弧、线条等。

方法描述
addArc(RectF oval,float startAngle,float sweepAngle)添加弧形路径
addCricle(float x,float y,float radius,Path.Direction d )添加圆形路径
addOval(RectF oval, Path.Direction d)添加椭圆路径
addRect(RectF rect,Path.Direction d)添加矩形路径
addRoundRect(RectF rect, float rx, float ry,Path.Direction d)添加圆角矩形
moveTo(float x,float y)设置开始绘制直线的起始点
lineTo(float x,float y)在moveTo()方法设置起始点与该方法的结束点之间画一条线,若之前并没有使用moveTo(),则将从(0,0)点开始画线
quadTo(float x1,float y1,float x2,float y2)根据指定的参数绘制一段轨迹的线段
close()闭合路径

Path.Direction 是一个常量,值为:

Path.Direction.CW 顺时针
Path.Direction.CCW 逆时针
(2)将定义好的路径画在画布上

canvas.drawPath(Path path,Paint paint);
例子:

Paint paint = new Paint();
Path path = new Path();
path.moveTo(50,100);
path.lineTo(100,45);
path.lineTo(150,100);
path.lineTo(200,80);
canvas.drawPath(path,paint);

按理说这样绘制出来的是一段折现,奈何它竟然是封闭的图形,这是个问题!!目前没有找到方法,求指教
在这里插入图片描述

canvas.drawTextOnPath();//绘制字符串

例子:

 String s = "Android is best os";
 Paint paint = new Paint();
 Path path = new Path();
 path.addCircle(150,150,20,Path.Direction.CW);
 canvas.drawTextOnPath(s,path,0,-80,paint);

在这里插入图片描述

绘制图片

可以将保存在Bitmap中的图像信息画出来

方法描述
drawBitmap(Bitmap bitmap,Rect src,RectF dst,Paint paint)从指定点绘制从源图像中挖出的一块
drawBitmap(Bitmap bitmap,float left,float top,Paint paint)在指定点绘制位图
drawBitmap(Bitmap bitmap,Rect src,Rect dst,Paint paint)从指定点绘制从源图像中挖出的一块

Rect src 表示要挖去的位置
Rect dst 表示要绘制的位置

使用Canvas:

(1)创建一个类继承View,重写onDraw();

          在继承的View类中,有四个构造方法,必须重写其中一个,这四个构造方法是在不同的情况下实现引用绘图布局的。
若要再布局中引用则要重写构造方法如下:

 public 类名(Context context,  AttributeSet attrs) {
        super(context, attrs);
    }

否则会有类似的报错:

android.view.InflateException: Binary XML file line #9: Error inflating class com.example.newland.f20190818.MyView

          在onDraw()方法中,实例化你想要的画笔,描绘你想绘画出来的图案。

package com.example.newland.g20190815;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

public class DrawV extends View {


    public DrawV(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        //实例化画笔
        Paint paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.BLUE);
        //设置阴影的颜色
        paint.setShadowLayer(2,3,3,Color.BLUE);
        //设置线性渐变
        Shader shader = new LinearGradient(0,0,50,50,Color.BLUE,Color.GREEN,Shader.TileMode.MIRROR);
        paint.setShader(shader);
        //画圆
        canvas.drawCircle(50,50,50,paint);
    }
}


(2)在布局文件中添加视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
   
    <com.example.newland.g20190815.DrawV
        android:id="@+id/xom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
</RelativeLayout>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值