Android-自定义View网络加载

一、前言

自定义View-仿QQ运动步数进度效果基础上衍生的网络加载,效果图如下:
loading

二、直接上三部曲

1)定义attrs.xml

    <declare-styleable name="LoadingView">
        <attr name="typeLoading" format="string"/>
        <attr name="circleWidth" format="dimension"/>
        <attr name="InnerColor" format="color"/>
        <attr name="foreginColor" format="color" />
    </declare-styleable>

2)在布局中使用

    <com.example.myapplication.Widget.LoadingView
        android:id="@+id/loading"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:foreginColor="@color/colorBlue"
        app:InnerColor="@color/colorRed"
        app:circleWidth="5dp"
        app:typeLoading="water"
        />

3)正文代码在此

package com.example.myapplication.Widget;


import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.example.myapplication.R;

public class LoadingView extends View {

    /**
     * 内外弧的宽高
     */
    private int circleWidth = 5;
    /**
     * 内弧颜色
     */
    private int InnerColor = Color.BLUE;
    /**
     * 外弧颜色
     */
    private int foreginColor = Color.RED;
    /**
     * 圆角颜色,也可在layout中设置背景,如果自己设置背景的话还要加圆角,这里已经是处理好了
     */
    private int rectColor = Color.parseColor("#999999");
    /**
     * 外弧画笔
     */
    private Paint paintforegin;
    /**
     * 内弧画笔
     */
    private Paint paintInner;
    /**
     * 背景圆角画笔
     */
    private Paint paintRect;
    /**
     * 外弧起始与终止点
     */
    private int startForegin = 135;
    private int endForegin = 275;
    /**
     * 内弧起始与终止点
     */
    private int startInner = 45;
    private int endInner = 90;

    /**
     * 内外弧半径
     */
    private int radus = 150;
    private int innerPading = radus - 70;
    /**
     * 背景圆角的半径
     */
    private int rectRadus = radus + 50;
    /**
     * 背景圆角大小
     */
    private int rectCircle = 30;

    public LoadingView(Context context) {
        super(context, null);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initType(attrs, context);
        loading();
    }

    /**
     * 获得属性
     * @param attrs
     * @param context
     */
    private void initType(AttributeSet attrs, Context context) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingView);
        foreginColor = typedArray.getColor(R.styleable.LoadingView_foreginColor, foreginColor);
        InnerColor = typedArray.getColor(R.styleable.LoadingView_InnerColor, InnerColor);
        circleWidth = typedArray.getDimensionPixelSize(R.styleable.LoadingView_circleWidth, circleWidth);
        typedArray.recycle();
        initCirclePaint();
    }


    /**
     * 初始化画笔
     */
    private void initCirclePaint(){
        paintforegin = new Paint();
        paintforegin.setAntiAlias(true);
        paintforegin.setDither(true);
        paintforegin.setStrokeWidth(circleWidth);
        paintforegin.setColor(foreginColor);
        paintforegin.setStyle(Paint.Style.STROKE);
        paintforegin.setStrokeCap(Paint.Cap.ROUND);
        paintforegin.setStrokeJoin(Paint.Join.ROUND);

        paintInner = new Paint();
        paintInner.setAntiAlias(true);
        paintInner.setDither(true);
        paintInner.setStrokeWidth(circleWidth);
        paintInner.setColor(InnerColor);
        paintInner.setStyle(Paint.Style.STROKE);
        paintInner.setStrokeJoin(Paint.Join.ROUND);
        paintInner.setStrokeCap(Paint.Cap.ROUND);

        paintRect = new Paint();
        paintRect.setAntiAlias(true);
        paintRect.setDither(true);
        paintRect.setColor(rectColor);
        paintRect.setStyle(Paint.Style.FILL);

    }
    /**
     * 测量处理
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取宽高的模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //指定宽高的大小
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        //如果宽高设置为wrap_content时,刚默认为300
        if(widthMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.AT_MOST){
            width = 300;
            height = width;
        }
        //如果宽高不一致时,则以宽为标准
        if(width != height){
            height = width;
        }

        setMeasuredDimension(width,height);
    }

    /**
     * 中心点
     */
    private int y;
    private int x;

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        x = (right - left) / 2;
        y = (bottom - top) / 2;
        super.onLayout(changed, left, top, right, bottom);
    }

    private int DptoPx(int dp, Context context) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawCircle(canvas);

        drawArc(canvas);
        drawArc2(canvas);

    }


    /**
     * 绘制外弧
     * @param canvas
     */
    private void drawArc(Canvas canvas) {
        RectF rectF = new RectF(x - radus, y - radus, x + radus, y + radus);
        canvas.drawArc(rectF, startForegin, endForegin, false, paintforegin);
    }
    /**
     * 绘制内弧
     * @param canvas
     */
    private void drawArc2(Canvas canvas) {
        RectF rectF = new RectF(x - innerPading, y - innerPading, x + innerPading, y + innerPading);
        canvas.drawArc(rectF, startInner, endInner, false, paintInner);
    }

    /**
     * 绘制背景圆角
     * @param canvas
     */
    private void drawCircle(Canvas canvas){
        RectF rectF = new RectF(x - rectRadus, y - rectRadus, x + rectRadus, y + rectRadus);

        canvas.drawRoundRect (rectF,rectCircle,rectCircle,paintRect);
    }

    /**
     * 绘制更新
     * @param value
     */
    private void reDraw(int value) {

        startForegin += value;

        startInner -= value;


        postInvalidate();
    }

    /**
     * 设置属性动画来重新绘制
     */
    @SuppressLint("WrongConstant")
    private void loading() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0,2);
        valueAnimator.setDuration(1);
        //设置重复次数
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        //设置重复模式
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                reDraw(value);
            }
        });
    }
}

简书地址:Android-自定义View网络加载

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要在 Android 应用中使用自定义加载动画,可以通过创建自定义的布局和使用 Animation 类来实现。以下是一个简单的示例,演示如何在 Android 应用中自定义加载动画: 首先,在 res/layout 目录下创建一个新的布局文件,例如 custom_loading.xml,其中包含自定义加载动画布局,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <ImageView android:id="@+id/loading_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/custom_loading_animation" android:layout_marginBottom="16dp"/> <TextView android:id="@+id/loading_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Loading..." android:textColor="@android:color/white"/> </LinearLayout> ``` 其中,loading_image 是一个自定义加载动画的 ImageView,loading_text 是一个文本视图,用于显示加载消息。 接下来,在 res/drawable 目录下创建一个新的动画文件,例如 custom_loading_animation.xml,其中包含自定义加载动画动画,例如: ```xml <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/custom_loading_frame1" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame2" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame3" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame4" android:duration="100" /> </animation-list> ``` 其中,custom_loading_frame1、custom_loading_frame2、custom_loading_frame3、custom_loading_frame4 是自定义加载动画的帧。 最后,在你的 Activity 中,使用 LayoutInflater 类将 custom_loading.xml 布局文件实例化为 View 对象,并使用 Animation 类将 custom_loading_animation.xml 动画文件加载到 ImageView 中,例如: ```java LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom_loading, null); ImageView imageView = (ImageView) view.findViewById(R.id.loading_image); Animation animation = AnimationUtils.loadAnimation(this, R.drawable.custom_loading_animation); imageView.startAnimation(animation); ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setCancelable(false); progressDialog.show(); progressDialog.setContentView(view); ``` 其中,使用 LayoutInflater 类将 custom_loading.xml 布局文件实例化为 View 对象,并使用 findViewById() 方法获取 loading_image ImageView 对象。然后,使用 AnimationUtils.loadAnimation() 方法将 custom_loading_animation.xml 动画文件加载到 ImageView 中,并调用 startAnimation() 方法开始播放动画。最后,将 View 对象设置为 ProgressDialog 的内容视图,调用 show() 方法显示 ProgressDialog。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值