Android 开发之Loading

Posted Java璐到底

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 开发之Loading相关的知识,希望对你有一定的参考价值。

最近公司需要做一个APP,自己也是学习了一下,下面是做的网络加载的loading

话不多说,直接上代码

是用dialog写的

后台 LoadingDialog.java
public class LoadingDialog extends Dialog 
    private ImageView iv_ing;
    private AnimationSet animationSet;

    private static LoadingDialog instance;

    public static LoadingDialog getInstance(Context context) 
        if(instance == null) 
            instance = new LoadingDialog(context);
        

        return instance;
    

    private LoadingDialog(@NonNull Context context) 
        super(context);
    

    private LoadingDialog(@NonNull Context context, @StyleRes int themeResId) 
        super(context, themeResId);
    

    private LoadingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) 
        super(context, cancelable, cancelListener);
    

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        //背景透明处理
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setDimAmount(0f);

        this.setContentView(R.layout.dialog_loading);

        //设置dialog属性
        setCancelable(true);
        setCanceledOnTouchOutside(false);

        iv_ing = findViewById(R.id.iv_ing);

        //加载动画
        loadIng();
    

    @Override
    protected void onStart() 
        super.onStart();
        iv_ing.startAnimation(animationSet);//开始播放
    

    @Override
    protected void onStop() 
        super.onStop();
    

    //加载动画
    private void loadIng() 
        animationSet = new AnimationSet(true);
        RotateAnimation animation_rotate = new RotateAnimation(0, +359,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);

        animation_rotate.setRepeatCount(-1);
        animation_rotate.setStartOffset(0);
        animation_rotate.setDuration(1000);
        LinearInterpolator lir = new LinearInterpolator();
        animationSet.setInterpolator(lir);
        animationSet.addAnimation(animation_rotate);
    
前端XML :dialog_loading.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.4"
    android:background="#000000"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="10dp">
   <RelativeLayout
       android:layout_width="200dp"

       android:layout_height="match_parent">
      <ImageView
          android:id="@+id/iv_ing"
          android:layout_width="60dp"
          android:layout_height="60dp"
          android:layout_centerInParent="true"
          android:src="@drawable/loading" />
      <TextView
          android:paddingTop="5dp"
          android:gravity="center"
          android:layout_width="300dp"
          android:layout_height="wrap_content"
          android:textColor="@color/teal_200"
          android:text="正在加载中..."

          android:layout_below="@+id/iv_ing"

          android:layout_marginTop="20dp"/>
   </RelativeLayout>
</LinearLayout >

调用方法

LoadingDialog.getInstance(this).show();//显示
LoadingDialog.getInstance(getApplicationContext()).dismiss();//隐藏

下面附上运行截图

 

以上是关于Android 开发之Loading的主要内容,如果未能解决你的问题,请参考以下文章

Android 开发之Loading

android 自定义控件之简单的loading框

AndroidAndroid开发之常用的loading等待效果实现,仿微博等待动画。两种实现方式详解

Android开发:网络请求延迟下使用loading,缓解尴尬~~~

Android自定义View实战之仿百度加载动画,一种优雅的Loading方式

Android自定义View实战之仿百度加载动画,一种优雅的Loading方式