SweepLoadingView-延时动画

简介: 这两天写了一个关于延时加载的控件,之前一直是用的 ProgressWheel, 后来参考了 lovejjfg 这个控件设计思路还是很巧妙的。不会用作图工具,凑合着看吧。

这两天写了一个关于延时加载的控件,之前一直是用的 ProgressWheel, 后来参考了 lovejjfg 这个控件设计思路还是很巧妙的。不会用作图工具,凑合着看吧。

SweepLoadingView.png

怎么用?

<pre>
<github.alex.sweeploadingview.SweepLoadingView
xmlns:SweepLoadingView="http://schemas.android.com/apk/res-auto"
android:layout_width="112dp"
android:layout_height="112dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
SweepLoadingView:slv_circleColor="#795548"
SweepLoadingView:slv_circleWidth="8dp"
SweepLoadingView:slv_duration="1200"
SweepLoadingView:slv_gapAngle="30"
SweepLoadingView:slv_strokeCap="square" />

</pre>

效果图

001.gif

奉上源码

<pre>
package github.alex.sweeploadingview;
/**
* 作者:Alex
*

* 时间:2016/8/24 16:31
*

* 博客地址:http://www.jianshu.com/users/c3c4ea133871/subscriptions
*

*

*/
public class SweepLoadingView extends View {
private RectF rectF;
private Paint paint;
private int circleColor;
private int circleWidth;
private int strokeCap;
/**
* 扫描一周 需要的时间
*/
private int swipeDuration;
/**
* 起点的角度
*/
private float startAngle;
/**
* 扫描的角度
*/
private float sweepAngle;

/\*\*
 \* 终点 和 起点的间隔 角度
 \*/
private float gapAngle;
/\*\*
 \* 起始点的偏移量
 \*/
private float startAngleOffset;
private ObjectAnimator sweepObjectAnimator;
private ObjectAnimator startAngleObjectAnimator;
/\*\*
 \* 扫描一周
 \*/
private boolean isSweepRepeat;

public SweepLoadingView(Context context) {
    super(context);
    initView(null);
}

public SweepLoadingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(attrs);
}

private void initView(AttributeSet attrs) {
    startAngleOffset = 0;
    isSweepRepeat = false;
    initObjectAnimation();
}

private void initObjectAnimation() {
    sweepObjectAnimator = ObjectAnimator.ofFloat(this, new AngleProperty(Float.class, "sweepAngle"), 360F - gapAngle \* 2);
    sweepObjectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    sweepObjectAnimator.setDuration(swipeDuration);
    sweepObjectAnimator.setRepeatCount(Integer.MAX_VALUE);
    sweepObjectAnimator.setRepeatMode(ValueAnimator.RESTART);
    sweepObjectAnimator.addListener(new SweepAngleAnimatorListener());

    startAngleObjectAnimator = ObjectAnimator.ofFloat(this, new AngleProperty(Float.class, "startAngle"), 360F);
    startAngleObjectAnimator.setInterpolator(new LinearInterpolator());
    startAngleObjectAnimator.setDuration(1500);
    startAngleObjectAnimator.setRepeatCount(Integer.MAX_VALUE);
    startAngleObjectAnimator.setRepeatMode(ValueAnimator.RESTART);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    int min = Math.min(w, h);
    int dWidth = circleWidth / 2;
    rectF.left = dWidth;
    rectF.right = min - dWidth;
    rectF.top = dWidth;
    rectF.bottom = min - dWidth;
}

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    float startAngle = this.startAngle - startAngleOffset;
    float sweepAngle = this.sweepAngle;
    if (isSweepRepeat) {
        startAngle += sweepAngle;
        sweepAngle = 360 - sweepAngle - gapAngle;
    } else {
        sweepAngle += gapAngle;
    }
    canvas.drawArc(rectF, startAngle, sweepAngle, false, paint);
}

private void startSweepAnimation() {
    if (!sweepObjectAnimator.isStarted()) {
        LogUtil.e("开始");
        sweepObjectAnimator.start();
    }
    if (!startAngleObjectAnimator.isStarted()) {
        startAngleObjectAnimator.start();
    }
}

private void stopSweepAnimation() {
    LogUtil.e("停止");
    sweepObjectAnimator.cancel();
    startAngleObjectAnimator.cancel();
}

private final class SweepAngleAnimatorListener extends SimpleAnimatorListener {
    @Override
    public void onAnimationRepeat(Animator animation) {
        handleSweepRepeat();
    }
}

/\*\*
 \* 处理扫描一周的 情况
 \*/
private void handleSweepRepeat() {
    if (isSweepRepeat) {
        startAngleOffset = (startAngleOffset + gapAngle \* 2) % 360;
    }
    isSweepRepeat = !isSweepRepeat;
}

private final class AngleProperty extends Property<SweepLoadingView, Float> {
    private String name;

    public AngleProperty(Class<Float> type, String name) {
        super(type, name);
        this.name = name;
    }

    @Override
    public Float get(SweepLoadingView object) {
        if ("sweepAngle".equals(name)) {
            return object.getSweepAngle();
        } else {
            return object.getStartAngle();
        }
    }

    @Override
    public void set(SweepLoadingView object, Float value) {
        if ("sweepAngle".equals(name)) {
            object.setSweepAngle(value);
        } else {
            object.setStartAngle(value);
        }
    }
}

private float getSweepAngle() {
    return sweepAngle;
}

private void setSweepAngle(float sweepAngle) {
    this.sweepAngle = sweepAngle;
    invalidate();
}

private float getStartAngle() {
    return startAngle;
}

private void setStartAngle(float startAngle) {
    this.startAngle = startAngle;
    invalidate();
}

}

</pre>

目录
相关文章
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17038 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
6天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1262 3
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2347 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3821 22
|
19天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。