免责声明

易百易数码科技

android 网络加载动画_加载网络实例

使用Android中的AsyncTask或Volley库加载网络数据,同时添加Glide或Picasso库实现图片加载动画。

在Android开发中,网络加载动画是一种常见的用户体验优化手段,它可以在用户等待数据加载时提供视觉反馈,增强应用的可用性和吸引力,以下是一个简单的网络加载动画实例:

1、在项目的res/anim目录下创建一个名为loading_animation.xml的文件,然后添加以下代码:

android 网络加载动画_加载网络实例-图1
<?xml version="1.0" encoding="utf8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="1000">
    <shape>
        <size
            android:width="100dp"
            android:height="100dp"/>
        <solid android:color="#FF4081"/>
    </shape>
</rotate>

2、在需要显示网络加载动画的地方(例如Activity或Fragment),创建一个ProgressBar控件,并将其背景设置为刚刚创建的动画:

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:indeterminate="false"
    android:max="100" />

3、在Activity或Fragment的布局文件中,将ProgressBar的背景设置为刚刚创建的动画:

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:indeterminate="false"
    android:max="100" />

4、在Activity或Fragment的Java代码中,为ProgressBar设置动画:

ProgressBar progressBar = findViewById(R.id.progressBar);
AnimationDrawable animationDrawable = (AnimationDrawable) progressBar.getBackground();
animationDrawable.start();

5、当网络请求开始时,启动动画;当网络请求结束时,停止动画:

// 开始网络请求前启动动画
progressBar.setVisibility(View.VISIBLE);
AnimationDrawable animationDrawable = (AnimationDrawable) progressBar.getBackground();
animationDrawable.start();
// 网络请求结束后停止动画并隐藏进度条
progressBar.setVisibility(View.GONE);
animationDrawable.stop();
android 网络加载动画_加载网络实例-图2
分享:
扫描分享到社交APP
上一篇
下一篇