Android如何自定义加载框

在Android应用中,经常会用到网络传输,不可避免的是,在传输过程中会受网络的影响,在等待服务器的响应的时候,用户体验就至关重要了,所以应该在等待的过程中,给应用加上一个加载框,这样用户体验就大大提升。那么该如何实现自定义的加载框呢?
第一步,准备好加载动画的素材,然后创建progress_loading.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/progress_1" android:duration="200"/>
    <item android:drawable="@drawable/progress_2" android:duration="200"/>
    <item android:drawable="@drawable/progress_3" android:duration="200"/>
    <item android:drawable="@drawable/progress_4" android:duration="200"/>
    <item android:drawable="@drawable/progress_5" android:duration="200"/>
    <item android:drawable="@drawable/progress_6" android:duration="200"/>
    <item android:drawable="@drawable/progress_7" android:duration="200"/>
    <item android:drawable="@drawable/progress_8" android:duration="200"/>
</animation-list>

在这里插入图片描述
第二步,创建弹窗dialog_loading.xml,在ImageView标签中加入progress_loading.xml文件,其他弹窗样式根据自己的需求来该,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <ImageView
            android:id="@+id/loading_image"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerHorizontal="true"
            android:paddingTop="10dp"
            android:src="@drawable/progress_loading"/>
        <TextView
            android:id="@+id/loading_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/loading_image"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="努力加载中..."
            android:textColor="#999999"
            android:textSize="12dp" />
</RelativeLayout>

第三步,实现创建一个LoadingDialog的类,然后继承弹窗继承类,代码如下:


/**
 * 加载中动画弹窗
 */
public class LoadingDialog extends Dialog {
    private String text;//提示
    private AnimationDrawable animationDrawable;
 
    public LoadingDialog(Context context) {
        super(context, R.style.loadingDialog);
    }
 
    public LoadingDialog(Context context, String text) {
        super(context, R.style.loadingDialog);  //这里设置了Dialog的Style让其背景为透明的,并且没有title,具体代码就不贴了
        this.text = text;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        if (text!= null){
            TextView textView = (TextView) findViewById(R.id.loading_text);
            textView.setText(text);
        }
        ImageView progressImageView = (ImageView) findViewById(R.id.loading_image); //在ImageView上使用帧动画
        animationDrawable = (AnimationDrawable) getContext().getResources().getDrawable(R.drawable.progress_loading); //帧动画的初始化
        progressImageView.setImageDrawable(animationDrawable); //将动画设置在ImageView上
    }
 
    /**
     * 开始帧动画
     */
    @Override
    protected void onStart() {
        animationDrawable.start();
        super.onStart();
    }
 
    /**
     * 停止帧动画
     */
    @Override
    protected void onStop() {
        animationDrawable.stop();
        super.onStop();
    }
}

第四步,直接在相关页面实例化LoadingDialog类,然后在网络请求的代码前调用实例化对象的show()方法,网络请求代码后调用实例化对象的hide()方法即可;
最终效果如下:

在这里插入图片描述

  • 0
    点赞
  • 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、付费专栏及课程。

余额充值