Change ProgressBar. Horizontal undefined behavior

I am trying to achieve a horizontal undefined ProgressBar that will completely change the animation. To clarify, I want the progress bar to display from 0 to 100, and then back from 100 to 0. Here is a standard animation video that I want to cancel at the end.

According to the ProgressBar documentation, this should be possible using xml, but I cannot achieve this. You can either set repeat (standard?), Or loop (this is what I want)

Here is my implementation:

<ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="8dip" android:indeterminate="true" android:indeterminateOnly="true" //tried "false" too android:indeterminateBehavior="cycle" // type "reverse" is the one from linked video? android:max="100" /> 

Tried max-value and other style-parents

However, I found the value of android:indeterminateDuration="[value]" and set the value to 1 second and the other to 10 seconds. In the end, both progressloaders were the same length, which leaves me with the idea that styles can be rewritten somewhere ?!

Does anyone know how to fix this?

Bounty Update: Issue resolved, for a working example, where undefined Behavior works

+6
source share
5 answers
 <layer-list> <item android:id="@android:id/background"> <shape> <corners android:radius="2dip" /> <gradient android:startColor="#43ffffff" android:centerColor="#43ffffff" android:centerY="0.75" android:endColor="#43ffffff" android:angle="270" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="2dip" /> <gradient android:startColor="#fff" android:endColor="#fff" android:angle="270" /> </shape> </clip> </item> 

uses this flexible

Look at this image

+2
source

You can add an event in which, depending on known factors, programmatically execute:

 progressBar.setIndeterminate(true); 

or

 progressBar.setIndeterminate(false); 

considering which style is completely dependent on your analysis of the data presented.

If you know that it will always be undefined and has a consistently expected behavior, add these xml properties:

 android:indeterminate="true" android:indeterminateDrawable="@drawable/progress_indeterminate_horizontal" android:indeterminateOnly="false" 
0
source

I could not find how to change the animator in the ProgressBar , so I suggest you go with your own drawable.

Draw progress as a rectangle

In a trivial example, let progress look like a rectangle:

res/drawable/vector_drawable_progress_indeterminate_horizontal.xml (name inspired by core / res / res / drawable / vector_drawable_progress_indeterminate_horizontal.xml )

 <?xml version="1.0" encoding="utf-8"?> <vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="10dp" android:width="100dp" android:viewportHeight="10" android:viewportWidth="100" > <group android:name="rect1_grp" android:scaleX="1" > <path android:pathData="M 0,0 h 100 v 10 h -100 Z" android:fillColor="?attr/colorControlActivated" /> </group> </vector> 

Animate horizontal scale

Of course, this rectangle is not animated at all, so you want to wrap it in an object that changes its horizontal scale ( scaleX of rect1_grp ).

res/drawable/custom_progress.xml

 <?xml version="1.0" encoding="utf-8"?> <animated-vector android:drawable="@drawable/vector_drawable_progress_indeterminate_horizontal" xmlns:android="http://schemas.android.com/apk/res/android"> <target android:animation="@anim/progress_indeterminate_horizontal_rect1" android:name="rect1_grp"/> </animated-vector> 

The only thing left to do is create your own animation:

res/anim/progress_indeterminate_horizontal_rect1 :

 <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:duration="1500" android:propertyName="scaleX" android:valueFrom="0" android:valueTo="1" android:valueType="floatType" android:repeatMode="reverse" android:repeatCount="infinite"/> </set> 

The animation grows linearly from 0f to 1f and repeats endlessly with the reverse mode (which avoids the sequence of two animators: one for zooming in and one for zooming out).

Enjoy

Now all you have to do is use it in res/layout/activity.xml :

 <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="8dip" android:indeterminate="true" android:indeterminateDrawable="@drawable/custom_progress" /> 

Left to do

  • I divided the progress to a minimum. You might want to get a wallpaper. You can also restore secondary progress.
  • You can adjust the interpolator in the animation.
0
source

Another approach that you might follow is to use a deterministic style and set values ​​from 0 to 100 and from 100 to 0 backward:

  setProgress(100); // sleep animation duration setProgress(0); // sleep animation duration // repeat 

I think this solution is less elegant than with custom inderminateDrawble , because it requires this fake progress to be updated.

0
source

add this to your layout,

  <ProgressBar android:id="@+id/pbProcessing" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/frame_getgas" android:indeterminateOnly="true"/> 
0
source

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


All Articles