自定义RatingBar

1.res->values->建立attrs.xml



    
        
        
        
        
        
        
        
        
        
        
        
        
    

2.自定义View命名MyRatingBar

package com.example.mypaint;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class MyRatingBar extends View {
    private Bitmap mStartNomal;
    private Bitmap mStartFocus;
    private int totalNumner=5;
    private int selectNumber=0;
    private int intervalNumner=10;
    private boolean isSelect=true;
    public MyRatingBar(Context context) {
        this(context,null);
    }

    public MyRatingBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyRatingBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyRatingBar);
        int image1=typedArray.getResourceId(R.styleable.MyRatingBar_statrNormal,0);
        int image2=typedArray.getResourceId(R.styleable.MyRatingBar_statrFocus,0);
        intervalNumner= (int) typedArray.getDimension(R.styleable.MyRatingBar_intervalNumner,px2dp(intervalNumner));
        mStartNomal= BitmapFactory.decodeResource(getResources(),image1);
        mStartFocus= BitmapFactory.decodeResource(getResources(),image2);
        totalNumner=  typedArray.getInt(R.styleable.MyRatingBar_totalNumner,totalNumner);
        selectNumber=  typedArray.getInt(R.styleable.MyRatingBar_selectNumber,selectNumber);
        isSelect=  typedArray.getBoolean(R.styleable.MyRatingBar_isSelect,isSelect);
        typedArray.recycle();
    }
    private int px2dp(int yhsize) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,yhsize,getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //图片的高度
        int height=mStartNomal.getHeight();
        int wight=mStartNomal.getWidth()*totalNumner+(intervalNumner-1)*totalNumner;
        setMeasuredDimension(wight,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < totalNumner; i++) {
            int x=i*mStartNomal.getWidth()+intervalNumner*i;
            if (selectNumber>i){
                canvas.drawBitmap(mStartFocus,x,0,null);
            }else{
                canvas.drawBitmap(mStartNomal,x,0,null);
            }

        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN://按下
                if (isSelect){
                    float movex=event.getX();
                    int num=mStartNomal.getWidth()+intervalNumner;
                    int current= (int) (movex/num+1);
                    if (current<0){
                        current=0;
                    }
                    if (current>totalNumner){
                        current=totalNumner;
                    }
                    selectNumber=current;
                    //刷新
                    invalidate();
                }


            case MotionEvent.ACTION_MOVE://移动

            case MotionEvent.ACTION_UP://抬起

        }
        return super.onTouchEvent(event);
    }
}

3.布局中直接引用

 

4如果你想定制自己APP请咨询山东六牛网络科技有限公司

你可能感兴趣的:(自定义RatingBar)