Android 自定义任意进度的好评星星(感觉有点怪怪的)

2016-07-22· 2858 次浏览
``` public class MyStarView extends View {     private Context mContext;     private Paint mPaint;     private Bitmap bgStar;     private Bitmap star;     private Rect mSrcRect, mDestRect;     private int starWidth, starHeight;       private int interval;     private int redStarCount = 0;     private float redStarCount2 = 0;     private int totalStarWith = 0;         public MyStarView(Context context) {         this(context, null);     }       public MyStarView(Context context, AttributeSet attrs) {         this(context, attrs, 0);     }       public MyStarView(Context context, AttributeSet attrs, int defStyleAttr) {         super(context, attrs, defStyleAttr);         this.mContext = context;           totalStarWith = convertDIP2PX(55);         starWidth = convertDIP2PX(11);         starHeight = convertDIP2PX(10);         interval = convertDIP2PX(5);           mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);         bgStar = ((BitmapDrawable) ContextCompat.getDrawable(mContext, R.drawable.img_star_grey)).getBitmap();         star = ((BitmapDrawable) ContextCompat.getDrawable(mContext, R.drawable.img_star_red)).getBitmap();         mSrcRect = new Rect(0, 0, starWidth, starHeight);         mDestRect = new Rect(0, 0, starWidth, starHeight);     }       @Override     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {         setMeasuredDimension(MeasureSpec.makeMeasureSpec(totalStarWith + 4 * interval, MeasureSpec.EXACTLY),                 MeasureSpec.makeMeasureSpec(starHeight, MeasureSpec.EXACTLY));     }       @Override     protected void onDraw(Canvas canvas) {         mSrcRect.set(0, 0, starWidth, starHeight);         for (int i = 0; i < 5; i++) {             mDestRect.set((interval + starWidth) * i, 0, (interval + starWidth) * i + starWidth, starHeight);             canvas.drawBitmap(bgStar, mSrcRect, mDestRect, mPaint);         }         for (int j = 0; j < redStarCount; j++) {             mDestRect.set((interval + starWidth) * j, 0, (interval + starWidth) * j + starWidth, starHeight);             canvas.drawBitmap(star, mSrcRect, mDestRect, mPaint);         }         if(redStarCount2 != 0) {             mSrcRect.set(0, 0, (int)(redStarCount2 * starWidth), starHeight);             mDestRect.set((interval + starWidth) * redStarCount, 0, (interval + starWidth) * redStarCount + (int)(redStarCount2 * starWidth), starHeight);             canvas.drawBitmap(star, mSrcRect, mDestRect, mPaint);         }     }       public void setStar(float star) {         float w = star / 100 * totalStarWith;         float starW = w / starWidth;         redStarCount = (int)starW; // 整数个星星         redStarCount2 = starW - redStarCount; // 不完整的星星         requestLayout(); //        invalidate();     }       //转换dip为px     public int convertDIP2PX(int dip) {         float scale = mContext.getResources().getDisplayMetrics().density;         return (int)(dip*scale + 0.5f*(dip>=0?1:-1));     } } ``` 先贴代码,等有时间了再来详细的说一下然后配图。