自定义Button成进度条

Posted 放纵的卡尔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义Button成进度条相关的知识,希望对你有一定的参考价值。

ProgressButton源码:

package com.example.progressbutton;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Button;

/**
 * @author  Administrator
 * @time     2015-7-19 下午4:41:06
 * @des    TODO
 *
 * @version $Rev$
 * @updateAuthor $Author$
 * @updateDate $Date$
 * @updateDes TODO
 */
public class ProgressButton extends Button {
    private boolean        mProgressEnable;
    private long        mMax    = 100;
    private long        mProgress;
    private Drawable    mProgressDrawable;

    /**设置是否允许进度*/
    public void setProgressEnable(boolean progressEnable) {
        mProgressEnable = progressEnable;
    }

    /**设置进度的最大值*/
    public void setMax(long max) {
        mMax = max;
    }

    /**设置当前的进度,并且进行重绘操作*/
    public void setProgress(long progress) {
        mProgress = progress;
        invalidate();
    }

    /**设置progressButton的进度图片*/
    public void setProgressDrawable(Drawable progressDrawable) {
        mProgressDrawable = progressDrawable;
    }

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

    public ProgressButton(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mProgressEnable) {
            Drawable drawable = new ColorDrawable(Color.BLUE);
            int left = 0;
            int top = 0;
            int right = (int) (mProgress * 1.0f / mMax * getMeasuredWidth() + .5f);
            int bottom = getBottom();
            drawable.setBounds(left, top, right, bottom);// 必须的.告知绘制的范围
            drawable.draw(canvas);
        }

        super.onDraw(canvas);// 绘制文本,还会绘制背景
    }
}

控制显示Demo:

package com.example.progressbutton;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(final View view) {

        new AsyncTask<Void, Integer, Void>() {
            int progress=0;
            private ProgressButton mButton;
            @Override
            protected void onPreExecute() {
                mButton = (ProgressButton) view;
                mButton.setProgressEnable(true);
            }

            @Override
            protected Void doInBackground(Void... params) {
                for (int i = 0; i <= 100; i++) {
                    SystemClock.sleep(100);
                    publishProgress(i);
                }
                return null;
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                mButton.setProgress(values[0]);
                mButton.setText(values[0]+"%");
            }

            protected void onPostExecute(Void result) {

            };

        }.execute();

    }

}

效果图:

 

以上是关于自定义Button成进度条的主要内容,如果未能解决你的问题,请参考以下文章

自定义进度条Android,kotlin

CodePush自定义更新弹框及下载进度条

如何制作WinForm中自定义进度条

Qt编写自定义控件14-环形进度条

下载时更新ListView中的进度条

C# WinForm自定义进度条