自定义view加载圆圈转圈效果

圆圈转圈效果的实现

public class LoadingView extends LinearLayout {
    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context,  AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        setBackgroundResource(R.drawable.loadingsp);

        LoadView loading = new LoadView(getContext());
        loading.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
        addView(loading);

        TextView tv = new TextView(getContext());
        tv.setText("正在加载中");
        LinearLayout.LayoutParams layoutParams = new LayoutParams(-2, -2);
        layoutParams.setMargins(0, 10, 0, 0);
        tv.setLayoutParams(layoutParams);
        tv.setGravity(Gravity.CENTER);
        tv.setTextColor(Color.WHITE);
        addView(tv);
    }


    class LoadView extends View {

        Paint mPaint;

        private int mWidth;
        private int mHeight;

        private int mCurrentIndex = 0;

        private int count = 12;

        public LoadView(Context context) {
            super(context);
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()));
            mPaint.setColor(Color.WHITE);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (mCurrentIndex >= count) {
                mCurrentIndex = 0;
            }

            int endAlpha = 255 / count;
            for (int i = 0; i < count; i++) {
                int alpha;
                if (mCurrentIndex - i > 0) {
                    alpha = endAlpha * (mCurrentIndex - i);
                } else {
                    alpha = 255 - 255 / count * (i - mCurrentIndex);
                }

                mPaint.setColor(Color.argb(alpha, 255, 255, 255));
                canvas.drawLine(mWidth / 2, 0, mWidth / 2, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()), mPaint);
                canvas.rotate(360 / count, mWidth / 2, mHeight / 2);
            }
            mCurrentIndex++;
            postInvalidateDelayed(100);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mWidth = getWidth();
            mHeight = getHeight();
        }
    }
}

效果
在这里插入图片描述
当然你也可以采用第三方开源框架

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值