簡體   English   中英

如何在Android drawable中為環形制作圓角

[英]How to make round corners for a ring shape in Android drawable

我有一個自定義進度條,如下所示:

在此處輸入圖片說明

這是我用來創建它的 .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>

我想要的是我用來顯示進度的戒指的圓角。 看起來像這樣的東西:

在此處輸入圖片說明

有人對如何實現這一目標有任何想法嗎?

好吧,我發現做我想做的最簡單的方法是在畫布上繪制進度弧,而不是使用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)
    }
}

好吧,我之前搜索了很多。

我發現的唯一的解決辦法是在GitHub上檢查庫在這里

你需要使用android:radius標簽。

例如:

<corners android:radius="10dp"/>

仔細閱讀此代碼希望這對您有所幫助

>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_width="match_parent"
        android:layout_height="match_parent"
        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_width="wrap_content"
            android:layout_centerInParent="true"
            android:layout_height="wrap_content">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="250dp"
                android:layout_height="250dp"
                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_width="wrap_content"
                android:layout_height="wrap_content"
                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" >

我能夠使用圖層列表實現這一點,並在線的每一側添加一個點。 第一個將被卡在頂部,而第二個將跟隨進度,因為在旋轉元素內添加了插入。 您必須根據進度條布局的大小進行調整。 我的是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>

在此輸入圖像描述 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM