Android 简单实现游戏的progressBar

仿游戏效果的progressBar


Android 简单实现游戏的progressBar_第1张图片
我的视频 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();
}

}

你可能感兴趣的:(Android 简单实现游戏的progressBar)