当前位置:   article > 正文

Android - Loading 中间显示logo,外部ing转圈加载动画_android glide 图片loading转圈

android glide 图片loading转圈

需求:

需求很简单,就是中间显示一个logo,外部是一个转圈的动画,这样看着就是一个loading。

这里通过dialog来实现,比较好集中代码。

图片:

全图          loading1        loading2 

代码:

调用:这里使用了单例调用

  1. LoadingDialog.getInstance(context).show();//显示
  2. LoadingDialog.getInstance(context).dismiss();//隐藏

LoadingDialog.java弹框

  1. package com.baofu.sdkwebpayment;
  2. import android.app.Dialog;
  3. import android.content.Context;
  4. import android.graphics.Color;
  5. import android.graphics.drawable.ColorDrawable;
  6. import android.os.Bundle;
  7. import android.support.annotation.NonNull;
  8. import android.support.annotation.Nullable;
  9. import android.support.annotation.StyleRes;
  10. import android.view.Window;
  11. import android.view.WindowManager;
  12. import android.view.animation.AnimationSet;
  13. import android.view.animation.LinearInterpolator;
  14. import android.view.animation.RotateAnimation;
  15. import android.widget.ImageView;
  16. /**
  17. * Loading弹框
  18. * Created by zst on 2018/5/8.
  19. */
  20. public class LoadingDialog extends Dialog {
  21. private ImageView iv_ing;
  22. private AnimationSet animationSet;
  23. private static LoadingDialog instance;
  24. public static LoadingDialog getInstance(Context context) {
  25. if(instance == null) {
  26. instance = new LoadingDialog(context);
  27. }
  28. return instance;
  29. }
  30. private LoadingDialog(@NonNull Context context) {
  31. super(context);
  32. }
  33. private LoadingDialog(@NonNull Context context, @StyleRes int themeResId) {
  34. super(context, themeResId);
  35. }
  36. private LoadingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
  37. super(context, cancelable, cancelListener);
  38. }
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. //背景透明处理
  43. getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  44. getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
  45. WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
  46. getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  47. getWindow().setDimAmount(0f);
  48. this.setContentView(R.layout.dialog_loading);
  49. //设置dialog属性
  50. setCancelable(true);
  51. setCanceledOnTouchOutside(false);
  52. iv_ing = findViewById(R.id.iv_ing);
  53. //加载动画
  54. loadIng();
  55. }
  56. @Override
  57. protected void onStart() {
  58. super.onStart();
  59. iv_ing.startAnimation(animationSet);//开始播放
  60. }
  61. @Override
  62. protected void onStop() {
  63. super.onStop();
  64. }
  65. //加载动画
  66. private void loadIng() {
  67. animationSet = new AnimationSet(true);
  68. RotateAnimation animation_rotate = new RotateAnimation(0, +359,
  69. RotateAnimation.RELATIVE_TO_SELF, 0.5f,
  70. RotateAnimation.RELATIVE_TO_SELF, 0.5f);
  71. //第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度
  72. //第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
  73. //第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
  74. animation_rotate.setRepeatCount(-1);
  75. animation_rotate.setStartOffset(0);
  76. animation_rotate.setDuration(1000);
  77. LinearInterpolator lir = new LinearInterpolator();
  78. animationSet.setInterpolator(lir);
  79. animationSet.addAnimation(animation_rotate);
  80. }
  81. }

dialog_loading.xml布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/ll_body"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:background="#00000000"
  7. android:orientation="horizontal"
  8. android:padding="10dp">
  9. <RelativeLayout
  10. android:layout_width="80dp"
  11. android:layout_height="80dp">
  12. <ImageView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_centerInParent="true"
  16. android:src="@drawable/ic_loading_bac" />
  17. <ImageView
  18. android:layout_width="50dp"
  19. android:layout_height="50dp"
  20. android:layout_centerInParent="true"
  21. android:src="@drawable/ic_loading_logo" />
  22. <ImageView
  23. android:id="@+id/iv_ing"
  24. android:layout_width="60dp"
  25. android:layout_height="60dp"
  26. android:layout_centerInParent="true"
  27. android:src="@drawable/ic_loading_ing" />
  28. </RelativeLayout>
  29. </LinearLayout>

图标最上面有的ic_loading_logo是logo,ic_loading_ing是loading1或2

结尾

因为loading是比较常用的,并且跟随dialog的生命周期,所以只用空时dialog就可以控制动画了,不必担心内存问题。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/542998
推荐阅读
相关标签
  

闽ICP备14008679号