In Android development, we often use the evaluation of businesses or commodities, using stars to grade. However, in the Android system with the scoring control, Ratingbar is particularly difficult to use, spacing and size can not be changed. So, I've customized a very useful scoring control. In the project can be used directly, particularly simple. The following directly figure:
Effect chart
Implementation principle
In fact, the custom view inherits LinearLayout and adds five ImageView to the dynamic.
Implementation code, with detailed comments
Variables declared in Attrs that can be set in XML
<declare-styleable name= "Ratingbar" >
<!--dimension value--> <attr name= "Starimagesize"
Dimension "/>
<!--star spacing-->
<attr name=" starpadding "format=" Dimension "/>
<!--The total number of stars- >
<attr name= "Starcount" format= "integer"/>
<!--blank Star resource file value--> <attr name=
"Starempty "format=" reference/>
<!--full-Star resource file value--> <attr name= "Starfill"
format= "reference"/>
<!--half Star resource file value-->
<attr name= "starhalf" format= "reference"/>
<!--can click a Boolean value-->
<attr name= "Clickable" format= "boolean"/>
<!--current progress float value--> <attr name=
"Starstep" format= "Float"/>
<!--The value of each progress method, the whole or half star-->
<attr name= "stepsize" >
<enum name= "Half" value= "0"/>
<enum name= "full" value= "1"/>
</attr>
</declare-styleable>
Ratingbar Source
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import android.graphics.drawable.Drawable;
Import Android.util.AttributeSet;
Import Android.view.View;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import COM.KEJIANG.YUANDL.R;
Import Java.math.BigDecimal;
/** * Created by Dylan on 2015/6/11. * Custom Score Control Ratingbar * can customize star size and spacing * correction clickevent from XML/public class Ratingbar extends LinearLayout {/** *
Whether you can click the * private Boolean mclickable;
/** * The total number of stars * * private int starcount;
/** * STAR Click event * * Private Onratingchangelistener Onratingchangelistener;
/** * The size of each star * * Private float starimagesize;
/** * The spacing of each star * * Private float starpadding;
/** * STAR display number, support decimal point * * Private float starstep;
/** * Blank default star picture * * Private drawable staremptydrawable;
/** * Selected stars fill the picture * * * Private drawable starfilldrawable;
/** * The picture of Half star * * Private drawable starhalfdrawable;
/** * The increase in the number of stars per click is the whole or half of the private stepsize stepsize; /** * Set a half star picture resource file * * @param Starhalfdrawable */public void setstarhalfdrawable (drawable starhalfdrawable) {this.starhalfdrawable = starhalfdrawable;}/** * Settings Mars's Picture resource file * * @param starfilldrawable/public void setstarfilldrawable (drawable starfilldrawable) {this.starfilldrawab
Le = starfilldrawable; /** * Set blank and default picture resource file * * @param staremptydrawable/public void setstaremptydrawable (drawable staremptydrawable) {This
. staremptydrawable = staremptydrawable;
/** * Set whether the star can click Operation * * @param clickable */public void setclickable (Boolean clickable) {this.mclickable = clickable;} /** * Set Star Click event * * @param onratingchangelistener/public void Setonratingchangelistener (Onratingchangelistener onrating ChangeListener) {This.onratingchangelistener = Onratingchangelistener}/** * Set the size of the star * * @param starimagesize/Public void Setstarimagesize (float starimagesize) {this.starimagesize = starimagesize;} public void Setstepsize (Stepsize StepS ize) {this.stepsize = stepsize}/** * constructor * Gets the resource file set in the XML * * @param context * @pAram Attrs * * Public ratingbar (context, AttributeSet attrs) {Super (context, attrs); SetOrientation (LinearLayout.
horizontal);
TypedArray Mtypedarray = context.obtainstyledattributes (Attrs, R.styleable.ratingbar);
Starimagesize = Mtypedarray.getdimension (r.styleable.ratingbar_starimagesize, 20);
starpadding = Mtypedarray.getdimension (r.styleable.ratingbar_starpadding, 10);
Starstep = Mtypedarray.getfloat (R.styleable.ratingbar_starstep, 1.0f);
Stepsize = Stepsize.fromstep (Mtypedarray.getint (r.styleable.ratingbar_stepsize, 1));
Starcount = Mtypedarray.getinteger (R.styleable.ratingbar_starcount, 5);
staremptydrawable = mtypedarray.getdrawable (r.styleable.ratingbar_starempty);
starfilldrawable = mtypedarray.getdrawable (R.styleable.ratingbar_starfill);
starhalfdrawable = mtypedarray.getdrawable (r.styleable.ratingbar_starhalf);
mclickable = Mtypedarray.getboolean (r.styleable.ratingbar_clickable, true);
Mtypedarray.recycle (); for (int i = 0; i < Starcount ++i) {final ImageView imaGeview = Getstarimageview ();
Imageview.setimagedrawable (staremptydrawable);
Imageview.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {if (mclickable) {//) integer part of floating-point number
int fint = (int) starstep;
BigDecimal B1 = new BigDecimal (float.tostring (starstep));
BigDecimal b2 = new BigDecimal (integer.tostring (fint));
The decimal part of the floating-point number is float fpoint = b1.subtract (b2). Floatvalue (); if (Fpoint = = 0) {fint = 1;} if (Indexofchild (v) > Fint) {setstar (Indexofchild (v) + 1);} else if (Indexofchild (v) = = Fint) {if (stepsize = = Stepsize.full) {//If the full star is not considered half a star return;//click on the default to add a star each time, click again to become half a star if (imageview.getdrawable (). GetCurrent (). Getconstantstate (). Equals (Starhalfdrawable.getconstantstate ())) {Setstar (Indexofchild (v) + 1);} else {Setstar (Indexofchild (v) + 0.5f);}}
else {Setstar (Indexofchild (v) + 1f);}}
}
}
);
AddView (ImageView);
} setstar (Starstep); /** * Set the parameters of each star * * @return/private ImageView Getstarimageview () {ImageView ImageView = new ImageView (getcOntext ()); Linearlayout.layoutparams layout = new Linearlayout.layoutparams (Math.Round (starimagesize), Math.Round (
starimagesize);/Set the size of each star in a linear layout layout.setmargins (0, 0, Math.Round (starpadding), 0);/set the spacing of each star in a linear layout
Imageview.setlayoutparams (layout);
Imageview.setadjustviewbounds (TRUE);
Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP);
Imageview.setimagedrawable (staremptydrawable);
Imageview.setminimumwidth (10);
Imageview.setmaxheight (10);
return ImageView; /** * Set the number of stars * * @param rating/public void Setstar (float rating) {if (Onratingchangelistener!= null) {Onratingcha
Ngelistener.onratingchange (rating);
} this.starstep = rating;
Integer part int fint = (int) rating of floating-point numbers;
BigDecimal B1 = new BigDecimal (float.tostring (rating));
BigDecimal b2 = new BigDecimal (integer.tostring (fint));
The decimal part of the floating-point number is float fpoint = b1.subtract (b2). Floatvalue (); Sets the selected star for (int i = 0; i < Fint ++i) {(ImageView) Getchildat (i)). Setimagedrawable (starfilldrawable);}//Set not selected star Star for (int i = Fint; i < Starcount; i++) {((ImageView) Getchildat (i)). Setimagedrawable (staremptydrawable);}//decimal point by default add half star if (Fpoint > 0) {((ImageView) G
Etchildat (Fint)). Setimagedrawable (starhalfdrawable); }/** * Action star Click event/public interface Onratingchangelistener {/** * number of stars selected * * @param ratingcount/void Onratingcha
Nge (float ratingcount);
/** * Stars each increase in the way the whole star or half star, enumerated type * Similar to View.gone/public enum stepsize {Half (0), full (1); stepsize (int step) {this.step = step;} public static stepsize fromstep (int step) {for (stepsize f:values ()) {if (F.s
TEP = = Step) {return F;}}
throw new IllegalArgumentException (); The usage in XML <com.kejiang.yuandl.view.ratingbar android:id= "@+id/rb" android:layout_width= "360DP" android:layout _height= "50DP" app:starcount= "5" app:starempty= "@mipmap/star_grey" app:starfill= "@mipmap/star_yellow" app:starhalf = "@mipmap/star_half_yellow" app:starimagesize= "40DP" app:starpadding= "20DP" app:starstep= "1.5" app:stepsize= "half" ></com.kejiang.yuandL.view.ratingbar>
The settings in the activity
Ratingbar ratingbar= (Ratingbar) Findviewbyid (R.ID.RB);
Ratingbar.setclickable (TRUE);/Set Whether you can click
Ratingbar.setstar (2.5f);//Set the number of stars displayed
ratingbar.setstepsize ( RATINGBAR.STEPSIZE.HALF)/Set each Click to add a star or a half star
Ratingbar.setonratingchangelistener (new Ratingbar.onratingchangelistener () {
@Override public
void Onratingchange (float ratingcount) {// Click on the number of stars
to select LOG.D ("Ratingbar", "ratingbar-count=" +ratingcount);
}
);