Android custom RatingBar (scoring Control)

Source: Internet
Author: User

Android custom RatingBar (scoring Control)

RatingBar

RatingBar is an extension based on SeekBar (drag bar) and ProgressBar (status bar). It displays rating using a star. When the default RatingBar is used, you can set ratings by touching, dragging, or pressing buttons (such as remote control). RatingBar comes in two modes: ratingBarStyleSmall, ratingBarStyleIndicator, and ratingBarStyleIndicator, user interaction is not applicable.

The yellow star is the custom RatingBar.
Small green is the built-in ratingBarStyleSmall
Big Green is the self-carried ratingBarStyleIndicator
By setting style = "XXXXXXXXXXXXX", you can switch the settings (for example, style = "? Android: attr/ratingBarStyleIndicator ")

Notes for customizing RatingBar

Generally, the system's built-in RatingBar is far from meeting the development requirements. We can customize a RatingBar Based on the image. Before we start to implement a custom RatingBar, next, let's take a look at the differences between the res directory and the directory where the res directory is stored: there is only one drawable directory in the res directory before android sdk 1.5, and three directories will appear after android sdk 1.6, they are drawable-hdpi, drawable-ldpi, drawable-mdpi, and drawable-xhdpi. (1) high-resolution images such as WVGA (480x800) are stored in drawable-hdpi ), FWVGA (480x854) (2) drawable-mdpi stores medium-resolution images, such as HVGA (320x480) (3) drawable-ldpi stores low-resolution images, for example, QVGA (240x320) (4) drawable-xhdpi stores ultra-high-resolution images, at least 960dp x 720dp

Because the resolution of the star image used in the Custom RatingBar is relatively low, we usually put the image in drawable-mdpi or drawable-ldpi.

Implementation of custom RatingBar

     
          
                                  
                   
   
  

Backgroud: it is used to fill in the background image, which is very similar to the progress bar. When we set the highest score (android: numStars), the system will follow our settings, (If android: numStars = "5", five Gray stars will be drawn)
Progress: indicates the attribute used to fill the background image (similar to the progress bar, the first progress position)
SecondaryProgress: Same as progress, it belongs to the Second progress position (if this is not defined, the progress bar is dragged to draw a whole Star (bright) each time, and the Second progress (dark) the position after the first progress is not covered. The N.5 stars cannot be dragged from left to right, so the score is incomplete)

Secondly, through the introduction, we know that the RatingBar style is switched by style. In android, we can use styles. in the xml file, we need to customize the control type by setting the style attribute, as shown in the following styles. xml

java





Select the inherited parent class through the parent attribute. Here we inherit the RatingBar class.
Redefine the progressDrawable attribute (the background bar of RatingBar, as we described in the first section)
MaxHeight and minHeight can be set based on our image pixels or other reference values.

Finally, add the RatingBar control in the xml configuration file that we need to use RatingBar.


      
  

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.