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

Source: Internet
Author: User

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

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.

<?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, let's take a look at other Drawable attributes:

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 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.



In android, there is a ProgressBar progress bar component and a button in mainxml. How can this progress bar be hidden in the initial state?

In mian. xml, set the visibility of ProgressBar to invisible. In the java file, find the ProgressBar ID through findViewById (), assume it is "progressBar", and finally write progressBar In the listener function of the button. setVisibility (View. VISIBLE); that's it.

C # What should I do when the maximum value of progressbar is unknown? How can I bring the display of the progress bar closer to the actual value?

There are several methods:
One is to prevent progress bars from representing the progress. It is only used to indicate that the current process is in progress and set the Style attribute of ProgressBar to System. windows. forms. progressBarStyle. marquee, this will display a slider sliding left and right on the progress bar
Msdn.microsoft.com/..e.aspx

The second is to calculate the progress. You can calculate the progress by the number of lines or bytes. The number of lines is nothing more than how many lines are read from a text file over and over again. The number of bytes is to get the file size first, then, when reading a row, you also need to accumulate the number of bytes in the row so that the two can form a percentage.

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: info-contact@alibabacloud.com 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.