前言

  ShapeDrawable一开始我以为它是对应xml文件属性里的shape的画图,后来发现我错了... 其实ShapeDrawable更像是一共自由度更大跟偏向与实现draw()方法的一共图像绘制类.所以,它的优点就是可以有更大的自由在代码里绘制一个你想要的图形,缺点是它搞起来有点不太方便,对于只需要简单的图形还不如GradientDrawable方便.

  ShapeDrawable是靠new一个继承Shape的类,来实现方便你的绘制的(其实底层原理就是View的draw()那套东西).我们可以查到一共有多少个shape,看如下图片:

  

画圆形 OvalShape

        ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
shapeDrawable.getPaint().setColor(Color.BLACK);
Rect rect = new Rect();
rect.top = 0;
rect.left = 0;
rect.bottom = 50;
rect.right = 50;
shapeDrawable.setBounds(rect);
mTextView.setBackground(shapeDrawable);

效果图:

画半圆 ArcShape

        ShapeDrawable shapeDrawable = new ShapeDrawable(new ArcShape(0,180));//ArcShape参数 开始角度startAngle 要画多少角度sweepAngle
shapeDrawable.getPaint().setColor(Color.BLACK);
Rect rect = new Rect();
rect.top = 0;
rect.left = 0;
rect.bottom = 50;
rect.right = 50;
shapeDrawable.setBounds(rect);
mTextView.setBackground(shapeDrawable);

效果图:

画矩形 RectShape

        ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());
shapeDrawable.getPaint().setColor(Color.BLACK);
Rect rect = new Rect();
rect.top = 0;
rect.left = 0;
rect.bottom = 50;
rect.right = 50;
shapeDrawable.setBounds(rect);
mTextView.setBackground(shapeDrawable);

效果图:

画内外双层矩形,并且有圆角 RoundRectShape

        float[] externalRound = {8, 8, 8, 8, 8, 8, 8, 8};//外部矩形的8个圆角半径,为什么是8个? 因为这个居然是一个角2个半圆组成的(太精细了...)
RectF distanceRectF = new RectF(10, 10, 10, 10); //内部矩形与外部矩形的距离
float[] insideRound = {10, 10, 10, 10, 10, 10, 10, 10}; //内部矩形的8个圆角半径值
ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(externalRound, distanceRectF, insideRound));
shapeDrawable.getPaint().setColor(Color.BLACK);
Rect rect = new Rect();
rect.top = 0;
rect.left = 0;
rect.bottom = 50;
rect.right = 50;
shapeDrawable.setBounds(rect);
mTextView.setBackground(shapeDrawable);

public RoundRectShape(@Nullable float[] outerRadii, @Nullable RectF inset, @Nullable float[] innerRadii)  这个类一共有3个参数,我在上面的注解已经说明了.

注意!这3个参数都是可以为null的.意思就是,你可以取消任意一个.获得一些其他效果,比如设置第二个和第三个参数为null,你就可以得到一个实心的圆角矩形.

效果图:

画任意形状 PathShape

待续....

Android开发 ShapeDrawable详解的更多相关文章

  1. Android开发实例详解之IMF(Android SDK Sample—SoftKeyboard)

    本博前面的文章介绍了Android开发环境的搭建和模拟器的常用操作.本次,将以Android Sample中经典的SoftKeyboard项目为例,详细解析Android上一个小型项目的开发过程和注意 ...

  2. Android开发——AsyncTask详解

    android提供AsynvTask,目的是为了不阻塞主线程(UI线程),且UI的更新只能在主线程中完成,因此异步处理是不可避免的. Android为了降低开发难度,提供了AsyncTask.Asyn ...

  3. Android开发 Context详解与类型 转载

    转载地址:https://blog.csdn.net/guolin_blog/article/details/47028975 个人总结: Context分为 activity : activity其 ...

  4. Android开发 GradientDrawable详解

    前言 GradientDrawable类似与Xml布局里的shape,常用在一些自己封装的对话框控件的背景或者其他View中,优势是不需要你在带着xml布局文件一起封包.. 画线 GradientDr ...

  5. Android开发之详解五大布局

    http://bbs.chinaunix.net/thread-3654213-1-1.html 为了适应各式各样的界面风格,Android系统提供了5种布局,这5种布局分别是: LinearLayo ...

  6. Android 开发 ConstraintLayout详解

    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3' app:layout_constraintHorizo ...

  7. Android 开发 HandlerThread详解 转载

    转载请注明出处:http://blog.csdn.net/vnanyesheshou/article/details/75073307 对于Handler不太懂的可以参考我的这两篇文章: Androi ...

  8. Android开发 StateListDrawable详解

    前言 StateListDrawable是与xml中的selector属性对应代码实现类,它需要配合GradientDrawable的使用,如果你还不了解GradientDrawable可以参考我的另 ...

  9. Android开发 layer-list详解

    参考:https://blog.csdn.net/speverriver/article/details/80925686 挖坑,以后填坑

随机推荐

  1. fasttext源码剖析

    fasttext源码剖析   目的:记录结合多方资料以及个人理解的剖析代码: https://heleifz.github.io/14732610572844.html http://www.cnbl ...

  2. pycharm 参数、快捷键、调试模式

    PyCharm参数.快捷键.调试模式 PyCharm设置参数 在运行Python脚本时,会经常遇到需要传入额外的参数来运行脚本. 例如下脚本1: #!/usr/bin/env python2 # *. ...

  3. VIM 配色方案,先保存一下

    https://github.com/chriskempson/tomorrow-theme http://ethanschoonover.com/solarized http://ethanscho ...

  4. VMware Workstation 添加磁盘 挂载目录(centos)

    参考文档: Linux命令大全 需求:测试环境虚拟机某个目录下空间不足,准备通过添加一块磁盘,并将该目录挂载到该磁盘 前面几步在测试服务器上操作的,截图困难,所以网上找了几张设置图 关闭虚拟机(没测试 ...

  5. 2019 牛客多校第三场 B Crazy Binary String

    题目链接:https://ac.nowcoder.com/acm/contest/883/B 题目大意   给定一个长度为 N 的 01 字符串,输出最长子串和子序列的长度,满足其中 0 和 1 的个 ...

  6. mybatis 处理枚举类型

    MyBatis支持持久化enum类型属性.假设t_user表中有一列gender(性别)类型为 varchar2(10),存储 MALE 或者 FEMALE 两种值.并且,User对象有一个enum类 ...

  7. 01、requests 基本使用

    requests模块的基本使用 基于网络请求的模块. 环境的安装:pip install requests 作用:模拟浏览器发起请求 分析requests的编码流程: 1.指定url 2.发起了请求 ...

  8. [eJOI2018]元素周期表

    题目 \((r_1,c_1),(r_2,c_1),(r_1,c_2)\)三个格子存在就说明\((r_2,c_2)\)存在,如果我们将\(r_1,c_2,c_1,r_2\)都看成一些点的话,那么这个关系 ...

  9. JS鼠标经过

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>菜鸟 ...

  10. sql中取出字符串中数字

    select substring(reverse('0->星光'),PATINDEX('%[0-9]%',reverse('0->星光')),1)