我正在try 创建一个可以用破折号绘制的圆.我可以用破折号实现圆形,但不能应用渐变色.有什么办法吗?提前谢谢.

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:width="@dimen/size60"
        android:height="@dimen/size60" />


    <stroke

        android:width="1dp"
        android:color="@color/white"
        android:dashWidth="3dp"
        android:dashGap="1dp" />

</shape>

获得的视图:

enter image description here

需要查看:

enter image description here

推荐答案

仅使用XML AFAIK无法创建渐变环.使用定制的可拉丝面料,您会有更好的运气.下面将扫掠渐变着色器与Paint对象相结合,以创建从起点到终点具有渐变的环.

class DashedRingDrawable : Drawable() {

    private val mPaint = Paint().apply {
        style = Paint.Style.STROKE
        strokeWidth = STROKE_WIDTH
    }

    private val mColorArray = intArrayOf(Color.WHITE, Color.BLACK)

    private var mRingOuterDiameter = 0f
    private var mRingOuterRadius = 0f
    private var mRingInnerRadius = 0f

    override fun onBoundsChange(bounds: Rect) {
        super.onBoundsChange(bounds)
        check(bounds.width() == bounds.height()) {
            "Width must be equal to height. (It's a circle.)"
        }
        mRingOuterDiameter = bounds.width().toFloat()
        mRingOuterRadius = mRingOuterDiameter / 2
        mRingInnerRadius = (mRingOuterDiameter - STROKE_WIDTH) / 2
        val dashLength = getNewDashLength()
        mPaint.pathEffect = DashPathEffect(floatArrayOf(dashLength, GAP_LENGTH), 0f)
        mPaint.shader = SweepGradient(mRingOuterRadius, mRingOuterRadius, mColorArray, null)
    }

    override fun draw(canvas: Canvas) {
        // The following statement is here to show the boundaries and can be removed/commented out.
//        canvas.drawColor(Color.RED)
        canvas.drawCircle(mRingOuterRadius, mRingOuterRadius, mRingInnerRadius, mPaint)
    }

    override fun setAlpha(alpha: Int) {
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
    }

    override fun getOpacity(): Int {
        return PixelFormat.OPAQUE
    }

    // Adjust the dash length so that we end on a gap and not in the middle of a dash.
    private fun getNewDashLength(): Float {
        val circumference = Math.PI.toFloat() * mRingInnerRadius
        val dashCount = (circumference / (DASH_LENGTH + GAP_LENGTH)).toInt()
        val newDashLength = (circumference - dashCount * GAP_LENGTH) / dashCount
        return newDashLength
    }

    companion object {
        const val STROKE_WIDTH = 15f
        const val DASH_LENGTH = 50f
        const val GAP_LENGTH = 15f
    }
}

enter image description here

对于API 24及更高版本,您可以将此可绘制文件放入XML文件中,并像其他任何XML可绘制文件一样使用它.

<drawable xmlns:android="http://schemas.android.com/apk/res/android"
    class="com.example.myapp.DashedRingDrawable"/>

对于API 24之前的API,您将需要以编程方式使用此自定义绘图.

Android相关问答推荐

我到底应该如何分享我的应用程序中的图片?

Jetpack Compose主导航中的全屏图标列表

安卓喷气背包组成倒计时动画

Android写/读二进制文件到共享存储

如何隐藏OutlinedTextField边框上的提示标签

如何在Jetpack Compose android中使用导航

在 kotlin 上向适配器添加绑定视图功能

将输出写入已发布的 Android 应用程序中的日志(log)文件?

Android 访问任意公共目录

Android 模拟器 Wifi 连接没有互联网

Android Studio:按下前缀键:切换 Logcat 格式

如何正确地将图像上传到 Jetpack Compose 中的 LazyList 中的项目?

在事件中使用 Context/Toast 时不需要的重组 - Jetpack Compose

如何将一个 Composable 作为其参数传递给另一个 Composable 并在 Jetpack Compose 中显示/运行它

如何在最后一个可见项目之后计算惰性列中的空白空间

Hilt 依赖注入重复绑定错误

在jetpack compose中将图像添加到脚手架顶部栏

更新后 Firebase 服务无法在模拟器上运行

Android WebView 没有在第一次页面完成时从本地存储读取数据?

更新应用程序是否会取消对应用程序特定文件的权限?