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