public class StarBar extends View { private int starDistance = 0; //星星间距 private int starCount = 5; //星星个数 private int starSize; //星星高度大小,星星一般正方形,宽度等于高度 private float starMark = 0.0F; //评分星星 private Bitmap starFillBitmap; //亮星星 private Bitmap starFillMedium; //亮星星 private Bitmap starFillExcellent; //亮星星 private Drawable starEmptyDrawable; //暗星星 private OnStarChangeListener onStarChangeListener;//监听星星变化接口 private Paint paint; //绘制星星画笔 private boolean integerMark = false; private int code; private int style; public StarBar(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public StarBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } /** * 初始化UI组件 * * @param context * @param attrs */ private void init(Context context, AttributeSet attrs){ setClickable(true); TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RatingBar); this.starDistance = (int) mTypedArray.getDimension(R.styleable.RatingBar_starDistance, 0); this.starSize = (int) mTypedArray.getDimension(R.styleable.RatingBar_starSize, 20); this.starCount = mTypedArray.getInteger(R.styleable.RatingBar_starCount, 5); this.starEmptyDrawable = mTypedArray.getDrawable(R.styleable.RatingBar_starEmpty); this.starFillBitmap = drawableToBitmap(mTypedArray.getDrawable(R.styleable.RatingBar_starFill)); this.starFillMedium = drawableToBitmap(mTypedArray.getDrawable(R.styleable.RatingBar_starMedium)); this.starFillExcellent = drawableToBitmap(mTypedArray.getDrawable(R.styleable.RatingBar_starExcellent)); mTypedArray.recycle(); paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(starFillBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); } /** * 设置是否需要整数评分 * @param integerMark */ public void setIntegerMark(boolean integerMark){ this.integerMark = integerMark; } /** * 设置显示的星星的分数 * * @param mark */ public void setStarMark(float mark){ if (integerMark) { starMark = (int) Math.ceil(mark); }else { starMark = Math.round(mark * 10) * 1.0f / 10; } setFile(starMark*10); if (this.onStarChangeListener != null) { this.onStarChangeListener.onStarChange(starMark*10,this); //调用监听接口 invalidate(); } } /** * 设置显示的星星的分数 * * @param mark */ public void setStarMarkEnabled(float mark){ if (integerMark) { starMark = (int) Math.ceil(mark); }else { starMark = Math.round(mark * 10) * 1.0f / 10; } setFile(starMark*10); this.setEnabled(false); invalidate(); } private void setFile(float starMark) { if (0 < starMark && starMark <=6) { code = 1; } else if (6 < starMark && starMark <=8) { code = 2; } else if (8 < starMark && starMark <= 10) { code = 3; } if (code == 1) { paint.setShader(new BitmapShader(starFillBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); } else if (code == 2) { paint.setShader(new BitmapShader(starFillMedium, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); } else if (code == 3) { paint.setShader(new BitmapShader(starFillExcellent, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); } } /** * 获取显示星星的数目 * * @return starMark */ public float getStarMark(){ return starMark; } /** * 定义星星点击的监听接口 */ public interface OnStarChangeListener { void onStarChange(float mark, StarBar starBar); } /** * 设置监听 * @param onStarChangeListener */ public void setOnStarChangeListener(OnStarChangeListener onStarChangeListener){ this.onStarChangeListener = onStarChangeListener; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // setMeasuredDimension(starSize * starCount + starDistance * (starCount - 1), starSize); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (starFillBitmap == null || starEmptyDrawable == null) { return; } for (int i = 0;i < starCount;i++) { // starEmptyDrawable.setBounds((starDistance + starSize) * i, -100, (starDistance + starSize) * i + starSize, starSize); starEmptyDrawable.setBounds((starDistance + starSize) * i, 0, (starDistance + starSize) * i + starSize, getMeasuredHeight()); starEmptyDrawable.draw(canvas); } if (starMark > 1) { canvas.drawRect(0, 0, starSize, starSize, paint); if(starMark-(int)(starMark) == 0) { for (int i = 1; i < starMark; i++) { canvas.translate(starDistance + starSize, 0); canvas.drawRect(0, 0, starSize, starSize, paint); } }else { for (int i = 1; i < starMark - 1; i++) { canvas.translate(starDistance + starSize, 0); canvas.drawRect(0, 0, starSize, starSize, paint); } canvas.translate(starDistance + starSize, 0); canvas.drawRect(0, 0, starSize * (Math.round((starMark - (int) (starMark))*10)*1.0f/10), starSize, paint); } }else { canvas.drawRect(0, 0, starSize * starMark, starSize, paint); } } public int setStyle(int i){ style = i; return style; } @Override public boolean onTouchEvent(MotionEvent event) { if(event != null){ int x = (int) event.getX(); if (x < 0) x = 0; if (x > getMeasuredWidth()) x = getMeasuredWidth(); switch(event.getAction()){ case MotionEvent.ACTION_DOWN: { if (style == 1){ }else { setStarMark(x*1.0f / (getMeasuredWidth()*1.0f/starCount)); } break; } case MotionEvent.ACTION_MOVE: { if(style == 1){ }else { setStarMark(x*1.0f / (getMeasuredWidth()*1.0f/starCount)); } break; } case MotionEvent.ACTION_UP: { break; } } invalidate(); return super.onTouchEvent(event); } return false; } /** * drawable转bitmap * * @param drawable * @return */ private Bitmap drawableToBitmap(Drawable drawable) { if (drawable == null)return null; Bitmap bitmap = Bitmap.createBitmap(starSize, starSize/10, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, starSize, starSize/10); drawable.draw(canvas); return bitmap; } }
values中 attr.xml中新增
name="RatingBar">
format="dimension" name="starDistance"/>
format="dimension" name="starSize"/>
format="integer" name="starCount"/>
format="reference" name="starEmpty"/>
format="reference" name="starFill"/>
format="reference" name="starMedium"/>
format="reference" name="starExcellent"/>
布局文件中
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ratingbar="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.test2.MainActivity">
android:layout_width="200dp"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
ratingbar:starCount="1"
ratingbar:starDistance="0dp"
ratingbar:starEmpty="@drawable/line_grey3x"
ratingbar:starExcellent="@drawable/line_green3x"
ratingbar:starFill="@drawable/line_red3x"
ratingbar:starMedium="@drawable/line_yellow3x"
ratingbar:starSize="200dp" />
图片资源