Android – 自定義Loading圓點

weixin_34007886發表於2017-10-25

網路等待Loading圖

2726727-e2bc60542ce884b8.gif
Loading.gif

剛開始做這種效果是用xml來畫圓形實心點的。
白色圓點

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    android:shape="oval" >
    <corners  android:radius="8dp"/>
    <solid  android:color="#fffdf8"/>
</shape>

灰色圓點

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    android:shape="oval" >
    <corners  android:radius="8dp"/>
    <solid  android:color="#7ffffdf8"/>
</shape>

Loading佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="60dp"
    android:layout_height="50dp"
    android:background="@drawable/toast_error_network_bg"
    android:orientation="horizontal"
    android:gravity="center">

    <ImageView
        android:id="@+id/iv_dot1"
        android:layout_width="5dp"
        android:layout_height="5dp"
        android:background="@drawable/dot_unfocus"/>

    <ImageView
        android:id="@+id/iv_dot2"
        android:layout_width="5dp"
        android:layout_height="5dp"
        android:layout_marginLeft="5dp"
        android:background="@drawable/dot_unfocus"/>
    <ImageView
        android:id="@+id/iv_dot3"
        android:layout_width="5dp"
        android:layout_height="5dp"
        android:layout_marginLeft="5dp"
        android:background="@drawable/dot_unfocus"/>

</LinearLayout>

使用Timer來修改background

Timer  mTimer = new Timer();
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if(mPosition ==1) {
                    mIvDot1.setBackgroundResource(R.drawable.dot_focus);
                    mIvDot2.setBackgroundResource(R.drawable.dot_unfocus);
                    mIvDot3.setBackgroundResource(R.drawable.dot_unfocus);
                    mPosition = 2;
                } else if(mPosition == 2) {
                    mIvDot1.setBackgroundResource(R.drawable.dot_unfocus);
                    mIvDot2.setBackgroundResource(R.drawable.dot_focus);
                    mIvDot3.setBackgroundResource(R.drawable.dot_unfocus);
                    mPosition = 3;
                } else if(mPosition == 3) {
                    mIvDot1.setBackgroundResource(R.drawable.dot_unfocus);
                    mIvDot2.setBackgroundResource(R.drawable.dot_unfocus);
                    mIvDot3.setBackgroundResource(R.drawable.dot_focus);
                    mPosition = 1;
                }
            }
        };

        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(0);
            }
        }, 0, 400);

這樣也是可以達到上面那種效果的,但是總感覺這麼寫不太符合程式設計師的風格。。。所以就有了下面的這種寫法。

public class LoadingPointView extends View {
    public static final int MESSAGE_ID = 0;
    //白色圓點
    private Paint mWhitePaint;
    //綠色圓點
    private Paint mGreenPaint;
    //半徑
    private int mRadius;
    //下一個被選中的圓點的index
    private int mIndex;
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ++mIndex;
            if (mIndex == 5) {
                mIndex = 0;
            }
            postInvalidate();
        }
    };

    public LoadingPointView(Context context) {
        this(context, null);
    }

    public LoadingPointView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    private void initParmas(Context context) {
        mWhitePaint = new Paint();
        mWhitePaint.setAntiAlias(true);
        mWhitePaint.setStyle(Paint.Style.FILL);
        mWhitePaint.setColor(ContextCompat.getColor(context, R.color.white));

        mGreenPaint = new Paint();
        mGreenPaint.setAntiAlias(true);
        mGreenPaint.setStyle(Paint.Style.FILL);
        mGreenPaint.setColor(ContextCompat.getColor(context, R.color.c_3ec88e));

        mPaintWidth = Px2DpUtil.dp2px(context, 2);
        mCircleX = Px2DpUtil.dp2px(context, 40);
        mCircleY = Px2DpUtil.dp2px(context, 40);
        mRadius = Px2DpUtil.dp2px(context, 5);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < 5; i++) {
            //修改圓心x軸座標,來畫出多個圓點
            canvas.drawCircle(getHeight() / 2 + mRadius * i * 2 + 5 * i, getHeight() / 2, mRadius, mWhitePaint);
        }
        //動態修改綠色圓點的位置
        canvas.drawCircle(getHeight() / 2 + mRadius * mIndex * 2 + 5 * mIndex, getHeight() / 2, mRadius, mGreenPaint);
        //傳送訊息不斷繪製,以達到無限迴圈的效果
        mHandler.sendEmptyMessageDelayed(MESSAGE_ID, 200);
    }
    //停止動畫
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeMessages(MESSAGE_ID);
        mHandler = null;
    }
}

使用方法

    <cn.custom.widget.widget.LoadingPointView
        android:id="@+id/id_loading_point_view"
        android:layout_width="60dp"
        android:layout_height="10dp"   
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
       />

具體效果和實現就是以上這些內容了。有什麼問題可以評論。

快樂生活!快樂工作!快樂程式設計!

相關文章