小言_互联网的博客

Android 居中圆角矩形 Loading框

182人阅读  评论(0)

相信项目中大家多多少少都会用到Loading框,用来优化一些体验
网上找了很多demo但是 要么有的太复杂,要么有的不满意,最后还是自己决定写一个吧。

1 UI效果


2 源代码

2.1 MsLoadingDialog.java

public class MsLoadingDialog extends Dialog {
   

    private TextView tvTips;
    private String mIngTips;
    private String mSuccessTips;
    private String mFailTips;
    private ImageView imgTop;
    private Drawable mIngDrawable;
    private Drawable mSuccessDrawable;
    private Drawable mFailDrawable;

    public MsLoadingDialog(@NonNull Context context) {
   
        super(context);
    }

    public MsLoadingDialog(@NonNull Context context, int themeResId) {
   
        super(context, themeResId);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading);
        tvTips = findViewById(R.id.tv_tips);
        imgTop = findViewById(R.id.img_top);
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }


    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
   
        if (tvTips != null) {
   
            tvTips.setText(mIngTips);
        }
        if (imgTop != null) {
   
            imgTop.setImageDrawable(mIngDrawable);
        }
    }

    /**
     * 初始化界面的确定和取消监听
     */
    private void initEvent() {
   
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }


    public MsLoadingDialog setIngTips(String tips) {
   
        mIngTips = tips;
        return this;
    }

    public MsLoadingDialog setSuccessTips(String tips) {
   
        mSuccessTips = tips;
        return this;
    }

    public MsLoadingDialog setFailTips(String tips) {
   
        mFailTips = tips;
        return this;
    }

    public MsLoadingDialog setIngImage(Drawable drawable) {
   
        mIngDrawable = drawable;
        return this;
    }

    public MsLoadingDialog setSuccessImage(Drawable drawable) {
   
        mSuccessDrawable = drawable;
        return this;
    }

    public MsLoadingDialog setFailImage(Drawable drawable) {
   
        mFailDrawable = drawable;
        return this;
    }

    public void showSuccessLoadView() {
   
        if (!isShowing()) {
   
            show();
        }
        if (imgTop != null) {
   
            imgTop.setAnimation(null);
            imgTop.setImageDrawable(mSuccessDrawable);
        }
        if (tvTips != null) {
   
            tvTips.setText(mSuccessTips);
        }
    }

    public void showFailLoadView() {
   
        if (!isShowing()) {
   
            show();
        }
        if (imgTop != null) {
   
            imgTop.setAnimation(null);
            imgTop.setImageDrawable(mFailDrawable);
        }
        if (tvTips != null) {
   
            tvTips.setText(mFailTips);
        }
    }

    public void showLoading() {
   
        if (!isShowing()) {
   
            show();
        }
        if (imgTop != null) {
   
            //开始旋转起来
            imgTop.setImageDrawable(mIngDrawable);
            RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setDuration(1500);
            rotateAnimation.setFillAfter(true);
            rotateAnimation.setRepeatMode(Animation.RESTART);
            rotateAnimation.setInterpolator(new LinearInterpolator());
            rotateAnimation.setRepeatCount(-1);
            imgTop.setAnimation(rotateAnimation);
            imgTop.startAnimation(rotateAnimation);
        }
        if (tvTips != null) {
   
            tvTips.setText(mIngTips);
        }
    }

}

2.2 MainActivity.java

public class MainActivity extends AppCompatActivity {
   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MsLoadingDialog myDialogT = new MsLoadingDialog(this, R.style.LoadingDialog);
        myDialogT.setSuccessTips("连接成功").setSuccessImage(getDrawable(R.mipmap.icon_loading_success));
        myDialogT.showSuccessLoadView();
    }
}

2.3 R.style.LoadingDialog

目录如下 src/main/res/values/themes.xml

<style name="LoadingDialog" parent="android:style/Theme.Dialog">
        <!--背景颜色和透明程度-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--是否去除标题-->
        <item name="android:windowNoTitle">true</item>
        <!--是否去除边框-->
        <item name="android:windowFrame">@null</item>
        <!--是否浮现在activity之上-->
        <item name="android:windowIsFloating">true</item>
        <!--是否模糊-->
        <item name="android:backgroundDimEnabled">false</item>
    </style>

2.4 dialog_loading.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="120dp"
    android:orientation="vertical"
    android:background="@drawable/bg_loading_dialog"
    android:layout_height="120dp">

    <ImageView
        android:id="@+id/img_top"
        android:layout_marginTop="25dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="33dp"
        android:layout_height="33dp"/>
    <TextView
        android:id="@+id/tv_tips"
        android:layout_marginTop="14dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="网络连接中"
        android:textColor="#FFFFFFFF"
        android:textSize="15sp" />
</LinearLayout>

2.5 bg_loading_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="6dp" />
            <solid android:color="#D6222222" />
        </shape>
    </item>
</selector>

转载:https://blog.csdn.net/LosingCarryJie/article/details/116855662
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场