android中自定義progress

package com.ichaoying.DBHelper;

import java.text.DecimalFormat;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ProgressBar;

public class MyProgress extends ProgressBar {
String text;
Paint mPaint;
public MyProgress(Context context) {
super(context);
initText();
}

public MyProgress(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initText();
}

public MyProgress(Context context, AttributeSet attrs) {
super(context, attrs);
initText();
}

public synchronized void setProgress(int progress) {
super.setProgress(progress);
setText(progress);
}

protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
// this.setText();
Rect rect = new Rect();
this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
int x = (getWidth() / 2) - rect.centerX();
int y = (getHeight() / 2) - rect.centerY();
canvas.drawText(this.text, x, y, this.mPaint);
}

// 初始化,画笔
private void initText() {
this.mPaint = new Paint();
this.mPaint.setColor(Color.WHITE);
}

private void setText() {
setText(this.getProgress());
}

public String FormetFileSize(long fileS) {//转换文件大小
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "G";
}
return fileSizeString;
}

// 设置文字内容
private void setText(int progress) {
float num = (float)progress/(float)getMax();
int result = (int)(num*100);
this.text = result + "%"+" 文件总大小:"+FormetFileSize(getMax());
}

}

 

 

使用時在xml中配置全路徑

<com.icss.MyProgress android:id="@+id/pgsBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:visibility="visible" >
</com.icss.MyProgress>

你可能感兴趣的:(progress)