Android – 自定义Loading圆点

网络等待Loading图

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"
       />

具体效果和实现就是以上这些内容了。有什么问题可以评论。

快乐生活!快乐工作!快乐编程!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,923评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,372评论 4 61
  • 1938年10月日军占领武汉,11月攻陷岳阳。长沙距岳阳1 30公里,原本是抗战大后方的长沙被推到了抗战第一线。日...
    梁宵阅读 3,808评论 0 1
  • 本身是个骄傲的人,眼看着身边的人一个个比自己好,而老公事业却从婚后到现在一直不顺。先是工作几年的公司倒闭,再是换了...
    胖兔儿阅读 1,400评论 0 1
  • 这是九瓣 365天写作计划第35天的写作内容。 今日无雨。 灰白的天,却有点点浅浅的阳光点缀着目所能及的这片地。包...
    九瓣阅读 2,929评论 1 1

友情链接更多精彩内容