• 自定义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();
    
        }
    
    }

    效果图:

  • 相关阅读:
    全网通5X_AL10 非定制版 固件汇总
    system.new.dat解压工具sdat2img
    linux 平台中 Android5.0的更新包中system.new.dat文件的解包
    【 henuacm2016级暑期训练-动态规划专题 A 】Cards
    【BZOJ 1486】 [HNOI2009]最小圈
    【BZOJ 1433】[ZJOI2009]假期的宿舍
    【BZOJ 1412】[ZJOI2009]狼和羊的故事
    【BZOJ 1305】[CQOI2009]dance跳舞
    【Codeforces Round #483 (Div. 2) C】Finite or not?
    【Codeforces Round #482 (Div. 2) C】Kuro and Walking Route
  • 原文地址:https://www.cnblogs.com/tinyclear/p/6106511.html
Copyright © 2020-2023  润新知