Nik*_*iko 20 android view progress-bar
有没有人知道如何使视图反转,我有一个水平ProgressBar我希望它从右到左而不是从左到右
小智 62
它更容易.您可以简单地调用以下方法然后旋转并按照您想要的方式工作.
progressBar.setRotation(180);
Run Code Online (Sandbox Code Playgroud)
一个例子:

小智 23
您可以使用scaleX或scaleY属性在xml中翻转视图
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="-1"/>
Run Code Online (Sandbox Code Playgroud)
Mos*_*oss 16
创建正常进度条视图的子类并实现onDraw方法以在绘制之前旋转画布:
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.rotate(180,<CenterX>,<CenterY>);
super.onDraw(canvas);
canvas.restore();
}
Run Code Online (Sandbox Code Playgroud)
这应该可以解决问题.
pen*_*ang 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"
/>
Run Code Online (Sandbox Code Playgroud)
mypackage:com.test.testProgressBar
您无需旋转整个View.
只需在您的:中使用单个xml属性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>
Run Code Online (Sandbox Code Playgroud)
文档告诉我们这样gravity="right"做:
将对象放在其容器的右边缘,而不是更改其大小.当clipOrientation为"水平"时,剪裁发生在drawable的左侧.
不要覆盖onDraw().这种实现在不同版本的Android中更加稳定.
不幸的是,如果ClipDrawable不调用其构造函数,就不可能以编程方式设置引力.
小智 7
因为我很懒,所以我只是将这两行添加到seekbarxml 中:
android:layoutDirection="rtl"
android:mirrorForRtl="true"
Run Code Online (Sandbox Code Playgroud)
这有什么缺点吗?
如果要使用XML,则可以使用两个属性。如果要使用android:layoutDirection="rtl"它,则需要最低API 17,但如果使用android:rotation="180",则没有API限制
<ProgressBar
android:progress="20"
android:rotation="180"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)