自定义RatingBar
2020-11-26 本文已影响0人
张岱熹
1.res->values->建立attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyRatingBar" >
<!--默认图片-->
<attr name="statrNormal" format="reference"/>
<!--选中图片-->
<attr name="statrFocus" format="reference"/>
<!--星星总数量-->
<attr name="totalNumner" format="integer"/>
<!--星间隔-->
<attr name="intervalNumner" format="dimension"/>
<!--是否可以选中-->
<attr name="isSelect" format="boolean"/>
<!--选中星星数量-->
<attr name="selectNumber" format="integer"/>
</declare-styleable>
</resources>
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.布局中直接引用
<com.example.mypaint.MyRatingBar
android:id="@+id/mk"
app:totalNumner="5"
app:intervalNumner="5dp"
app:selectNumber="2"
app:isSelect="true"
app:statrNormal="@mipmap/list_icon_colloect_n"
app:statrFocus="@mipmap/list_icon_colloect_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>