【问题标题】:How to set gradient in stroke android drawable如何在stroke android drawable中设置渐变
【发布时间】:2022-06-11 05:48:22
【问题描述】:

我正在尝试用破折号创建一个可绘制的圆形。我能够用破折号实现圆圈,但无法应用渐变色。有什么办法吗?提前致谢。

 <?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>

实现的视图:

需要查看:

【问题讨论】:

    标签: android android-layout android-xml android-drawable


    【解决方案1】:

    没有办法仅使用 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 * 2
            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
        }
    }
    

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

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

    对于 API 24 之前的 API,您需要以编程方式使用此自定义可绘制对象。

    【讨论】:

    • 谢谢,Cheticamp。我在哪里可以学习这种类型的自定义可绘制对象?
    • 在线查找有关使用画布的任何教程。我认为 Google 在他们的学习实验室中有一些经验教训。
    猜你喜欢
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多