您的位置:首页 > 其它

自定义RadioGroup 实现选择

2017-01-15 10:31 239 查看
先来张效果图





去不代码:

package com.example.demo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioGroup;

/**
* Created by Administrator on 2017/1/7 0007.
*/
public class SecletDangType extends View {
//上下文
private Context mContext;
//画笔
private Paint mPaint;
//选中的位置
private int position = 0;
//灰色开始的横坐标
private int start = 0;
//灰色结束的横坐标
private int stop = 0;
//view 宽度
private double width;
//文字的边界
private Rect mBounds;

private Paint.FontMetrics mFontMetrics;

public SecletDangType(Context context) {
super(context);
}

public SecletDangType(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.RED);
mPaint.setTextSize(DensityUtil.sp2px(mContext, 16));
mBounds = new Rect();
mPaint.getTextBounds("手动", 0, "手动".length(), mBounds);

mFontMetrics = mPaint.getFontMetrics();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

mPaint.setColor(Color.WHITE);
mPaint.setXfermode(null);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(10);
int end = (int) (width / 3);

Bitmap output = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);

Canvas bit = new Canvas(output);

RectF rectF = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
//画白色背景
bit.drawRoundRect(rectF, 20, 20, mPaint);

mPaint.setColor(Color.parseColor("#DDDDDD"));
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));

if (stop > 0) {
end = stop;
}
//画选中矩形
bit.drawRect(start, 0, end, getMeasuredHeight(), mPaint);

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.parseColor("#6B6C6E"));
//画周围圆角矩形
bit.drawRoundRect(rectF, 20, 20, mPaint);

mPaint.setStrokeWidth(3);
mPaint.setColor(Color.parseColor("#DDDDDD"));
//画分割线
bit.drawLine((float) (width * 2 / 3), 5, (float) (width * 2 / 3), getMeasuredHeight() - 5, mPaint);
bit.drawLine((float) (width / 3), 5, (float) (width / 3), getMeasuredHeight() - 5, mPaint);

canvas.drawBitmap(output, 0, 0, mPaint);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(2);
//画文字
float y = getMeasuredHeight() / 2 + (Math.abs(mFontMetrics.ascent) - mFontMetrics.descent) / 2;
canvas.drawText("不限", (float) ((width / 3 - mBounds.width()) / 2), y, mPaint);
canvas.drawText("手动", (float) (((width / 3 - mBounds.width()) / 2) + width / 3), y, mPaint);
canvas.drawText("自动", (float) (((width / 3 - mBounds.width()) / 2) + width * 2 / 3), y, mPaint);
}

/**
* 点击事件
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_UP) {
float x = event.getX();

if (0 < x && x < width / 3) {
start = 0;
stop = (int) (width / 3);
invalidate();
position = 0;
} else if (width / 3 < x && x < width * 2 / 3) {
start = (int) (width / 3);
stop = (int) (width * 2 / 3);
invalidate();
position = 1;
} else if (x > width * 2 / 3) {
start = (int) (width * 2 / 3);
stop = getMeasuredWidth();
invalidate();
position = 2;
}
}
return true;
}

public int getPosition() {
return position;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: