android progressbar gif,Android 简单实现游戏的progressBar

仿游戏效果的progressBar

e0716c2a28e7

我的视频 10_clip.gif

 
 

package com.example.cloud.zqchart;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.Rect;

import android.graphics.RectF;

import android.support.annotation.Nullable;

import android.util.AttributeSet;

import android.view.View;

/**

Created by CLOUD on 2017/5/31.

*/

public class ZqBar extends View {

Paint mPaintBg;

Paint mPaintLine;

Paint mPaintText;

Paint mPaintProgress;

private int defaultUnitHeight;

private int defaultWidth;

private int aroundRSamll=15;

private int aroundRBig=30;

private int progress;

private String progressTime;

public ZqBar(Context context) {

super(context);

init();

}

public ZqBar(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

init();

}

public ZqBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

private void init(){

mPaintBg=new Paint();

mPaintBg.setColor(Color.BLACK);

mPaintBg.setStyle(Paint.Style.FILL);

mPaintBg.setAntiAlias(true);

mPaintLine=new Paint();

mPaintLine.setColor(Color.WHITE);

mPaintLine.setStyle(Paint.Style.STROKE);

mPaintLine.setAntiAlias(true);

mPaintLine.setStrokeWidth(3);

mPaintText=new Paint();

mPaintText.setColor(Color.WHITE);

mPaintText.setStyle(Paint.Style.STROKE);

mPaintText.setAntiAlias(true);

mPaintText.setTextSize(16);

mPaintProgress=new Paint();

mPaintProgress.setColor(Color.parseColor("#FFC77002"));

mPaintProgress.setStyle(Paint.Style.FILL);

mPaintProgress.setAntiAlias(true);

defaultWidth =dip2px(getContext(),200);

defaultUnitHeight=dip2px(getContext(),50);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int widthmode = MeasureSpec.getMode(widthMeasureSpec);

int heightmode = MeasureSpec.getMode(heightMeasureSpec);

int width = MeasureSpec.getSize(widthMeasureSpec);

int height = MeasureSpec.getSize(heightMeasureSpec);

if (widthmode==MeasureSpec.AT_MOST){

width=defaultWidth;

}

if (heightmode==MeasureSpec.AT_MOST){

height= defaultUnitHeight;

}

setMeasuredDimension(width,height);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

int measuredWidth = getMeasuredWidth();

int measuredHeight = getMeasuredHeight();

Path path=new Path();

path.moveTo(aroundRSamll,measuredHeight/2+aroundRSamll);

path.quadTo(0,measuredHeight/2,aroundRSamll,measuredHeight/2-aroundRSamll);

path.lineTo(measuredWidth-aroundRBig*2,measuredHeight/2-aroundRSamll);

RectF f = new RectF(measuredWidth-aroundRBig*2,measuredHeight/2-aroundRBig,measuredWidth,measuredHeight/2+aroundRBig);

float startAngle= (float)(180* Math.atan(aroundRSamll*1.0/aroundRBig*1.0)/Math.PI)+180;

float sweepAngle=360*3-2*startAngle;

path.arcTo(f,startAngle,sweepAngle,false);

path.lineTo(aroundRSamll,measuredHeight/2+aroundRSamll);

canvas.drawPath(path,mPaintBg);

canvas.drawPath(path,mPaintLine);

/***

* 进度

*/

canvas.save();

canvas.clipPath(path);

canvas.drawRect(new RectF(0,measuredHeight/2-aroundRBig,progress*measuredWidth/100f,measuredHeight/2+aroundRBig),mPaintProgress);

String sPro = progress + "/100";

Rect bounds=new Rect();

mPaintText.getTextBounds(sPro,0,sPro.length(),bounds);

canvas.drawText(sPro,measuredWidth/2-bounds.width()/2,measuredHeight/2+bounds.height()/2,mPaintText);

Rect boundsTime=new Rect();

mPaintText.getTextBounds(progressTime,0,progressTime.length(),boundsTime);

canvas.drawText(progressTime,measuredWidth-boundsTime.width()/2-aroundRBig,measuredHeight/2+boundsTime.height()/2,mPaintText);

canvas.restore();

}

/**

* 根据手机的分辨率从 dp 的单位 转成为 px(像素)

*/

public int dip2px(Context context, float dpValue) {

final float scale = context.getResources().getDisplayMetrics().density;

return (int) (dpValue * scale + 0.5f);

}

public void setProgress(int progress,String progressTime) {

this.progress = progress;

this.progressTime=progressTime;

invalidate();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值