Android自定义加载圈的方法

作者:这个杀手不太累 时间:2023-07-16 14:38:46 

本文实例为大家分享了Android自定义加载圈的具体代码,供大家参考,具体内容如下

Android自定义加载圈的方法

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tlkg.welcome.loadingviewdemo.MainActivity">

    <com.tlkg.welcome.loadingviewdemo.LoadingView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true" />

</RelativeLayout>
public class LoadingView extends LinearLayout {
    public LoadingView(Context context) {
        this(context, null);
    }

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

    public LoadingView(Context context, @Nullable 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();
        }
    }
}
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp" />
    <solid android:color="#aa000000" />
</shape>

来源:https://blog.csdn.net/zhe_ge_sha_shou/article/details/75196308

标签:Android,加载圈
0
投稿

猜你喜欢

  • GoLang与Java各自生成grpc代码流程介绍

    2021-06-20 09:28:50
  • IDEA设置Tab选项卡快速的操作

    2022-07-15 18:20:31
  • C#利用控件拖拽技术制作拼图游戏

    2023-05-24 21:27:34
  • Java使用Collections.sort()排序的方法

    2022-09-11 18:47:55
  • android studio 使用Mocklocation虚拟定位

    2022-12-31 12:26:34
  • Android点亮屏幕或屏幕解锁和锁定以及其他相关权限实现代码

    2021-12-21 16:44:26
  • 浅谈web服务器项目中静态请求和动态请求处理

    2022-03-01 21:26:12
  • 关于Android中自定义ClassLoader耗时问题的追查

    2021-08-10 06:15:23
  • Java抽象类和接口的区别详情

    2023-05-23 20:09:59
  • Android编程简单实现九宫格示例

    2021-12-06 21:43:57
  • Android仿百度外卖自定义下拉刷新效果

    2022-05-24 06:32:45
  • Android编程自定义线程池与用法示例

    2021-11-27 14:46:53
  • springboot集成本地缓存Caffeine的三种使用方式(小结)

    2021-06-29 07:00:40
  • Android开发之获取短信验证码后按钮背景变化并且出现倒计时

    2022-12-21 10:09:40
  • c#通过进程调用cmd判断登录用户权限代码分享

    2021-07-24 00:53:26
  • Android 大文件切割与合并的实现代码

    2023-05-25 21:58:58
  • 死磕 java同步系列之synchronized解析

    2023-09-27 10:07:43
  • C#修改及重置电脑密码DirectoryEntry实现方法

    2021-12-03 05:08:16
  • springboot+jwt+微信小程序授权登录获取token的方法实例

    2022-07-11 17:53:57
  • C#中加载dll并调用其函数的实现方法

    2022-06-27 17:14:08
  • asp之家 软件编程 m.aspxhome.com