Android自定义进度条(用画笔手动画出来)

package com.example.myprogressbar;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;


import java.util.ArrayList;


public class SQProgressBar extends View
{
private Paint backgroundPaint;
private int mRectX = 0;
private int currentProgress = 0;
private int maxCount = 100;
private float density = 0;


private static final int ballColor1_S = Color.parseColor("#27e9da");
private static final int ballColor2_S = Color.parseColor("#2acfe1");
private static final int ballColor3_S = Color.parseColor("#2faceb");
private static final int ballColor4_S = Color.parseColor("#3389f5");
private static final int ballColor5_S = Color.parseColor("#3670fd");


private static final int ballColor1_Q = Color.parseColor("#eaf382");
private static final int ballColor2_Q = Color.parseColor("#eedc82");
private static final int ballColor3_Q = Color.parseColor("#f4bc82");
private static final int ballColor4_Q = Color.parseColor("#fa9b83");
private static final int ballColor5_Q = Color.parseColor("#ff8483");


private static final int DEFAULT_START_COLOR_S = Color.parseColor("#27e0d1");
private static final int DEFAULT_END_COLOR_S = Color.parseColor("#376df8");


private static final int DEFAULT_START_COLOR_Q = Color.parseColor("#eaf382");
private static final int DEFAULT_END_COLOR_Q = Color.parseColor("#ff8483");


private static final int ballColors_S[] = { ballColor1_S, ballColor2_S, ballColor3_S, ballColor4_S, ballColor5_S };
private static final int ballColors_Q[] = { ballColor1_Q, ballColor2_Q, ballColor3_Q, ballColor4_Q, ballColor5_Q };
private int ballColors[] = ballColors_S;


private static final int[] SECTION_COLORS_S = { DEFAULT_START_COLOR_S, DEFAULT_END_COLOR_S };
private static final int[] SECTION_COLORS_Q = { DEFAULT_START_COLOR_Q, DEFAULT_END_COLOR_Q };
private int[] SECTION_COLORS = SECTION_COLORS_S;


private int Rect_X = 22;
private int Rect_Y = 10;
private int Radius = 30;
private int RectY2 = 26;

private int CircleY = 18;
private int textY = 4;
private int progress_Duration = 330;

private int textX = 40;


public static final int type_S = 0;
public static final int type_Q = 1;


private int mWidth = 0;
private int mHeight = 0;
private int mScreenWidth = 0;
private int mScreenHeight = 0;
private Context mContext = null;
public void setProgressType(int type)
{
if (type == type_Q)
{
ballColors = ballColors_Q;
SECTION_COLORS = SECTION_COLORS_Q;
}
else
{
ballColors = ballColors_S;
SECTION_COLORS = SECTION_COLORS_S;
}
}


public SQProgressBar(Context context)
{
super(context);
init(context);


}


public SQProgressBar(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);

}


public SQProgressBar(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init(context);
}


private void init(Context context)
{
mContext = context;
}

