android开源仪表盘,Android实现仪表盘效果

本文实例为大家分享了Android实现仪表盘效果的具体代码,供大家参考,具体内容如下

仪表盘效果,圆弧可变色,效果图如下:

5f95423ddb5b33a0787fa44e3b7a854d.png

通过自定义view实现,代码如下:

public class DashboardView extends View {

private int mRadius; // 画布边缘半径(去除padding后的半径)

private int mStartAngle = 150; // 起始角度

private int mSweepAngle = 240; // 绘制角度

private int mMin = 1; // 最小值

private int mMax = 8; // 最大值

private int mSection = 8; // 值域(mMax-mMin)等分份数

private int mPortion = 3; // 一个mSection等分份数

private String mHeaderText = ""; // 表头

private int mCreditValue = (int) 8.12; // 信用分

private int mSolidCreditValue = mCreditValue; // 信用分(设定后不变)

private int mSparkleWidth; // 亮点宽度

private int mProgressWidth; // 进度圆弧宽度

private float mLength1; // 刻度顶部相对边缘的长度

private int mCalibrationWidth; // 刻度圆弧宽度

private float mLength2; // 刻度读数顶部相对边缘的长度

private int mPadding;

private float mCenterX, mCenterY; // 圆心坐标

private Paint mPaint;

private RectF mRectFProgressArc;

private RectF mRectFCalibrationFArc;

private RectF mRectFTextArc;

private Path mPath;

private Rect mRectText;

private String[] mTexts;

private int mBackgroundColor;

private int[] mBgColors;

private boolean isAnimFinish = true;

private float mAngleWhenAnim;

public DashboardView(Context context) {

this(context, null);

}

public DashboardView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public DashboardView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

private void init() {

mSparkleWidth = dp2px(10);

mProgressWidth = dp2px(3);

mCalibrationWidth = dp2px(10);

mPaint = new Paint();

mPaint.setAntiAlias(true);

mPaint.setStrokeCap(Paint.Cap.ROUND);

mRectFProgressArc = new RectF();

mRectFCalibrationFArc = new RectF();

mRectFTextArc = new RectF();

mPath = new Path();

mRectText = new Rect();

mTexts = new String[]{"", "Ⅳ", "", "Ⅲ", "", "Ⅱ", "", "Ⅰ", ""};

mBgColors = new int[]{ContextCompat.getColor(getContext(), R.color.color_red),

ContextCompat.getColor(getContext(), R.color.color_orange),

ContextCompat.getColor(getContext(), R.color.color_yellow),

ContextCompat.getColor(getContext(), R.color.color_green),

ContextCompat.getColor(getContext(), R.color.color_blue)};

mBackgroundColor = mBgColors[0];

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

mPadding = Math.max(

Math.max(getPaddingLeft(), getPaddingTop()),

Math.max(getPaddingRight(), getPaddingBottom())

);

setPadding(mPadding, mPadding, mPadding, mPadding);

mLength1 = mPadding + mSparkleWidth / 2f + dp2px(8);

mLength2 = mLength1 + mCalibrationWidth + dp2px(1) + dp2px(5);

int width = resolveSize(dp2px(220), widthMeasureSpec);

mRadius = (width - mPadding * 2) / 2;

setMeasuredDimension(width, width - dp2px(30));

mCenterX = mCenterY = getMeasuredWidth() / 2f;

mRectFProgressArc.set(

mPadding + mSparkleWidth / 2f,

mPadding + mSparkleWidth / 2f,

getMeasuredWidth() - mPadding - mSparkleWidth / 2f,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值