Android Custom Simple controls-star rating

Source: Internet
Author: User

Realize
Package Com.easypass.carstong.view;import Android.content.context;import Android.content.res.TypedArray; Import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.paint;import Android.support.annotation.attrres;import Android.support.annotation.NonNull; Import Android.support.annotation.nullable;import Android.util.attributeset;import Android.view.view;import COM.EASYPASS.CARSTONG.R;/** * Created by Huangbo on 2017/8/1.*/ Public classViewstar extends View { Public StaticFinalintMax_star =5;  PublicViewstar (@NonNull context context) { This(Context,NULL); }     PublicViewstar (@NonNull context context, @Nullable AttributeSet attrs) { This(Context, Attrs,0); }     PublicViewstar (@NonNull context context, @Nullable AttributeSet attrs, @AttrResintdefstyleattr)        {Super (context, attrs, defstyleattr); TypedArray TypedArray=context.obtainstyledattributes (Attrs, R.styleable.starview); Mrating= Typedarray.getfloat (r.styleable.starview_rating,0);        Typedarray.recycle ();    Init ();    } Paint paint;    Bitmap Staryellow;    Bitmap Stargray; floatmrating; intStarwidth; intStarheight; intGap; Private voidinit () {Paint=NewPaint (); Staryellow=Bitmapfactory.decoderesource (Getresources (), r.mipmap.rating_star_yellow); Stargray=Bitmapfactory.decoderesource (Getresources (), R.mipmap.rating_star); Starwidth=staryellow.getwidth (); Starheight=staryellow.getheight (); Gap=5;    Invalidate (); }     Public voidSetrating (floatrating) {         This. mrating =rating;    Invalidate (); }     Public voidSetgraystar (intResId,intAlpha) {Stargray=Bitmapfactory.decoderesource (Getresources (), resId);    Invalidate (); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec)        {super.onmeasure (Widthmeasurespec, Heightmeasurespec); intWidthmode =Measurespec.getmode (WIDTHMEASURESPEC); intWidthsize =measurespec.getsize (WIDTHMEASURESPEC); intHeightmode =Measurespec.getmode (HEIGHTMEASURESPEC); intHeightsize =measurespec.getsize (HEIGHTMEASURESPEC); intwidth = (Getpaddingleft () + (starwidth + gap) * Max_star +getpaddingright ()); intHeight = (getpaddingtop () + Starheight +Getpaddingbottom ()); Setmeasureddimension (Widthmode= = measurespec.exactly?Widthsize:width, Heightmode= = measurespec.exactly?heightsize:height); } @Overrideprotected voidOnDraw (canvas canvas) {super.ondraw (canvas); floatcurrentrating = Mrating <0?0: (Mrating > Max_star?)max_star:mrating); intMleft =0; intMtop =0; intFull = (int) currentrating;/*the number of the whole star*/        /** * Paint the whole yellow star*/         for(inti =0; I < full; i++) {Canvas.drawbitmap (Staryellow, Mleft, mtop, paint); Mleft= Mleft + Starwidth +Gap; }        if(Currentrating = =Max_star) {            return; }        /** * Draw the whole gray star*/         for(inti = full; i < Max_star; i++) {Canvas.drawbitmap (Stargray, Mleft, mtop, paint); Mleft= Mleft + Starwidth +Gap; }        /** * Draw the star of the decimal part*/        floatPart = mrating-Full ; if(Part >0) {            intW = (int) (PART *starwidth); Bitmap Partbitmap= Bitmap.createbitmap (Staryellow,0,0, W, Staryellow.getheight ()); Canvas.drawbitmap (Partbitmap, full* (Starwidth +Gap), mtop, paint); }    }}
How to use

1. Using in the layout file

<your.package.name.ViewStar   android:layout_width="wrap_content"    app:rating="2.5"   android:layout_height="  Wrap_content"/>

2. Use in code

Viewstar star=New Viewstar (this); Star.setrating (1.5);

Android Custom Simple controls-star rating

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.