Drawable
Shape标签与ShapeDrawable
首先明确一点的是:shape标签对应的java类是GradientDrawable,而不是ShapeDrawable;
ShapeDrawable drawable = (ShapeDrawable) ((TextView) findViewById(R.id.tv)).getBackground();
将报:
public GradientDrawable(Orientation orientation, @ColorInt int[] colors)
从构造函数可以看出GradientDrawable完成的是gradient标签的功能,并不能完成shape标签所能完成的构造函数矩形、椭圆等功能;神奇的是:ShapeDrawable能完成shape的标签的所有功能;
shape标签一般是通过控件的android:background属性引入的,在代码中通过控件view.getBackground()得到shape标签的实例即GradientDrawable,然后完成比如;设置圆角等功能
shape标签对应的标签以及标签属性:
左边为Shape标签自身属性,右边为shape标签支持的标签以及属性;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither=["true" | "false"] //将在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动。默认值为 true。
android:shape=["rectangle" | "oval" | "line" | "ring"]//分别为矩形、椭圆、线、环。默认为矩形rectangle
android:innerRadius="integer" // shape为ring时有效,内环半径
android:innerRadiusRatio="float" // shape为ring时有效,内环的厚度比,即环的图形宽度与内环半径的比例,按照这个比例计算内环半径,默认为3,可被innerRadius值覆盖
android:thickness="integer" // shape为ring时有效,环的厚度
android:thicknessRatio="float" // shape为ring时有效,环的厚度比,即环的图形宽度与环的厚度的比例,按照这个比例计算环的厚度,默认为9,可被thickness值覆盖
android:tint="color" // 给shape着色
android:tintMode=["src_in" | "src_atop" | "src_over" | "add" | "multiply" | "screen"] // 着色类型
android:useLevel=["true" | "false"] // 较少用,一般设为false,否则图形不显示。为true时可在LevelListDrawable使用
android:visible=["true" | "false"] >
<!-- 圆角 -->
<corners
android:radius="integer" // 圆角半径,设置下面四个属性时,对应的位置属性会被覆盖
android:topLeftRadius="integer" // 左上角圆角半径
android:topRightRadius="integer" // 右上角圆角半径
android:bottomLeftRadius="integer" // 左下角圆角半径
android:bottomRightRadius="integer" // 右下角圆角半径
/>
<!-- 渐变 -->
<gradient
android:type=["linear" | "radial" | "sweep"]// 渐变类型,线性、放射性、扫描性;默认为线性
android:angle="integer" // 渐变角度,渐变类型为linear时有效;默认为0,从左至右渐变,角度逆时针方向计算,角度需要时45的整数倍数
android:centerColor="integer" // 渐变中间位置颜色
android:startColor="color" // 渐变开始位置颜色
android:endColor="color" // 渐变结束位置颜色
android:centerX="float" // 设置渐变中心的X坐标,取值区间[0,1],默认为0.5,即中心位置
android:centerY="float" // 设置渐变中心的Y坐标,取值区间[0,1],默认为0.5,即中心位置
android:gradientRadius="integer" // type为放射性渐变radial时有效,渐变的半径
android:useLevel=["true" | "false"] // 与shape中该属性的一致
/>
<!-- 内边距 -->
<padding
android:left="integer" // 左边距
android:top="integer" // 上边距
android:right="integer" // 右边距
android:bottom="integer" // 下边距
/>
<!-- 大小 -->
<size
android:width="integer" // 图形宽度
android:height="integer" // 图形高度
/>
<!-- 填充 -->
<solid
android:color="color" // 图形的填充色
/>
<!-- 描边 -->
<stroke
android:width="integer" // 描边的宽度
android:color="color" // 描边的颜色
android:dashWidth="integer" // 虚线宽度
android:dashGap="integer" // 虚线间隔
/>
</shape>
ShapeDrawable
public ShapeDrawable()
public ShapeDrawable(Shape s)
ShapeDrawable需要与Shape关联起来,第一个构造函数需要额外调用ShapeDrawable.setShape()设置Shape对象,一般使用第二构造函数直接传入Shape;Shape只是一个基类,draw函数时一个虚函数,每个不同的子类可以根据不同的需求来绘制不同的图形;
常用函数:
- setBounds(Rect rect)或者 setBounds(int left,int top,int right,int bottom):设置当前ShapeDrawable在当前控件中的显示位置’
- setAlpha(int alpha):设置透明度取值范围为0~255
- setColorFilter(ColorFilter colorFilter):
- setIntrinsicHeight():设置默认高度,当Drawable以setBackgroundDrawable()或者setImageDrawable()使用时,会使用默认高度、宽度来计算当前Drawable的大小与位置;如果不设置,默认高度为-1px;
- setPadding()
- getPaint():ShapeDrawable是自带画笔的,只要通过该函数既可得到ShapeDrawable的Paint对象,对其进行操作,效果就会显示在ShapeDrawable上;
需要注意一点的是:我们之前在讲解Shader时,说Shader是从画布左上角绘制的额,所以ShapeDrawable的paint调用Shader时,Shader是从ShapeDrawable所在的区域的左上角开始绘制的;
前在讲解Shader时,说Shader是从画布左上角绘制的额,所以ShapeDrawable的paint调用Shader时,Shader是从ShapeDrawable所在的区域的左上角开始绘制的;