2010-10-07 24 views
13

Có ai biết cách tạo Chế độ xem bị đảo ngược không, tôi có ProgressBar nằm ngang và tôi muốn nó sang phải sang trái thay vì từ trái sang phảiTiến trình từ phải sang tráiBar?

+1

ngôn ngữ gì? Nền tảng gì? API gì? – EboMike

+0

Tại sao bạn cần thanh tiến trình RTL? Tất cả các thanh tiến trình tôi đã nhìn thấy đi từ trái sang phải. –

+0

Chúng tôi chỉ có thể trả lời các câu hỏi giả mã với mã giả ... nếu không, vui lòng nêu rõ ý kiến ​​của EboMike yêu cầu. – BoltClock

Trả lời

14

Tạo lớp con của chế độ xem thanh tiến trình bình thường và triển khai phương pháp onDraw để xoay canvas trước khi vẽ:

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

Điều này cần thực hiện thủ thuật.

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

Tuyệt vời! Điều này hoạt động hoàn hảo, cảm ơn! – Tom

8

Bạn không cần phải xoay toàn bộ View.

Chỉ cần sử dụng a single xml attribute trong my_progress_drawable.xml của bạn:

<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 cho chúng ta biết gravity="right" thực hiện điều này:

Đặt các đối tượng ở rìa bên phải của thùng chứa của nó, chứ không phải thay đổi kích thước của nó. Khi clipOrientation là "nằm ngang", việc cắt xảy ra ở phía bên trái của đối tượng có thể vẽ được.

Đừng ghi đè onDraw(). Triển khai này ổn định hơn trên các phiên bản Android khác nhau.

Thật không may, không thể đặt trọng số của ClipDrawable theo chương trình mà không cần gọi constructor.

+0

Đối với chủ đề holo mà sử dụng hơn , scaleGravity có thể được sử dụng thay – FunkTheMonk

54

Thậm chí còn dễ dàng hơn. Bạn có thể chỉ cần gọi phương thức sau và sau đó nó được xoay và hoạt động theo cách bạn muốn.

progressBar.setRotation(180); 

Một ví dụ: a normal progressbar and a RTL progressbar

+0

nhờ cho câu trả lời –

+0

phải là câu trả lời tốt nhất cho đến nay –

+1

tai nạn ứng dụng của tôi ... – Hamidreza

1

Cos Tôi lười biếng tôi chỉ cần thêm hai dòng sau vào seekbar xml:

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

Bất kỳ nhược điểm này?

8

Bạn có thể lật một cái nhìn trong xml sử dụng scaleX hoặc scaleY thuộc tính

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

Đây phải là câu trả lời hay nhất –

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" /> 
Các vấn đề liên quan