Android开发动态设置drawable圆角背景,虚线等

本文介绍如何在Android开发中动态地为视图设置圆角背景和虚线装饰,以此提升开发效率,减少XML背景资源的创建。
/**
 * 画边框方法

 * @param color      背景色
 * *
 * @param radius     圆角
 * *
 * @param stockWidth 边框宽度
 * *
 * @param stockColor 边框颜色
 * *
 * @param dashWidth  边框线间隔
 * *
 * @param dashGap    边框线长度
 * *
 * @return
 */
fun getShapeDrawable(color: Int, radius: Float, stockWidth: Int? = null, stockColor: Int? = null, dashWidth: Float? = null, dashGap: Float? = null): GradientDrawable {
    val gradientDrawable = GradientDrawable()
    val stockWidth = stockWidth ?: 0
    val stockColor = stockColor ?: Color.parseColor("#ffffff")
    val dashWidth = dashWidth ?: 0f
    val dashGap = dashGap ?: 1f
    gradientDrawable.cornerRadius = radius
    gradientDrawable.setColor(color)
    gradientDrawable.setStroke(stockWidth, stockColor, dashWidth, dashGap)
    return gradientDrawable
}

/**
 * 画边框  倒圆角
 * @param color
 * *
 * @param topLeftRadius
 * *
 * @param topRightRadiu
 * *
 * @param bottomLeftRadius
 * *
 * @param bottomRightRadius
 * *
 * @param stockWidth
 * *
 * @param stockColor
 * *
 * @return
 */
fun getShapeDrawable(color: Int, topLeftRadius: Float, topRightRadiu: Float, bottomLeftRadius: Float, bottomRightRadius: Float, stockWidth: Int?, stockColor: Int?): GradientDrawable {
    var stockWidth = stockWidth
    var stockColor = stockColor
    val gradientDrawable = GradientDrawable()
    val f = floatArrayOf(topLeftRadius, topLeftRadius, topRightRadiu, topRightRadiu, bottomLeftRadius, bottomLeftRadius, bottomRightRadius, bottomRightRadius)
    gradientDrawable.cornerRadii = f
    gradientDrawable.setColor(color)
    stockWidth = if (stockWidth == null) 0 else stockWidth
    stockColor = if (stockColor == null) Color.parseColor("#ffffff") else stockColor
    gradientDrawable.setStroke(stockWidth, stockColor)
    return gradientDrawable
}

可以避免创建很多xml的背景,提高开发效率

