900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > android 自定义 对号 【Android】自定义progressBar和动画显示对号

android 自定义 对号 【Android】自定义progressBar和动画显示对号

时间:2020-04-23 08:12:10

相关推荐

android 自定义 对号 【Android】自定义progressBar和动画显示对号

编写代码时参考了大神的文章

-----------------------------------------------------------------------------------------------

1.说明: 界面上显示正在加载的提示动画 ,加载完成之后显示对号。

2.显示效果:

3. 代码

3.1 为自定义控件添加属性

3.2 获取自定义的属性

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

super(context, attrs, defStyleAttr);

TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar,defStyleAttr,0);

int count = array.getIndexCount();

for (int i = 0;i < count;i++){

int arr = array.getIndex(i);

switch (arr){

case R.styleable.CustomProgressBar_bgColor:

circleColor = array.getColor(arr, Color.BLUE);

break;

case R.styleable.CustomProgressBar_runSpeed:

circleSpeed = array.getInt(arr,20);

break;

case R.styleable.CustomProgressBar_paintWidth:

circleWidth = array.getDimensionPixelSize(arr, (int) TypedValue.applyDimension(PLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));

break;

}

}

array.recycle();

}

3.3 画圆弧

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

int center = getWidth()/2;//圆心

int radius = center - circleWidth/2;//半径

RectF oval = new RectF(circleWidth/2,circleWidth/2,center+radius,center+radius);

canvas.drawArc(oval,startAngle,progress,false,mPaint);

}

3.4 动态画圆弧

分析: 想要圆弧变化怎么办?

开始画圆弧的位置和圆弧的长度变化。

new Thread(new Runnable() {

@Override

public void run() {

while (true){

progress+=1;

moveAngle+=0.002;//自定义圆弧其实位置变化的大小

startAngle +=moveAngle;

if (progress >= (340 +moveAngle)){//如果340-->360 那么圆环闭合才会从新画圆

progress = 0;

startAngle = START_ANGLE;

moveAngle = 0;

}

postInvalidate();

try {

Thread.sleep(circleSpeed);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}).start();

4.源码下载

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。