Android ProgressBar reverse progress bar/progress bar from right to left

Source: Internet
Author: User

Android ProgressBar reverse progress bar/progress bar from right to left

For a recent project, you need to use a bar chart to display the ratio and align it right. For details, see:

I thought of using the progress bar, so I don't need to dynamically draw a bar in the code, saving a lot of work.

How can I display the progress bar from right to left?

Solution 1:

Use the background of the ProgressBar, set the foreground color to the same color as the entire large background, and set the background to the color of the bar chart you want, so that you cannot see the actual progress, the progress background is displayed, and the right alignment is displayed visually. Actually, it is still the original progress bar.

Disadvantage: I put this progress bar in the item of the ListView. Click listview, because the selected background of the listview is displayed, the foreground color of the ssssbar is displayed.

Solution 2:

Override the onDraw function of ProgressBar and rotate the Canvas 180 degrees to achieve the progress from right to left. This solution is theoretically feasible. If someone rotates 90 degrees online, you can. refer:

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(-90);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);break;}

 

Disadvantages of this solution: it is complicated to calculate the center of rotation. Because I am not familiar with canvas rotation, I finally failed to rotate 180 degrees. Which of the following experts did it and told me to learn together?

Solution 3:

This solution is the best solution, simple and efficient! Draw a progress bar in reverse direction using the Drawable attributes. Comment out backgroud and do not display backgroud. Then, add two key attributes to the foreground color: android: clipOrientation = horizontal, android: gravity = right, in this way, you can draw a progress bar from right to left.

 

 
 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 to the right of the container without changing its size. When clipOrientation is set to horizontal, It is cropped on the left of the resource that can be drawn.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: [email protected] and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.