自定义RatingBar评分控件

功能:随着手指滑动或者点击实现星星选中显示(黄色代表选中、灰色代表未选中)。
废话少说,开始开车。下面效果图片:


自定义RatingBar评分控件_第1张图片
RatingBar.gif

功能分析:
1、自定义的属性:



    
        
        
        
        
    

unSelected:未选中显示的图片(灰色星星)
selected:选中显示的图片(黄色的星星)
starNum:绘制星星的数量
gap:每两个星星之间间距
2、自定义RatingBar

public class RatingBar extends View {
    private int mStarNum =5;
    private Bitmap mSelectedBitmap;
    private Bitmap mUnSelectedBitmap;
    private int mCurrentGrade=0;
    private int mGap=5;
    public RatingBar(Context context) {
        this(context,null);
    }
    public RatingBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    public RatingBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RatingBar);
        mStarNum = array.getInteger(R.styleable.RatingBar_starNum, mStarNum);
        mGap=array.getInteger(R.styleable.RatingBar_gap,mGap);
        int selected_resourceId = array.getResourceId(R.styleable.RatingBar_selected, 0);
        if (selected_resourceId==0){
            throw new RuntimeException("请设置selected属性");
        }else{
            mSelectedBitmap= BitmapFactory.decodeResource(getResources(),selected_resourceId);
        }
        int unSelected_resourceId = array.getResourceId(R.styleable.RatingBar_unSelected, 0);
        if (unSelected_resourceId==0){
            throw new RuntimeException("请设置unSelected属性");
        }else{
            mUnSelectedBitmap=BitmapFactory.decodeResource(getResources(),unSelected_resourceId);
        }
        array.recycle();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width_bitmap = mSelectedBitmap.getWidth();
        int height_height = mSelectedBitmap.getHeight();
        int width=width_bitmap*mStarNum+(mStarNum-1)*mGap;//计算控件的宽度
        int height = height_height;//计算控件的高度
        setMeasuredDimension(width,height);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i i){
                canvas.drawBitmap(mSelectedBitmap,x,0,null);
            }else{
                canvas.drawBitmap(mUnSelectedBitmap,x,0,null);
            }
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
//                break;
            case MotionEvent.ACTION_MOVE:
                /*获取到滑动的位置*/
                float x = event.getX();
                int currentGrade= (int) (x/(float) (mSelectedBitmap.getWidth()+mGap)+1);
                if (currentGrade<0){
                    mCurrentGrade=0;
                }
                if (currentGrade>mStarNum){
                    mCurrentGrade=mStarNum;
                }
                if (mCurrentGrade==currentGrade){
                    return true;
                }
                mCurrentGrade=currentGrade;
                invalidate();
                break;
//            case MotionEvent.ACTION_UP:
//                break;
        }
//        return super.onTouchEvent(event);
        return true;//设置返回为true,否则不会进入onTouchEvent()这个方法。
    }
}

这里把MotionEvent.ACTION_DOWN事件加入进来实现点击也可以实现评分,否则评分效果只有滑动才能实现评分,点击不能实现评分。

你可能感兴趣的:(自定义RatingBar评分控件)