Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android自定義View之三種流行進度條的寫法

Android自定義View之三種流行進度條的寫法

編輯:關於Android編程

概述:

利用自定義View的onDraw()方法,可以繪制很多種圖形,進度框只是其中之一。

Demo

這是一個模擬下載的demo。

自中央逐漸充滿型圓形進度框

這裡寫圖片描述

demo1

public class FirstProgressView extends View{

    private int width;
    private int height;

    private int progress;
    private int maxProgress = 100;
    private Paint mPaintBackGround;
    private Paint mPaintCurrent;
    private Paint mPaintText;

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public int getMaxProgress() {
        return maxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.maxProgress = maxProgress;
    }

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

    public FirstProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaintBackGround = new Paint();//新定義一個畫筆,用來畫背景
        mPaintBackGround.setColor(Color.GRAY);//設置畫筆顏色
        mPaintBackGround.setAntiAlias(true);//設置為true,代表抗鋸齒

        mPaintCurrent = new Paint();//用於畫進度圖
        mPaintCurrent.setColor(Color.GREEN);
        mPaintCurrent.setAntiAlias(true);

        mPaintText = new Paint();//用來畫文本的畫筆
        mPaintText.setColor(Color.BLACK);
        mPaintText.setAntiAlias(true);
        mPaintText.setTextAlign(Paint.Align.CENTER);//設置文本排布方式:正中央
        mPaintText.setTextSize(50);//設置文本大小,這裡為50xp
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //左起:圓心x坐標,圓心y坐標,半徑,Paint對象(畫筆)
        canvas.drawCircle(width / 2, height / 2, 400, mPaintBackGround);
        canvas.drawCircle(width/2,height/2,progress*400f/maxProgress,mPaintCurrent);
        //左起:文本內容,起始位置x坐標,起始位置y坐標,畫筆
        canvas.drawText(progress*100f/maxProgress+%,width / 2, height / 2,mPaintText);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //該View布局寬
        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        //該View布局高
        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        //設定本View的大小的方法
        setMeasuredDimension(width, height);
    }
}

水箱型注水式進度框

這裡寫圖片描述

demo2

public class SecondProgressView extends View {

    private int width;
    private int height;

    private int progress;
    private int maxProgress = 100;
    private Paint mPaintBackGround;
    private Paint mPaintCurrent;
    private Paint mPaintText;

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public int getMaxProgress() {
        return maxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.maxProgress = maxProgress;
    }

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

    public SecondProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaintBackGround = new Paint();
        mPaintBackGround.setColor(Color.GRAY);
        mPaintBackGround.setStyle(Paint.Style.STROKE);
        mPaintBackGround.setStrokeWidth(20);
        mPaintBackGround.setAntiAlias(true);

        mPaintCurrent = new Paint();
        mPaintCurrent.setColor(Color.BLUE);
        mPaintCurrent.setAntiAlias(true);

        mPaintText = new Paint();
        mPaintText.setColor(Color.BLACK);
        mPaintText.setAntiAlias(true);
        mPaintText.setTextAlign(Paint.Align.CENTER);
        mPaintText.setTextSize(50);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawRect(width / 2 - 200, height / 2 - 200, width / 2 + 200, height / 2 + 200, mPaintBackGround);
        canvas.drawRect(width/2-190,height/2-190+(380-progress*380f/maxProgress),width/2+190,height/2+190,mPaintCurrent);
        canvas.drawText(progress*100f/maxProgress+%,width / 2, height / 2,mPaintText);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);
    }
}

圓圈式弧形進度框:

這裡寫圖片描述

demo3:<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjwvcD4NCjxwcmUgY2xhc3M9"brush:java;"> public class ThirdProgressView extends View { private int width; private int height; private int progress; private int maxProgress = 100; private Paint mPaintBackGround; private Paint mPaintCurrent; private Paint mPaintText; public void setWidth(int width) { this.width = width; } public void setHeight(int height) { this.height = height; } public int getProgress() { return progress; } public void setProgress(int progress) { this.progress = progress; invalidate(); } public int getMaxProgress() { return maxProgress; } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } public ThirdProgressView(Context context) { super(context); } public ThirdProgressView(Context context, AttributeSet attrs) { super(context, attrs); mPaintBackGround = new Paint(); mPaintBackGround.setColor(Color.GRAY); mPaintBackGround.setStrokeWidth(60); mPaintBackGround.setStyle(Paint.Style.STROKE); mPaintBackGround.setAntiAlias(true); mPaintCurrent = new Paint(); mPaintCurrent.setColor(Color.GREEN); mPaintCurrent.setStrokeWidth(60); mPaintCurrent.setStyle(Paint.Style.STROKE); mPaintCurrent.setAntiAlias(true); mPaintText = new Paint(); mPaintText.setColor(Color.BLACK); mPaintText.setAntiAlias(true); mPaintText.setTextAlign(Paint.Align.CENTER); mPaintText.setTextSize(50); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(width / 2, height / 2, 400, mPaintBackGround); RectF oval = new RectF(width/2-400,height/2-400,width/2+400,height/2+400); //左起分別是RectF對象,起始角度,終止角度,是否顯示扇邊(如果為true畫出的是一個扇形),Paint對象 canvas.drawArc(oval,0,progress*360f/maxProgress,false,mPaintCurrent); canvas.drawText(progress*100f/maxProgress+%,width / 2, height / 2,mPaintText); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec); setMeasuredDimension(width, height); } }

需要在布局文件中加載自定義View,這裡隨便加載一個作示范(自定義View都這麼加載):



    

主活動的中自定義View也需要聲明和findViewById,這裡寫的是一個模擬下載的小demo,加載的是第三種自定義View,其他的自定義View加載方式完全一樣。

public class MainActivity extends Activity {
    private int progress;
    private Button mButtonStart;
    private ThirdProgressView mProgressView;

    private static final int DOWNLOAD_UPDATE = 0x99;
    //模擬下載
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //處理msg
            switch (msg.what) {
                case DOWNLOAD_UPDATE:
                    progress++;
                    if (progress <= 100) {
                        mProgressView.setProgress(progress);
                        sendEmptyMessageDelayed(DOWNLOAD_UPDATE, 100);//每隔100毫秒發送一次handler
                    }
                    break;
            }
        }
    };

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

        mButtonStart = (Button) findViewById(R.id.button_start);
        mProgressView = (ThirdProgressView) findViewById(R.id.progress_view_first);

        mButtonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //向handler發送一個延時空消息,1000毫秒後發送
                mHandler.sendEmptyMessageDelayed(DOWNLOAD_UPDATE, 1000);
            }
        });

    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved