为了账号安全,请及时绑定邮箱和手机立即绑定

activity_main.xml模拟器不显示?


57936dae0001d76805000343.jpg

57936daf00012ee805000358.jpg

http://img1.sycdn.imooc.com//57936e9b000142c008800495.jpg


package com.view;


import com.example.progressbar.R;


import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.os.Bundle;

import android.support.v4.content.res.TypedArrayUtils;

import android.util.AttributeSet;

import android.util.TypedValue;

import android.widget.ProgressBar;


public class HorPBWithProgress extends ProgressBar {


private static final int DEFAULT_TEXT_SIZE = 10;

private static final int DEFAULT_TEXT_COLOR = 0xFFC00D1;

private static final int DEFAULT_COLOR_UNREACH = 0xffd3d6da;

private static final int DEFAULT_HEIGHT_UNREACH = 2;

private static final int DEFAULT_COLOR_REACH = DEFAULT_TEXT_COLOR;

private static final int DEFAULT_HEIGHT_REACHE = 2;

private static final int DEFAULT_TEXT_OFFSET = 10;


private int mTextSize = sp2px(DEFAULT_TEXT_SIZE);

private int mTextColor = DEFAULT_TEXT_COLOR;

private int mUnReachColor = DEFAULT_COLOR_UNREACH;

private int mUnReachHeight = dp2px(DEFAULT_HEIGHT_UNREACH);

private int mReachColor = dp2px(DEFAULT_COLOR_REACH);

private int mReachHeight = dp2px(DEFAULT_HEIGHT_REACHE);

private int mTextOffset = dp2px(DEFAULT_TEXT_OFFSET);


private Paint mPaint = new Paint();


private int mRealWidth;


public HorPBWithProgress(Context context) {

this(context, null);


}


public HorPBWithProgress(Context context, AttributeSet attrs) {

this(context, attrs, 0);


}


public HorPBWithProgress(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

obtainStyledAttrs(attrs);

}


private void obtainStyledAttrs(AttributeSet attrs) {

// TODO Auto-generated method stub

TypedArray ta = getContext().obtainStyledAttributes(attrs,

R.styleable.HorPBWithProgress);

mTextSize = (int) ta.getDimension(

R.styleable.HorPBWithProgress_progress_text_size, mTextSize);

mTextColor = ta.getColor(

R.styleable.HorPBWithProgress_progress_text_color, mTextColor);

mTextOffset = (int) ta

.getDimension(

R.styleable.HorPBWithProgress_progress_text_offset,

mTextOffset);

mUnReachColor = ta.getColor(

R.styleable.HorPBWithProgress_progress_unreach_color,

mUnReachColor);

mUnReachHeight = (int) ta.getDimension(

R.styleable.HorPBWithProgress_progress_unreach_height,

mUnReachHeight);

mReachHeight = (int) ta.getDimension(

R.styleable.HorPBWithProgress_progress_reach_height,

mReachHeight);

mReachColor = ta

.getColor(R.styleable.HorPBWithProgress_progress_reach_color,

mReachColor);


ta.recycle();

mPaint.setTextSize(mTextSize);

}


@Override

protected synchronized void onMeasure(int widthMeasureSpec,

int heightMeasureSpec) {

// int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int widthVal = MeasureSpec.getSize(widthMeasureSpec);

int height = measureHeight(heightMeasureSpec);

setMeasuredDimension(widthVal, height);

mRealWidth=getMeasuredWidth()-getPaddingLeft()-getPaddingRight();

}


private int measureHeight(int heightMeasureSpec) {

int result=0;

int mode=measureHeight(heightMeasureSpec);

int size=MeasureSpec.getSize(heightMeasureSpec);

if(mode==MeasureSpec.EXACTLY)

{

result=size;

}else

{

int textHeigh=(int) (mPaint.descent()-mPaint.ascent());

result=getPaddingTop()+getPaddingBottom()+Math.max(Math.max(mReachHeight,mUnReachHeight),Math.abs(textHeigh));

if(mode==MeasureSpec.AT_MOST)

{

result=Math.min(result,size);

}

}

return result;

}

@Override

protected synchronized void onDraw(Canvas canvas) {

canvas.save();

canvas.translate(getPaddingLeft(), getHeight()/2);

boolean noNeedUnRech=false;

String text=getProgress()+"%";

int textWidth=(int)mPaint.measureText(text);

float radio=getProgress()*1.0f/getMax();

float progressX=radio*mRealWidth;

if(progressX+textWidth>mRealWidth){

progressX=mRealWidth-textWidth;

noNeedUnRech=true;

}

float endX=progressX-mTextOffset/2;

if(endX>0)

{

mPaint.setColor(mReachColor);

mPaint.setStrokeWidth(mReachHeight);

canvas.drawLine(0, 0, endX, 0, mPaint);

}

mPaint.setColor(mTextColor);

int y=(int) (-(mPaint.descent()+mPaint.ascent())/2);

canvas.drawText(text, progressX, y, mPaint);

if(!noNeedUnRech)

{

float start=progressX+mTextOffset/2+textWidth;

mPaint.setColor(mUnReachColor);

mPaint.setStrokeWidth(mUnReachHeight);

canvas.drawLine(start, 0, mRealWidth, 0, mPaint);

}

canvas.restore();

}

private int dp2px(int dpVal) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,

dpVal, getResources().getDisplayMetrics());

}


private int sp2px(int spVal) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,

spVal, getResources().getDisplayMetrics());


}

}


正在回答

1 回答

你没有导入包 HorPBWithProgress,试试点击进度条框,会有导入提示,选择第一个就好了。我androidStdio是这样的

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android-打造炫酷进度条
  • 参与学习       30849    人
  • 解答问题       59    个

Android开发中各种各样的自定义进度条介绍,让你的APP更炫酷

进入课程

activity_main.xml模拟器不显示?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信