android自带的checkbox交互体验不好点击一下干巴巴的难看,很早就写了一个demo现在贴上来。
没必要解释很简单了
public class RightView extends View implements View.OnClickListener,ValueAnimator.AnimatorUpdateListener{
private Paint borderPaint;
private float paintWidth=5;
private int animDuration=200;
private int selectColor=Color.RED;
private int unselectColor=Color.GRAY;
private boolean pressed=false;
Path pathLine,dst1;
PathMeasure pathMeasure;
ValueAnimator animator;
private RightViewSelectListener listener;
float v1;
public RightView(Context context) {
this(context,null);
}
public RightView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public RightView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
setOnClickListener(this);
}
private void init() {
setBackgroundColor(Color.WHITE);
borderPaint=new Paint();
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setAntiAlias(true);
pathLine=new Path();
dst1=new Path();
pathMeasure=new PathMeasure();
animator=ValueAnimator.ofFloat(0,1);
animator.setDuration(animDuration);
animator.setInterpolator(new DecelerateInterpolator());
animator.addUpdateListener(this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(measureDimension(widthMeasureSpec),measureDimension(heightMeasureSpec));
}
private int measureDimension(int ms){
int result;
int measureMode=MeasureSpec.getMode(ms);
int measureSize=MeasureSpec.getSize(ms);
if(measureMode==MeasureSpec.EXACTLY){
result=measureSize;
}else {
result=20;
if(measureMode==MeasureSpec.AT_MOST){
result=Math.max(result,measureSize);
}
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rect=new Rect(0,0,getWidth(),getHeight());
if(!pressed){
borderPaint.setColor(unselectColor);
}else{
//点击之后
borderPaint.setStrokeWidth(paintWidth/2);
borderPaint.setColor(selectColor);
pathLine.moveTo(20, (float) (getHeight()*2.0/3));
pathLine.lineTo((float) (getWidth()*1.0/3),getHeight()-20);
pathLine.lineTo(getWidth()-20, (float) (getHeight()*1.0/3));
pathMeasure.setPath(pathLine,false);
pathMeasure.getSegment(0,v1*pathMeasure.getLength(),dst1,true);
canvas.drawPath(dst1,borderPaint);
dst1.reset();
}
borderPaint.setStrokeWidth(paintWidth);
canvas.drawRect(rect,borderPaint);//边框
}
@Override
public void onClick(View v) {
if(pressed){
pressed=false;
invalidate();
}else{
pressed=true;
animator.start();
}
if(listener!=null){
listener.onSelect(pressed);
}
}
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if(animation.equals(animation)){
v1= ((float) animation.getAnimatedValue());
invalidate();
}
}
public boolean getState(){
return pressed;
}
public void setRightViewSelectListener(RightViewSelectListener l){
listener=l;
}
interface RightViewSelectListener{
void onSelect(boolean select);
}
}
这里是xml
<com.sofang.lxm.viewevent.RightView
android:layout_width="50dp"
android:layout_height="50dp" />