安卓用shape画圆
Shape
2023-09-27 14:27:30 时间
1. 用xml画普通圆
- 效果图
- 用xml的shape实现
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFF68F" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
- 用代码实现
/**
* 获取圆型背景
* @param colorInt 整型色值
*/
fun getCircleBg(colorInt: Int) = GradientDrawable().also {
it.shape = GradientDrawable.OVAL
it.setColor(colorInt)
}
/**
* 获取圆型背景
* @param colorStr 色值字符串,如:#ffdd00
*/
fun getCircleBg(colorStr: String) = getCircleBg(Color.parseColor(colorStr))
2.用xml画带边框的圆
- 效果图
- 用xml的shape实现
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFF68F" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
- 用代码实现
如果要画空心圆,就不要调用“setColor”方法
private fun getCircle() = GradientDrawable().also {
it.shape = GradientDrawable.OVAL
val strokeWidth = 4 // 边框宽度
val strokeColor = Color.parseColor("#FFFFFF") //边框颜色
it.setStroke(strokeWidth, strokeColor)
it.setColor(Color.parseColor("#FFF68F"))
}
相关文章
- Android之shape综述篇
- C++程序设计:原理与实践(进阶篇)17.7 使用Shape类
- [UWP]用Shape做动画(2):使用与扩展PointAnimation
- [UWP]实用的Shape指南
- Shape Triangle Rectangle Circle类的实现
- Delphi 图形组件(Shape)
- android shape实现阴影或模糊边效果
- 安卓开发笔记(三十一):shape标签下子类根结点的具体使用
- [LintCode] Shape Factory 形状工厂
- 10 Technologies That will Shape Future Education--reference
- image.shape[0]、[1]分别是高和宽