自定义loadview

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);
    }
}
 
  
 
  
 
  
添加view
 
  
<com.myapp.widgets.LoadingView
    android:id="@+id/main_loading"
    android:layout_width="@dimen/loadingview_width"
    android:layout_height="@dimen/loadingview_height"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="@dimen/splash_loading_margin_top" />
 
  
实现start和stop方法(main_loading
 
  
效果图
 
  
 
  

你可能感兴趣的:(android)