@Override
protected void onDraw(Canvas canvas)
{
// TODO Auto-generated method stub
super.onDraw(canvas);

backgroundPaint = new Paint();
backgroundPaint.setAntiAlias(true);
backgroundPaint.setColor(Color.parseColor("#393e46"));
Rect r1 = new Rect(Rect_X, Rect_Y, Radius + progress_Duration * 4, RectY2);
canvas.drawRect(r1, backgroundPaint);
mRectX = 0;
float currentPercent = ((float) currentProgress) / maxCount;
//Log.d("ccc", "currentPercent " + currentPercent);
Paint paintText = new Paint();
paintText.setAntiAlias(true);
paintText.setTextSize(30);
paintText.setColor(Color.WHITE);
canvas.drawText(currentProgress + " %", Radius + progress_Duration * 4 + textX , RectY2 + textY, paintText);
int curX = (int) ((mRectX + Radius*4 + progress_Duration * 4) * currentPercent);
//Log.d("ccc", "currentPercent curX  " + curX);


Paint pColor = new Paint();
LinearGradient shader2 = new LinearGradient(3, 3, mRectX + Radius + progress_Duration * 4, RectY2, SECTION_COLORS, null, Shader.TileMode.REPEAT);
pColor.setShader(shader2);


Rect r2 = new Rect(Rect_X, Rect_Y, curX, RectY2);
canvas.drawRect(r2, pColor);


// LinearGradient shader = new LinearGradient(3, 3, 20,20,
// SECTION_COLORS,null, Shader.TileMode.REPEAT);


ArrayList<Paint> paintList = new ArrayList<Paint>();
for (int i = 0; i < 5; i++)
{
Paint p = new Paint();
p.setAntiAlias(true);
paintList.add(p);
}


for (int i = 0; i < 5; i++)
{
mRectX = Radius + progress_Duration * i;
if (curX > mRectX)
{
paintList.get(i).setColor(ballColors[i]);
}
else
{
paintList.get(i).setColor(Color.parseColor("#393e46"));
}


canvas.drawCircle(mRectX, CircleY, Radius, paintList.get(i));
}


}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int width = this.getMeasuredSize(widthMeasureSpec, true);
int height = this.getMeasuredSize(heightMeasureSpec, false);
mWidth = width;
mHeight = height;
//Log.d("sqpro", "width : "+width+"   height : "+height);
//获取屏幕信息
DisplayMetrics dm = getResources().getDisplayMetrics();
density = dm.density;
mScreenWidth = dm.widthPixels;
mScreenHeight = dm.heightPixels;
changeLength();
//float percentX =  
//Log.d("sqpro", "widthG : "+mScreenWidth+"   heightG : "+mScreenHeight);
setMeasuredDimension(width, height);
};

private void changeLength()
{
float pW = ((float) mWidth)/ mScreenWidth ;
float pH = ((float) mHeight)/mScreenHeight;

textY = 4;
progress_Duration = 330;
textX = 40;
Rect_X = 22;
Rect_Y = 24;
Radius = 30;
RectY2 = 40;

CircleY = 30;



Log.d("sqpro222", " pw : "+pW+"  pH : "+pH +" Rect_X: "+Rect_X+" Rect_Y "+Rect_Y+" Radius "+Radius+" RectY2 "+RectY2+" CircleY  "+CircleY+"  textY "+textY+" progress_Duration  "+progress_Duration+"  textX "+textX);
Rect_X =  (int) (Rect_X*pW);
Rect_Y = (int) (Rect_Y*pW);
Radius = (int) (Radius*pW);
RectY2 = (int) (RectY2*pW);
CircleY = (int) (CircleY*pW);
//textY = (int) (textY*pW);
progress_Duration = (int) (progress_Duration*pW);
textX = (int) (textX*pW);
Log.d("sqpro", " pw : "+pW+"  pH : "+pH +" Rect_X: "+Rect_X+" Rect_Y "+Rect_Y+" Radius "+Radius+" RectY2 "+RectY2+" CircleY  "+CircleY+"  textY "+textY+" progress_Duration  "+progress_Duration+"  textX "+textX);

}


/**
* 计算控件的实际大小

* @param length
*            onMeasure方法的参数,widthMeasureSpec或者heightMeasureSpec
* @param isWidth
*            是宽度还是高度
* @return int 计算后的实际大小
*/
private int getMeasuredSize(int length, boolean isWidth)
{
// 模式
int specMode = MeasureSpec.getMode(length);
// 尺寸
int specSize = MeasureSpec.getSize(length);
// 计算所得的实际尺寸,要被返回
int retSize = 0;
// 得到两侧的padding(留边)
int padding = (isWidth ? getPaddingLeft() + getPaddingRight() : getPaddingTop() + getPaddingBottom());


// 对不同的指定模式进行判断
if (specMode == MeasureSpec.EXACTLY)
{ // 显式指定大小,如40dp或fill_parent
retSize = specSize;
}
else
{ // 如使用wrap_content
retSize = (isWidth ? this.getWidth() + padding : this.getHeight() + padding);
if (specMode == MeasureSpec.UNSPECIFIED)
{
retSize = Math.min(retSize, specSize);
}
}


return retSize;
}


public int getCurrentProgress()
{
return currentProgress;
}


public void setCurrentProgress(int currentProgress)
{
this.currentProgress = currentProgress;
invalidate();
}


public int getMaxCount()
{
return maxCount;
}


public void setMaxCount(int maxCount)
{
this.maxCount = maxCount;
}


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

景兄弟1366

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值