Right to Left ProgressBar?

前端 未结 8 1885
陌清茗
陌清茗 2020-12-05 23:50

Does anyone know how to make a View reversed, I have a horizontal ProgressBar and I want it to right to left instead of left to right

相关标签:
8条回答
  • 2020-12-05 23:56

    You can flip a view in xml using scaleX or scaleY attributes

        <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleX="-1"/>
    
    0 讨论(0)
  • 2020-12-06 00:05

    if you want it in XML there are two properties you can use. if you want to use android:layoutDirection="rtl" it requires minimum API 17 but if you use android:rotation="180" there is no API limitation

    <ProgressBar
            android:progress="20"
            android:rotation="180"
            style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    0 讨论(0)
  • 2020-12-06 00:09

    Cos I'm lazy I just add these two lines to the seekbar xml:

    android:layoutDirection="rtl"
    android:mirrorForRtl="true"
    

    Any downsides to this?

    0 讨论(0)
  • 2020-12-06 00:15

    Make a subclass of the normal progress bar view and implement the onDraw method to rotate the canvas before drawing it:

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.rotate(180,<CenterX>,<CenterY>);
        super.onDraw(canvas);
        canvas.restore();
    }
    

    This should do the trick.

    0 讨论(0)
  • 2020-12-06 00:16
     public class inverseSeekBar extends ProgressBar {
    
    public inverseSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    
    public inverseSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    
    public inverseSeekBar(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
         canvas.save(); 
    
            //now we change the matrix
            //We need to rotate around the center of our text
            //Otherwise it rotates around the origin, and that's bad. 
            float py = this.getHeight()/2.0f;
            float px = this.getWidth()/2.0f;
            canvas.rotate(180, px, py); 
    
            //draw the text with the matrix applied. 
            super.onDraw(canvas); 
    
            //restore the old matrix. 
            canvas.restore(); 
    }}
      <com.hlidskialf.android.widget.inverseSeekBar 
    
           style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" 
    
    /> 
    

    mypackage: com.test.testProgressBar

    0 讨论(0)
  • 2020-12-06 00:17

    You don't need to rotate the entire View.

    Just use a single xml attribute in your my_progress_drawable.xml:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item
            android:id="@android:id/background"
            android:drawable="@drawable/my_background"/>
        <item android:id="@android:id/progress">
            <clip
                android:drawable="@drawable/my_right_to_left_progress"
                android:gravity="right" /> <!-- Clip the image from the RIGHT -->
        </item>
    
    </layer-list>
    

    The documentation tells us that gravity="right" does this:

    Put the object at the right edge of its container, not changing its size. When clipOrientation is "horizontal", clipping occurs at the left side of the drawable.

    Don't override onDraw(). This implementation is more stable across different versions of Android.

    Unfortunately, it's impossible to set the gravity of a ClipDrawable programmatically without invoking its constructor.

    0 讨论(0)
提交回复
热议问题