网络等待Loading图

Loading.gif
刚开始做这种效果是用xml来画圆形实心点的。
白色圆点

<?xml version="1.0" encoding="utf-8"?>    android:shape="oval" >        

灰色圆点

<?xml version="1.0" encoding="utf-8"?>    android:shape="oval" >        

Loading布局文件

<?xml version="1.0" encoding="utf-8"?>            

使用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;    }}

使用方法

    

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

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

更多相关文章

  1. Android(安卓)单击图片切换效果
  2. Android(安卓)logcat内核信息
  3. Android帧布局实现霓虹灯的效果
  4. android实现页面下方的Tab效果 .
  5. android动画效果2
  6. Android中WARNING: Application does not specify an API level
  7. Android点击水波纹效果
  8. Android参数设置: Preference
  9. android 修改系统各种类型的声音的默认值和最大最小值

随机推荐

  1. 【面试】两个变量进行交替的N种方法
  2. 【已解决】您的要求未能完成。我们暂时未
  3. 0929作业
  4. 【面试】PHP 字符串翻(反)转的几种方法
  5. 【面试】PHP 中 两层 Foreach 中使用 con
  6. tp5中在js函数中给url传参数并进行连接
  7. 【h5和海报有什么区别】h5和海报区别有哪
  8. 桌面文件误删怎么找回??
  9. 位置不可用 chkdsk无法修复. 不知道怎么
  10. 位置不可用无法访问. 不知道怎样找到呢??