导航刚刚打开的时候,如果等待时间比较长,那是一个不好的用户体验。所以此时设置一个加载的界面比较合适。话不多说,上代码
使用时候直接调用 main_loading.start 和main_loading.stop
1----自定义view
public class LoadingView extends View {
private Paint mPaint;
private int mAngle;
private float mRadius;
private int mWidth, mHeight;
private boolean startFlag = true;
public void start() {
if (startFlag) return;
startFlag = true;
invalidate();
setVisibility(View.VISIBLE);
}
public void stop() {
startFlag = false;
invalidate();
mAngle = 0;
setVisibility(View.GONE);
}
public LoadingView(Context context) {
super(context);
}
public LoadingView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public LoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(getResources().getColor(R.color.loading_blue));
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeWidth(4);
stop();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
mWidth = right - left;
mHeight = bottom - top;
mRadius = Math.min(mWidth, mHeight) / 2f;
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
if (!startFlag) return;
super.onDraw(canvas);
canvas.translate(mWidth / 2f - mRadius, mHeight / 2f - mRadius);
canvas.rotate(mAngle, mRadius, mRadius);
for (int i = 0; i < 12; i++) {
mPaint.setAlpha((int) Math.max(255 * 0.1f, 255 * (9 - i) / 10f));
canvas.drawCircle(mRadius, Math.max(6, 20 - i * 2), Math.max(3, 10 - i), mPaint);
canvas.rotate(-30, mRadius, mRadius);
}
mAngle += 30;
postInvalidateDelayed(80);
}
}
2---引用view