Android自定義view之實(shí)現(xiàn)進(jìn)度球

前言

最近一直在研究自定義view,正好項(xiàng)目中有一個根據(jù)下載進(jìn)度來實(shí)現(xiàn)球體進(jìn)度的需求,所以自己寫了個進(jìn)度球,代碼非常簡單。先看下效果:


在這里插入圖片描述

效果還是非常不錯的。

準(zhǔn)備知識

要實(shí)現(xiàn)上面的效果我們只要掌握兩個知識點(diǎn)就好了,一個是Handler機(jī)制,用于發(fā)消息刷新我們的進(jìn)度球,一個是clipDrawable。網(wǎng)上關(guān)于Handler的教程很多,這里重點(diǎn)介紹一下clipDrawable,進(jìn)度球的實(shí)現(xiàn)全靠clipDrawable。
clipDrawable
如下圖所示:ClipDrawable和InsertDrawable一樣繼承DrawableWrapper,DrawableWrapper繼承Drawable。

在這里插入圖片描述

ClipDrawable是可以進(jìn)行裁剪操作的drawable,提供了函數(shù)setLevel(@IntRange(from=0,to=10000) int level)來設(shè)置裁剪的大小。level越大圖片越大。level=0時圖片完全不顯示,level=10000時圖片完全顯示。

        ClipDrawable clipDrawable =  (ClipDrawable) getContext().getResources().getDrawable(R.drawable.bottom_top_clip_gradient_color);//獲取圖片
        clipDrawable.setLevel(100);//進(jìn)行裁剪

一般還要設(shè)置裁剪的方向,垂直裁剪還是水平裁剪,我們這個進(jìn)度球用的是垂直從下向上裁剪。
思路:知道了ClipDrawable的用法,進(jìn)度球就好實(shí)現(xiàn)了。只需要一個球形的圖片,從下往上裁剪,通過設(shè)置setLevel從0到10000,就可以實(shí)現(xiàn)進(jìn)度球從0到進(jìn)度100的效果了。

實(shí)現(xiàn)

1、定義BallProgress,BallProgress繼承View,重寫onDraw()方法,用于實(shí)現(xiàn)進(jìn)度球的view。

/**
 * Created time 15:02.
 *
 * @author huhanjun
 * @since 2018/12/26
 */
public class BallProgress extends View {
    private float mProgress = 0.0f;   //取值位 0 - 1.0

    private boolean selected = true;

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

    public BallProgress(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BallProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mProgressPaint = new Paint();//初始化,定義畫筆。
        mProgressPaint.setAntiAlias(true);//設(shè)置抗鋸齒
    }

    public float getProgress() {
        return mProgress;
    }

    public void setProgress(float progress) {//設(shè)置進(jìn)度,通過進(jìn)度的大小實(shí)現(xiàn)裁剪的大小
        mProgress = progress;
        invalidate();
    }

    private Paint mProgressPaint;


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap dst = getRectangleBitmap();//獲取bitmap
        setLayerType(LAYER_TYPE_HARDWARE, null);  //開啟硬件離屏緩存
        canvas.drawBitmap(dst, 0, 0, mProgressPaint);
    }
    private Bitmap getRectangleBitmap() {
        int width = getWidth();
        int height = getHeight();
        
        Bitmap dstBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        
        ClipDrawable clipDrawable = null;
            clipDrawable = (ClipDrawable) getContext().getResources().getDrawable(R.drawable.bottom_top_clip_single_color);//獲取球形的背景圖片,用于裁剪,就是上面看到的進(jìn)度球中的圖片
            
        clipDrawable.setBounds(new Rect(0, 0, width, height));//設(shè)置邊界
        
        clipDrawable.setLevel((int) (10000 * mProgress));//設(shè)置進(jìn)度,
        
        Canvas canvas = new Canvas(dstBitmap);//設(shè)置畫布
        
        clipDrawable.draw(canvas);//繪制
        
        return dstBitmap;//將bitmap返回
    }

}

有了自定義的BallProgress,就可以在布局中使用了,定義的xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.admin.floatprogressbar.BallProgress
        android:id="@+id/progress"
        android:layout_width="@dimen/progress_size"
        android:layout_height="@dimen/progress_size"
        android:layout_centerInParent="true" />
    <ImageView
        android:layout_width="@dimen/progress_size"
        android:layout_height="@dimen/progress_size"
        android:layout_centerInParent="true"
        android:background="@drawable/main_tab_un_select_bg" />

</RelativeLayout>

上面布局中的ImageView是懸浮球的邊界。在MainActivity中來定時的改變進(jìn)度球的大小。代碼如下:

public class MainActivity extends AppCompatActivity {
    private final int PROGRESS_MESSAGE = 0;
    private float progress = 0.0f;
    private BallProgress mBallProgress;
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case PROGRESS_MESSAGE:
                    progress = (progress > 0.9f) ? 0.9f : progress;
                    mBallProgress.setProgress(progress);//接收消息,改變進(jìn)度球的進(jìn)度
                    break;
            }
            return true;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initAction();
    }

    private void initView() {
        mBallProgress = findViewById(R.id.progress);
    }
    //發(fā)消息
    private void initAction() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    progress += 0.02f;
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mHandler.sendEmptyMessage(PROGRESS_MESSAGE);//每隔100毫秒發(fā)送一次消息,對進(jìn)度球進(jìn)度進(jìn)行更新。
                }
            }
        }).start();
    }

}

上面代碼在inAction()中開一個線程,每隔100毫秒發(fā)送消息,在handler中處理更新。實(shí)際應(yīng)用中,可以是跟業(yè)務(wù)相關(guān)的具體進(jìn)度。

總結(jié)

自定義進(jìn)度球,用的是繼承view,并且通過自定義畫筆,重寫onDraw()方法來實(shí)現(xiàn),一般自定義view都會重寫onDraw()方法,一般進(jìn)度條都是ClipDrawable來實(shí)現(xiàn)的。
源碼地址:進(jìn)度球代碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容