How does the ProgressBar work?

Background

I wanted to make a rounded progress bar that is deterministic (this means android: undefined = "false" ), so I searched the Internet and found a short answer by Romain Guy, here .

So, I grabbed the code and used it in a sample project:

layout file (part):

<ProgressBar
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@color/backColor"
    android:indeterminate="false"
    android:indeterminateOnly="false"
    android:max="100"
    android:progress="33"
    android:progressDrawable="@drawable/progress" />

hood / progress.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<!--    <item android:drawable="@drawable/progress_circular_background"/> -->
    <item>
        <shape  
            android:innerRadiusRatio="3.4"
            android:shape="ring"
            android:thicknessRatio="6.0" >
            <gradient
                android:endColor="#ffffffff"
                android:startColor="#ff000000"  
                android:type="sweep"
                android:useLevel="true" />

        </shape>
    </item>

    <item>
        <rotate
            android:drawable="@drawable/progress_particle"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="360" />
    </item>

</layer-list>

screenshots (not quite from the current code):

enter image description here

Question

It works great, but I don’t understand how it works.

How does the progress indicator know what exactly to change in the drawings, and how?

For example, how does he know how to take only the shape of a ring from 0 degrees to the right, and not from other places?

Is it possible to configure its work?

+4
1

ProgressBar . doRefreshProgress():

final int level = (int) (scale * MAX_LEVEL);
(progressDrawable != null ? progressDrawable : d).setLevel(level);

A - , , , Drawable.

, , , .

true, Drawable (, invalidate), false.

, a GradientDrawable ( useLevel="true", , ) , , . , :

final float level = st.mUseLevel ? (float) getLevel() / 10000.0f : 1.0f;    
x0 = r.left;            y0 = r.top;
x1 = level * r.right;   y1 = y0;

, , , 360- :

float sweep = st.mUseLevelForShape ? (360.0f * getLevel() / 10000.0f) : 360f;

, setProgress() , .

RotateDrawable, progress_particle ( 90 ) :

mState.mCurrentDegrees = mState.mFromDegrees +
    (mState.mToDegrees - mState.mFromDegrees) * ((float) level / MAX_LEVEL);

, , " , 0 " , . Ring GradientDrawables . :

// inner top
ringPath.moveTo(x + radius, y);
// outer top
ringPath.lineTo(x + radius + thickness, y);
+8

Source: https://habr.com/ru/post/1542352/


All Articles