2010-10-07 179 views
13

有誰知道如何使視圖顛倒,我有一個水平ProgressBar,我希望它從右到左而不是從左到右從右到左ProgressBar?

+1

什麼語言XML格式的看法?什麼平臺?什麼API? – EboMike 2010-10-07 00:13:00

+0

爲什麼你需要一個RTL進度條?我看到的所有進度條都是從左到右。 – 2010-10-07 00:19:40

+0

我們只能用僞代碼回答僞代碼問題......否則請至少指定EboMike的註釋要求的內容。 – BoltClock 2010-10-07 00:28:22

回答

14

使正常進度欄視圖的子類和實現onDraw方法旋轉繪製它之前的畫布:

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

這應該有所斬斷。

14
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

太棒了!這工作完美,謝謝! – Tom 2012-04-10 22:42:39

8

你不需要旋轉整個View

my_progress_drawable.xml只需使用a single xml attribute

<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告訴我們,gravity="right"做到這一點:

把對象在其容器的右邊緣,而不是改變它的大小。當clipOrientation是「水平」時,裁剪髮生在drawable的左側。

請勿覆蓋onDraw()。此實現在不同版本的Android中更穩定。

不幸的是,無法以編程方式設置ClipDrawable的引力而不調用其constructor

+0

對於使用而非全息主題,scaleGravity可用於回答 – FunkTheMonk 2013-08-09 14:53:57

54

它更容易。你可以簡單地調用下面的方法,然後它被旋轉並按照你想要的方式工作。

progressBar.setRotation(180); 

一個例子: a normal progressbar and a RTL progressbar

+0

感謝應該是最好的答案至今 – 2013-12-31 21:41:45

+0

代替 – 2014-04-21 07:22:04

+1

崩潰我的應用程序... – Hamidreza 2014-09-24 08:36:22

1

因爲我是懶惰的我只是這兩行添加到seekbar XML:

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

任何缺點這樣做呢?

8

可以翻轉使用的scaleX或屬性的scaleY

<ProgressBar 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:scaleX="-1"/> 
+1

這應該是最好的答案 – 2017-05-25 07:35:45

1
<ProgressBar 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:max="100" 
    android:layout_margin="8dp" 
    android:layoutDirection="rtl" 
    android:progress="80" />