<think>我们参考用户提供的引用资料,总结如下: 引用[1]提到需要设置TextView的drawable的宽高,可能需要单独设置上下左右四个方向的drawable的宽高(共8个属性),或者统一设置(共10个属性)。 引用[2]展示了如何通过代码设置TextView的drawableLeft,使用的是`setCompoundDrawablesRelativeWithIntrinsicBounds`方法,但这个方法使用的是图片的固有尺寸,不能直接设置大小。 引用[3]提到了图文间距的问题,但用户的问题集中在控制drawable尺寸。 因此,用户的需求是:在Android开发中,使用Java语言为TextView设置Drawable,并且能够控制Drawable的尺寸大小。 根据Android开发经验,设置Drawable并控制其尺寸可以通过以下步骤实现: 1. 获取原始的Drawable对象。 2. 设置Drawable的边界(bounds),通过`setBounds`方法,该方法需要指定左、上、右、下四个参数,实际上就是设置drawable在控件中显示的矩形区域。 3. 使用TextView的`setCompoundDrawables`或`setCompoundDrawablesRelative`方法将设置好边界的Drawable设置到TextView的相应位置。 注意:`setCompoundDrawables`和`setCompoundDrawablesRelative`的区别在于,前者是绝对方向(左、上、右、下),后者是相对于布局方向(起始、上、结束、下)。 具体步骤: 1. 获取Drawable对象,可以通过资源ID获取:`ContextCompat.getDrawable(context, R.drawable.your_drawable)`。 2. 设置Drawable的bounds,例如:`drawable.setBounds(left, top, right, bottom)`。这里的left、top、right、bottom是相对于TextView的drawable位置,通常我们希望设置一个固定大小,那么可以设置left=0, top=0, right=width, bottom=height,其中width和height是我们希望设置的宽高(单位是像素,所以通常需要将dp值转换为px)。 3. 调用TextView的`setCompoundDrawables`或`setCompoundDrawablesRelative`方法,传入四个Drawable对象,分别对应左、上、右、下(或起始、上、结束、下)。对于不需要设置的位置传入null。 但是,需要注意的是,如果只设置一个方向的drawable,其他方向要保持原有的drawable不变,可以先获取原有的drawable,然后只替换需要设置drawable。如果不需要保留其他位置的drawable,可以传入null。 另外,如果只设置大小而不改变位置,那么设置bounds即可。 示例代码: ```java // 获取TextView实例 TextView textView = findViewById(R.id.textView); // 获取drawable对象(假设是left方向的drawableDrawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_launcher); if (drawable != null) { // 将50dp转换为像素值 int sizeInPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()); // 设置drawable的边界,这里设置一个正方形 drawable.setBounds(0, 0, sizeInPx, sizeInPx); // 设置到TextView的左侧(起始侧) // 使用setCompoundDrawablesRelative:用于支持RTL布局 textView.setCompoundDrawablesRelative(drawable, null, null, null); } ``` 另外,如果用户需要同时设置多个方向的drawable,并且分别控制大小,那么需要分别获取drawable设置bounds,然后一起设置。 注意:在设置drawable时,如果某个方向不需要改变,可以使用TextView原有的drawable。例如: ```java Drawable[] drawables = textView.getCompoundDrawablesRelative(); // 假设我们只改变左侧(起始侧)的drawable Drawable leftDrawable = ... // 获取并设置bounds textView.setCompoundDrawablesRelative(leftDrawable, drawables[1], drawables[2], drawables[3]); ``` 总结: 关键点在于使用`setBounds`方法设置drawable的显示区域,然后使用`setCompoundDrawables`或`setCompoundDrawablesRelative`方法应用到TextView。 最后,根据指令,我们需要生成相关问题: §§相关问题§§ 1. 如何将dp单位转换为像素(px)? 2. 在Android中如何支持RTL布局? 3. TextView的drawablePadding属性有什么作用? </think> ### 在Android中使用Java控制TextView的Drawable尺寸 在Android开发中,通过Java代码控制TextView的Drawable尺寸需要以下步骤: #### 核心实现步骤 1. **获取Drawable对象** 从资源文件中加载Drawable: ```java Drawable drawable = ContextCompat.getDrawable(context, R.drawable.your_icon); ``` 2. **设置Drawable尺寸** 使用`setBounds()`方法定义尺寸(单位:像素): ```java // 将50dp转换为像素值 int sizeInPx = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics() ); drawable.setBounds(0, 0, sizeInPx, sizeInPx); // 左,上,右,下 ``` 3. **应用到TextView** 使用`setCompoundDrawables()`方法设置: ```java textView.setCompoundDrawables( drawable, // 左侧 null, // 顶部 null, // 右侧 null // 底部 ); ``` #### 完整示例代码 ```java // 获取TextView实例 TextView textView = findViewById(R.id.tv_example); // 加载drawable资源 Drawable icon = ContextCompat.getDrawable(this, R.drawable.ic_icon); // 设置尺寸为50dp x 50dp int iconSize = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics() ); icon.setBounds(0, 0, iconSize, iconSize); // 应用到TextView左侧 textView.setCompoundDrawables(icon, null, null, null); // 设置图文间距(可选) textView.setCompoundDrawablePadding(16); // 单位:像素 ``` #### 关键说明 1. **尺寸单位转换** 必须将dp值转换为像素值,使用`TypedValue.applyDimension()`方法实现[^1]。 2. **setBounds参数** `setBounds(0, 0, width, height)`中的参数定义Drawable的显示区域: - 前两个参数表示左上角坐标(通常设为0) - 后两个参数定义宽度和高度 3. **多方向设置** 若要设置多个位置的Drawable,需为每个方向单独创建并设置bounds: ```java textView.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable); ``` 4. **RTL布局支持** 对于从右到左布局,使用`setCompoundDrawablesRelative()`替代[^3]: ```java textView.setCompoundDrawablesRelative(startDrawable, topDrawable, endDrawable, bottomDrawable); ``` #### 注意事项 - 直接使用`setCompoundDrawablesWithIntrinsicBounds()`无法自定义尺寸 - 调用`setBounds()`后必须重新设置Drawable到TextView - 图文间距通过`setCompoundDrawablePadding()`单独控制
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值