Android开发 ShapeDrawable详解
前言
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详解的更多相关文章
- Android开发实例详解之IMF(Android SDK Sample—SoftKeyboard)
本博前面的文章介绍了Android开发环境的搭建和模拟器的常用操作.本次,将以Android Sample中经典的SoftKeyboard项目为例,详细解析Android上一个小型项目的开发过程和注意 ...
- Android开发——AsyncTask详解
android提供AsynvTask,目的是为了不阻塞主线程(UI线程),且UI的更新只能在主线程中完成,因此异步处理是不可避免的. Android为了降低开发难度,提供了AsyncTask.Asyn ...
- Android开发 Context详解与类型 转载
转载地址:https://blog.csdn.net/guolin_blog/article/details/47028975 个人总结: Context分为 activity : activity其 ...
- Android开发 GradientDrawable详解
前言 GradientDrawable类似与Xml布局里的shape,常用在一些自己封装的对话框控件的背景或者其他View中,优势是不需要你在带着xml布局文件一起封包.. 画线 GradientDr ...
- Android开发之详解五大布局
http://bbs.chinaunix.net/thread-3654213-1-1.html 为了适应各式各样的界面风格,Android系统提供了5种布局,这5种布局分别是: LinearLayo ...
- Android 开发 ConstraintLayout详解
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3' app:layout_constraintHorizo ...
- Android 开发 HandlerThread详解 转载
转载请注明出处:http://blog.csdn.net/vnanyesheshou/article/details/75073307 对于Handler不太懂的可以参考我的这两篇文章: Androi ...
- Android开发 StateListDrawable详解
前言 StateListDrawable是与xml中的selector属性对应代码实现类,它需要配合GradientDrawable的使用,如果你还不了解GradientDrawable可以参考我的另 ...
- Android开发 layer-list详解
参考:https://blog.csdn.net/speverriver/article/details/80925686 挖坑,以后填坑
随机推荐
- NX二次开发-NXOpen::WCS Class Reference
NX11+VS2013 #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include < ...
- Linux ifconfig 单网卡配置多网段
1 2 3 4 5 6 7 8 9 10 11 ifconfig eth0 down ifconfig eth0 hw ether 01:02:03:04:05:06 ifconfig eth0 ...
- SecureCRT是最常用的终端仿真程序,简单的说就是Windows下登录UNIX或Liunx服务器主机的软件,本文主要介绍SecureCRT的使用方法和技巧
SecureCRT是最常用的终端仿真程序,简单的说就是Windows下登录UNIX或Liunx服务器主机的软件,本文主要介绍SecureCRT的使用方法和技巧 VanDyke CRT 和 VanDyk ...
- Python3 From Zero——{最初的意识:002~字符串和文本}
一.使用多个界定符分割字符串 字符串.split(',')形式只适用于单一分割符的情况:多分割符同时应用的时候,可使用re.split() >>> line = 'asdf fjdk ...
- Git 学习(一)安装 Git
这里写自定义目录标题 这一章介绍怎么安装 Git 大家都是开发老司机,就不简介什么是 Git 了,直接开花. 在 Linux 上安装Git 在 Windows 上安装 Git 初次使用 Git 前的配 ...
- Linux上 安装Sorl4.7 中间件用tomcat
最近需要用到solr,公司内部搭建了一个solr测试环境. 版本:solr4.7.2 ,tomcat 7.0.55 jdk:1.7_051 解压 solr 和tomcat 这里就不详说. 1.启动t ...
- 多线程中Runnable 和Thread关于synchronized的疑点
学java时和同学碰到的一道题: 转自https://blog.csdn.net/qq_40857349/article/details/102809100 某公司组织年会,会议入场时有两个入口,在入 ...
- Android开发 解决Installation failed due to XXX 问题
报错信息 Android studio 安装app的时候以下报错 Installation did not succeed. The application could not be installe ...
- The Battle of Chibi
The Battle of Chibi 给出一段长度为n的序列\(\{a_i\}\),求其中长度为m的严格上升子序列个数\(mod\ 10^9+7\),\(n\leq 10^3\). 解 不难想到设\ ...
- C++相对路径和绝对路径
学习备忘 转自:http://www.cnblogs.com/vranger/p/3820783.html 电脑硬盘E盘下,建文件夹“test”,"test"下建立子文件夹“fil ...