• 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>

  • 相关阅读:
    转 mysql 数据结构详解
    转单元测试之道C#版
    转 告诉你如何用C#写出iOS与Android应用
    转 MySQL索引背后的数据结构及算法原理
    转单元测试基础知识
    转C#冒泡排序
    如何让web页面鼠标右键单击之后不出现菜单选项
    开博文
    jquery ui 1.7 ui.tabs 动态添加与关闭(按钮关闭+双击关闭)
    jquery ui tabs详解(中文)
  • 原文地址:https://www.cnblogs.com/zgz345/p/2490572.html
Copyright © 2020-2023  润新知