服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现创意LoadingView动画效果

Android实现创意LoadingView动画效果

2021-05-26 15:30Android开发网 Android

这篇文章主要介绍了Android实现创意LoadingView动画效果的相关资料,需要的朋友可以参考下

Android上的热火锅煮萝卜蔬菜的Loading动画效果。 这是一个锅煮萝卜的Loading动画,效果仿照自之前IOS上看到的一个效果,觉得挺有意思,就移植过来了,在此完成了Dialog的样式,方便使用者作为LoadingView去使用。
关键性代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package yellow5a5.demo.boilingloadingview.View;
 
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.ClipDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
 
import java.util.Timer;
import java.util.TimerTask;
 
import yellow5a5.demo.boilingloadingview.R;
 
/**
 * Created by Weiwu on 16/1/2.
 */
public class BoilingPanView extends RelativeLayout {
 
 
  private View mView;
  private ClipDrawable mWaterDrawable;
 
  private WaterView mWaterView;
  private FlameView mFlameView;
 
  private View mPea1;
  private View mPea2;
  private ImageView mPotato;
  private ImageView mCarrot;
  private ImageView mCoverView;
 
  private Animation mLeftInAnim;
  private Animation mRightInAnim;
 
  private boolean isRightRotate = true;
  private ValueAnimator mCoverAnim;
 
  private BoilingAnimListener mBoilingAnimListener;
 
  public interface BoilingAnimListener {
    //初始动画结束监听
    void onFirstAnimEnd();
  }
 
  public void setBoilingAnimListener(BoilingAnimListener l) {
    this.mBoilingAnimListener = l;
  }
 
  private Handler mHandle = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
      if (msg.what == 0X0000) {
        mWaterDrawable.setLevel(mWaterDrawable.getLevel() + 800);
      }
      return false;
    }
  });
 
  public BoilingPanView(Context context) {
    this(context, null);
  }
 
  public BoilingPanView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }
 
  public BoilingPanView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mView = LayoutInflater.from(context).inflate(R.layout.boiling_pan, this, true);
    initView();
    initStartAnim();
    initCoverAnim();
  }
 
  private void initView() {
    mWaterView = (WaterView) mView.findViewById(R.id.img_water);
    mFlameView = (FlameView) mView.findViewById(R.id.flame);
    mCoverView = (ImageView) mView.findViewById(R.id.img_cover);
    mPea1 = mView.findViewById(R.id.img_pea1);
    mPea2 = mView.findViewById(R.id.img_pea2);
    mPotato = (ImageView) mView.findViewById(R.id.img_potato);
    mCarrot = (ImageView) mView.findViewById(R.id.img_carrot);
    mWaterDrawable = (ClipDrawable) mWaterView.getDrawable();
  }
 
  private void initStartAnim() {
    mLeftInAnim = AnimationUtils.loadAnimation(getContext(), R.anim.left_in_anim);
    mRightInAnim = AnimationUtils.loadAnimation(getContext(), R.anim.right_in_anim);
  }
 
  /*
  抖动的盖子
   */
  private void initCoverAnim() {
    mCoverAnim = ValueAnimator.ofFloat(0f, 1f, 0f).setDuration(800);
    mCoverAnim.setRepeatMode(Animation.REVERSE);
    mCoverAnim.setRepeatCount(-1);
    mCoverAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        float value = (float) animation.getAnimatedValue();
        if (isRightRotate) {
          mCoverView.setRotation(value * 5);
        } else {
          mCoverView.setRotation(-value * 5);
        }
        mCoverView.setTranslationY(-value * TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics()));
      }
    });
    mCoverAnim.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationRepeat(Animator animation) {
        super.onAnimationRepeat(animation);
        isRightRotate = !isRightRotate;
      }
    });
  }
 
  /*
  开始启动的动画
   */
  public void beginFirstInAnim() {
    mPea1.setVisibility(VISIBLE);
    mPea2.setVisibility(VISIBLE);
    mPotato.setVisibility(VISIBLE);
    mCarrot.setVisibility(VISIBLE);
    mCoverView.setVisibility(VISIBLE);
    mPea1.startAnimation(mLeftInAnim);
    mPea2.startAnimation(mLeftInAnim);
    mPotato.startAnimation(mLeftInAnim);
    mCarrot.startAnimation(mRightInAnim);
    mCoverView.startAnimation(mRightInAnim);
    mRightInAnim.setAnimationListener(new Animation.AnimationListener() {
      @Override
      public void onAnimationStart(Animation animation) {
 
      }
 
      @Override
      public void onAnimationEnd(Animation animation) {
        if (mBoilingAnimListener != null) {
          //这里是为了给外部留有操作的空间
          mBoilingAnimListener.onFirstAnimEnd();
        } else {
          beginBoilingAnim();
        }
      }
 
      @Override
      public void onAnimationRepeat(Animation animation) {
 
      }
    });
  }
 
  /*
  开始加水燃火动画
   */
  public void beginBoilingAnim() {
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        mHandle.sendEmptyMessage(0X0000);
        if (mWaterDrawable.getLevel() >= 10000) {
          timer.cancel();
        }
      }
    }, 0, 50);
    mFlameView.startFlaming();
    mCoverAnim.start();
  }
 
  /*
  重置动画
   */
  public void resetAnim() {
    mWaterDrawable.setLevel(0);
    mWaterView.resetBubbleAnim();
    mFlameView.stopFlaming();
    mPea1.setVisibility(INVISIBLE);
    mPea2.setVisibility(INVISIBLE);
    mPotato.setVisibility(INVISIBLE);
    mCarrot.setVisibility(INVISIBLE);
    mCoverView.setVisibility(INVISIBLE);
 
  }
}

希望本文所述对大家学习Android软件编程有所帮助。

延伸 · 阅读

精彩推荐