赞
踩
需求很简单,就是中间显示一个logo,外部是一个转圈的动画,这样看着就是一个loading。
这里通过dialog来实现,比较好集中代码。
全图 loading1
loading2
调用:这里使用了单例调用
- LoadingDialog.getInstance(context).show();//显示
- LoadingDialog.getInstance(context).dismiss();//隐藏
LoadingDialog.java弹框
- package com.baofu.sdkwebpayment;
-
- import android.app.Dialog;
- import android.content.Context;
- import android.graphics.Color;
- import android.graphics.drawable.ColorDrawable;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.annotation.StyleRes;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.animation.AnimationSet;
- import android.view.animation.LinearInterpolator;
- import android.view.animation.RotateAnimation;
- import android.widget.ImageView;
-
- /**
- * Loading弹框
- * Created by zst on 2018/5/8.
- */
-
- 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);
- //第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度
- //第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
- //第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
- animation_rotate.setRepeatCount(-1);
- animation_rotate.setStartOffset(0);
- animation_rotate.setDuration(1000);
- LinearInterpolator lir = new LinearInterpolator();
- animationSet.setInterpolator(lir);
- animationSet.addAnimation(animation_rotate);
- }
- }

dialog_loading.xml布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/ll_body"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00000000"
- android:orientation="horizontal"
- android:padding="10dp">
-
- <RelativeLayout
- android:layout_width="80dp"
- android:layout_height="80dp">
-
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:src="@drawable/ic_loading_bac" />
-
- <ImageView
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:layout_centerInParent="true"
- android:src="@drawable/ic_loading_logo" />
-
- <ImageView
- android:id="@+id/iv_ing"
- android:layout_width="60dp"
- android:layout_height="60dp"
- android:layout_centerInParent="true"
- android:src="@drawable/ic_loading_ing" />
- </RelativeLayout>
- </LinearLayout>

图标最上面有的ic_loading_logo是logo,ic_loading_ing是loading1或2
因为loading是比较常用的,并且跟随dialog的生命周期,所以只用空时dialog就可以控制动画了,不必担心内存问题。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。