代码地址如下:
http://www.demodashi.com/demo/14712.html

前言

在讲述自定义控件的时候,我们讲到了自定义控件的基本步骤,那么在自定义控件中,我们第一个需要了解的就是Paint,即画笔。那么今天就来讲讲paint的基本使用。

自定义控件概述

今天要讲的内容包括:

  1. Paint画笔的基本使用
  2. Paint设置宽度时需要注意的地方
  3. 利用paint开发的一个简单示例
  4. 项目结构图和效果图
一.Paint画笔的基本使用
1.1 创建画笔,即Paint对象
        //初始化Paint
Paint paint=new Paint();
1.2 设置Paint颜色

在设置Paint颜色之前,我们需要了解下颜色的一些基本知识

色值的定义有两种方式:

1.十进制方式

2.十六进制表示法

十进制的话颜色色值是从 0-255,数字越大,颜色越深,数字越小,颜色越浅;

十六进制表示法是:(00-ff)(00-ff)(00-ff)(00-ff)分别代表ARGB(例如表示白色,我们可以这样:#ffffffff),数值越小越淡,越大越深。

A表示透明度

R表示红色色值

G表示绿色色值

B表示蓝色色值

每种颜色都由ARGB组成,例如透明为:#00000000,红色为:#FFFF0000,绿色为:#FF00FF00,蓝色为:#FF0000FF

那么接下来就是给Paint设置颜色,代码如下:

        //设置画笔颜色
paint.setColor(0xffff0000);
1.3 设置Paint填充样式

Paint的填充样式分三种:

        //Paint.Style.FILL:仅填充内部
//Paint.Style.STROKE:仅描边
//Paint.Style.FILL_AND_STROKE:描边且填充内部

如果想设置Paint的填充样式为仅描边,你可以这样:

        //设置画笔样式
paint.setStyle(Paint.Style.STROKE);
1.4 设置Paint宽度

我们可以用以下方法来给Paint设置宽度(设置Piant宽度为30f):

        //设置画笔宽度
paint.setStrokeWidth(30f);
1.5 Paint使用步骤基本代码

下面以创建一个MyView代码为例,贴出Paint使用步骤的基本代码

public class MyView extends View{

    public MyView(Context context) {
super(context);
} public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
} public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); //初始化Paint
Paint paint=new Paint();
//设置画笔颜色
paint.setColor(0xffff0000);
//设置画笔样式
paint.setStyle(Paint.Style.STROKE);
//设置画笔宽度
paint.setStrokeWidth(10f);
//将paint设置到canvas中去
canvas.drawCircle(200,200,100,paint);
}
}
二.Paint设置宽度时需要注意的地方

paint.setStrokeWidth();方法仅对Paint的样式为Paint.Style.STROKE或Paint.Style.FILL_AND_STROKE起作用,当Paint的样式为Paint.Style.FILL时,设置Paint的宽度不起作用,因为Paint设置的宽度是描边宽度,而Paint.Style.FILL是仅填充内部,没有描边。

三.利用paint开发的一个简单示例

这里我们自定义一个PaintView,实现的是一个类似同心圆的效果,具体代码在demo中有,这里主要讲解其在MainActivity对应的activity_main.xml中的引用,xml中引用代码如下:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="com.android.testdemo.main.MainActivity"> <com.android.testdemo.animation.PaintView
android:layout_width="match_parent"
android:layout_height="match_parent"/> </android.support.constraint.ConstraintLayout>
四.项目结构图和效果图

项目结构图

效果图

Paint的基本使用

代码地址如下:
http://www.demodashi.com/demo/14712.html

Paint的基本使用的更多相关文章

  1. 详解Paint的setXfermode(Xfermode xfermode)

    一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是 ...

  2. android Canvas 和 Paint用法

    自定义view里面的onDraw方法,在这里我们可以绘制各种图形,onDraw里面有两个API我们需要了解清楚他们的用法:Canvas 和 Paint. Canvas翻译成中文就是画布的意思,Canv ...

  3. [LeetCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

  4. [LeetCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  5. [LeetCode] Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  6. xp系统重绘边框线不显示(首次加载没有触发paint事件)

    同样是,重绘边框事件,win7系统显示正常,而xp系统却不显示,这是什么原因造成的呢? 于是,小编开始百度,不停的查找原因,通过一番查找,小编也意外的收获了一些内容: 例如:窗口的拖动,放大,缩小,等 ...

  7. LeetCode Paint House II

    原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...

  8. LeetCode Paint House

    原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...

  9. zjuoj 3773 Paint the Grid

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3773 Paint the Grid Time Limit: 2 Secon ...

  10. zjuoj 3780 Paint the Grid Again

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...

随机推荐

  1. Verilog 加法器和减法器(1)

    两个一位的二进制数x,y相加,假设和为s,进位为cout,其真值表为: 从真值表中,我们可以得到:s = x^y, cout = x&y,实现两个一位数相加的逻辑电路称为半加器. 实现该电路的 ...

  2. java问题排查命令

    java问题排查命令 jps:查看java进程 jmap:导出堆详细信息(与jhat一起使用) jhat:分析Java堆的命令(与jmap一起使用) jstack:可以定位到线程堆栈,根据堆栈信息我们 ...

  3. 在PC上像普通winform程序调试WINCE程序

    在PC上像普通winform程序调试WINCE程序 步骤: 1. 在VS2008中到 工具→选项→设备工具→设备,选择对应的平台,另存为新的名称,如CEDesktopRun,关闭VS2008.(如果不 ...

  4. {{jQuery源码分析}}jQuery对象初始化的多种传参数形式

    jQuery对象初始化的传参方式包括:1.$(DOMElement)2.$('<h1>...</h1>'), $('#id'), $('.class') 传入字符串, 这是最常 ...

  5. 【Java】PS-查看Java进程-线程数

    PS-查看Java进程-线程数 ps 线程 个数_百度搜索 查看进程的线程数命令 - CSDN博客 java命令行运行jar里的main类 - coderland - 博客园

  6. centos配置ssh免密码登录

    master.slave1两台机器实现ssh免密码登录,user:hadoop,passwd:123456 1.设置master: vi /etc/sysconfig/network hostname ...

  7. capwap学习笔记——初识capwap(四)

    2.5.7 CAPWAP传输机制 WTP和AC之间使用标准的UDP客户端/服务器模式来建立通讯. CAPWAP协议支持UDP和UDP-Lite [RFC3828]. ¢ 在IPv4上,CAPWAP控制 ...

  8. MongoDB server side Javascript 如何直接传入字符串?

    MongoDB server side Javascript的介绍如下: https://docs.mongodb.com/v3.0/core/server-side-javascript/#runn ...

  9. 结构体指针之 段错误 具体解释(segmentation fault)

    一个网友问了我一个问题.一个C程序执行出现了段错误,这个问题非常好.非常多刚開始学习的人都easy犯这个错误,详细代码例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3 ...

  10. Perl、PHP、Python、Java 和 Ruby 比较【转载+整理】

    从本文的内容上,写的时间比较早,而且有些术语我认为也不太准,有点口语化,但是意思到了. 问题: Perl.Python.Ruby 和 PHP 各自有何特点? 为什么动态语言多作为轻量级的解决方案? L ...