如何在Android drawable中为环形制作圆角

Posted

技术标签:

【中文标题】如何在Android drawable中为环形制作圆角【英文标题】:How to make round corners for a ring shape in Android drawable 【发布时间】:2019-02-02 03:28:43 【问题描述】:

我有一个如下所示的自定义进度条:

这是我用来创建它的 .xml 代码:

background_drawable.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="@dimen/progress_bar_radial_inner_radius"
    android:thickness="@dimen/progress_bar_radial_thickness"
    android:shape="ring"
    android:useLevel="false" >
    <solid android:color="@color/main_color_alpha"/>
</shape>

progress_drawable.xml

<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="270"
    android:toDegrees="270">
    <shape
        android:innerRadius="@dimen/progress_bar_radial_inner_radius"
        android:thickness="@dimen/progress_bar_radial_thickness"
        android:shape="ring" >
        <solid android:color="@color/main_color"/>
    </shape>
</rotate>

我想要的是一个圆角的圆环,我用它来显示进度。看起来像这样的东西:

有人知道如何实现这一点吗?

【问题讨论】:

形状块有角属性。尽情享受吧:) 【参考方案1】:

我能够使用图层列表来实现这一点,并在线条的每一侧添加一个点。第一个将卡在顶部,而第二个将跟随进度,因为在旋转元素中添加了插图。不过,您必须根据进度条布局的大小对其进行调整。我的是 250dp x 250dp。

progress_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate android:fromDegrees="270" android:toDegrees="270">
        <shape
            android:innerRadiusRatio="2.55"
            android:shape="ring"
            android:thickness="15dp"
            android:useLevel="true">
            <solid android:color="@color/main_color" />
        </shape>
        </rotate>
    </item>
    <item android:bottom="211dp">
        <shape
            android:innerRadiusRatio="1000"
            android:shape="ring"
            android:thickness="7dp"
            android:useLevel="false">
            <solid android:color="@color/main_color" />
        </shape>
    </item>
    <item>
        <rotate>
            <inset android:insetBottom="211dp">
                <shape
                    android:innerRadiusRatio="1000"
                    android:shape="ring"
                    android:thickness="7dp"
                    android:useLevel="false">
                    <solid android:color="@color/main_color" />
                </shape>
            </inset>
        </rotate>
    </item>
</layer-list>

【讨论】:

【参考方案2】:

嗯,我之前搜索过很多。

我找到的唯一解决方案是 GitHub 上的一个库检查 here

【讨论】:

【参考方案3】:

浏览这段代码希望对你有帮助

>ProgressBarActivity.java

        public class ProgressBarActivity extends AppCompatActivity 

            private TextView txtProgress;
            private ProgressBar progressBar;
            private int pStatus = 0;
            private Handler handler = new Handler();

            @Override
            protected void onCreate(Bundle savedInstanceState) 
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_progress);

                txtProgress = (TextView) findViewById(R.id.txtProgress);
                progressBar = (ProgressBar) findViewById(R.id.progressBar);

                new Thread(new Runnable() 
                    @Override
                    public void run() 
                        while (pStatus <= 100) 
                            handler.post(new Runnable() 
                                @Override
                                public void run() 
                                    progressBar.setProgress(pStatus);
                                    txtProgress.setText(pStatus + " %");
                                
                            );
                            try 
                                Thread.sleep(100);
                             catch (InterruptedException e) 
                                e.printStackTrace();
                            
                            pStatus++;
                        
                    
                ).start();

            
        


   > activity_progress.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_
        android:layout_
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.placio.android.custom_progressbar_circular.MainActivity" >


        <RelativeLayout
            android:layout_
            android:layout_centerInParent="true"
            android:layout_>

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_
                android:layout_
                android:layout_centerInParent="true"
                android:indeterminate="false"
                android:max="100"
                android:progress="0"
                android:progressDrawable="@drawable/progress_drawable"
                android:secondaryProgress="0" />


            <TextView
                android:id="@+id/txtProgress"
                android:layout_
                android:layout_
                android:layout_alignBottom="@+id/progressBar"
                android:layout_centerInParent="true"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </RelativeLayout>



    </RelativeLayout>




   > progress_drawable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="-90"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270" >

【讨论】:

【参考方案4】:

好的,我发现做我想做的最简单的方法是在画布上绘制进度弧,而不是使用 progress_drawable.xml。 这是我的代码,以防有人遇到类似问题。

class RadialProgressBar : ProgressBar 

    private val thickness = 28f
    private val halfThickness = thickness / 2
    private val startAngle = 270f
    private var boundsF: RectF? = null
    private lateinit var paint: Paint

    constructor(context: Context?) : super(context) 
        init()
    

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) 
        init()
    

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) 
        init()
    

    private fun init() 
        paint = Paint()
        paint.isAntiAlias = true
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = thickness
        paint.strokeCap = Paint.Cap.ROUND
        paint.color = ContextCompat.getColor(context, R.color.main_color)

        progressDrawable = null
    

    override fun draw(canvas: Canvas?) 
        super.draw(canvas)

        if (boundsF == null) 
            boundsF = RectF(background.bounds)
            boundsF?.inset(halfThickness, halfThickness)
        

        canvas?.drawArc(boundsF, startAngle, progress * 3.60f, false, paint)
    

【讨论】:

这不会画任何东西。你能不能也展示一下这个的 xml 布局? 不幸的是,我必须同意 @SuppressWarnings 。这对我也不起作用。也许您可以提供更多内部信息它是如何工作的。对我来说,draw 只被调用了两次,进度为 0。

以上是关于如何在Android drawable中为环形制作圆角的主要内容,如果未能解决你的问题,请参考以下文章

Android - 使用 xml drawable 制作检查图标

如何在android中为按钮添加图像?

如何在 Android 中为 WebView 制作滚动监听器

无论如何在Android中将Drawable转换为int

如何在 android studio 中为线性布局制作“波浪”边框?

如何在android中为pdf查看器制作注释,如突出显示、删除线、下划线、绘制、添加文本等?