2010-10-07 166 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" /> 
相关问题