For recent projects, there is a need to use a bar chart to display proportions, and right-align, see:
I think of using the progress bar, so that you do not need to dynamically draw bars in the code, save a lot of work.
So how does the progress bar show from right to left?
Programme one:
The front background of the ProgressBar, set the foreground color to be the same as the whole background, set the background to the bar chart you want, so that you do not see the actual progress, see the progress background, visually appears to be right-aligned. It's actually the same progress bar.
Cons: My progress bar is placed in the ListView item, click the ListView, because the ListView has a selected background to display, so that the foreground color ProgressBar.
Scenario Two:
Rewrite ProgressBar's OnDraw function to achieve a right-to-left progress effect by rotating the canvas 180 degrees. This scheme is theoretically feasible, the online someone rotated 90 degrees, is possible, refer to the address:
Http://www.xprogrammer.com/234.html
Http://www.verydemo.com/demo_c131_i3507.html
Key code:
@Overrideprotected synchronized void OnDraw (canvas canvas) {switch (curr_mode) {case MODE_BOTTOM:canvas.rotate (- Canvas.translate (-canvas.getheight (), 0); Super.ondraw (canvas); break; Case MODE_TOP:canvas.rotate (90,canvas.getwidth (), 0), canvas.translate (10,0); Super.ondraw (canvas);
Disadvantages of this scenario: the need to calculate the rotation center point, more complex, due to the canvas rotation is not ripe, I finally failed to rotate 180 degrees of success, which the great God has done tell me, learn together
Programme III:
This solution is the best solution, simple and efficient! Draw the progress bar backwards using the properties of the drawable itself. The Backgroud is commented out, the Backgroud is not displayed, and the foreground color is added with two key attributes: android:cliporientation= "Horizontal", android:gravity= "right", This allows you to draw a progress bar from right to left.
<?xml version= "1.0" encoding= "Utf-8"? ><layer-list xmlns:android= "http://schemas.android.com/apk/res/ Android "> <!-- <item android:id=" @android: Id/background " android:drawable=" @drawable/ Progress_bg "/>- <item android:id=" @android: id/progress "> <clip android: cliporientation= "Horizontal" android:gravity= "right" > <scale android:drawable= "@drawable/progress _progress "/> </clip> </item></layer-list>
By the way, learn about the other properties of drawable:
Http://www.cnblogs.com/andriod-html5/archive/2012/04/24/2539432.html
android:cliporientation=["Horizontal" | "Vertical"] android:gravity=["Top" | "Bottom" | "Left" | "Right" | "Center_vertical" | " Fill_vertical "| "Center_horizontal" | "Fill_horizontal" | " Center "| "Fill" | "Clip_vertical" | "Clip_horizontal"]/>
Place the object on the right side of the container without changing its size. When Cliporientation is set to horizontal, it is cropped to the left of the drawing resource.