绘制系列(十七)-Drawable

博客探讨了Android中的ShapeDrawable和Shape标签的关系。虽然shape标签对应的类是GradientDrawable,但ShapeDrawable才能实现shape标签的所有功能。内容详细介绍了ShapeDrawable的构造、常用方法,如设置边界、透明度和颜色过滤,以及ShapeDrawable的Paint对象如何与Shader配合工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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函数时一个虚函数,每个不同的子类可以根据不同的需求来绘制不同的图形;

常用函数:

  1. setBounds(Rect rect)或者 setBounds(int left,int top,int right,int bottom):设置当前ShapeDrawable在当前控件中的显示位置’
  2. setAlpha(int alpha):设置透明度取值范围为0~255
  3. setColorFilter(ColorFilter colorFilter):
  4. setIntrinsicHeight():设置默认高度,当Drawable以setBackgroundDrawable()或者setImageDrawable()使用时,会使用默认高度、宽度来计算当前Drawable的大小与位置;如果不设置,默认高度为-1px;
  5. setPadding()
  6. getPaint():ShapeDrawable是自带画笔的,只要通过该函数既可得到ShapeDrawable的Paint对象,对其进行操作,效果就会显示在ShapeDrawable上;

需要注意一点的是:我们之前在讲解Shader时,说Shader是从画布左上角绘制的额,所以ShapeDrawable的paint调用Shader时,Shader是从ShapeDrawable所在的区域的左上角开始绘制的;

前在讲解Shader时,说Shader是从画布左上角绘制的额,所以ShapeDrawable的paint调用Shader时,Shader是从ShapeDrawable所在的区域的左上角开始绘制的;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值