Android -purpose of useLevel in shape tag

10,333

It is for ProgressBars. For example this gist uses a ring shape as a progress drawable.

res/drawable/github_232_circular.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:innerRadiusRatio="2.3"
       android:shape="ring"
       android:thickness="3.8sp"
       android:useLevel="true">
    <solid android:color="#ff0000" />
</shape>

In your layout:

<ProgressBar
    android:id="@+id/progress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_gravity="top|end"
    android:max="100"
    android:progress="0"
    android:progressDrawable="@drawable/github_232_circular"
/>

Basically, useLevel makes it so the drawable can be drawn partially. For example there is a method ImageView.setImageLevel() that lets you set a "level," e.g. 25% progress, so the ring drawable would be drawn as a quarter-circle. And ProgressBar.setProgress() does the same thing, updating the "level" of the drawable you've set on the ProgressBar.

Share:
10,333
j2emanue
Author by

j2emanue

A mobile developer of both IOS and Android platforms

Updated on July 06, 2022

Comments

  • j2emanue
    j2emanue almost 2 years

    What is the purpose of android shape xml tag useLevel attribute ? This is in respect to a layer-list but should not matter. From the docs i found the meanging of the useLevel tag:

    Indicates whether the drawable's level affects the way the gradient is drawn.

    So if i have the following xml snippet:

        <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="ring"
        android:innerRadius="15dp"
        android:thickness="10dp"
        android:useLevel="false">
    
        <solid android:color="#ababf2" />
    
        <size
            android:height="50dp"
            android:width="50dp" />
    </shape>
    

    then with the useLevel=true, the ring is disabled. It must be false for the ring to appear. But what is the purpose of this attribute ? The docs are not clear.