This repository has been archived by the owner on Dec 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloudView.java
141 lines (118 loc) · 4.62 KB
/
CloudView.java
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
/**
* @author Kanghee Lee Created on 13-02-10
*/
public class CloudView extends View {
private Bitmap mBitmap;
private static final int NUM_CLOUDS = 4; // Current number of clouds
ArrayList<Texture> clouds = new ArrayList<Texture>(NUM_CLOUDS); // List of
// current
// Animator used to drive all separate cloud animations. Rather than have
// potentially hundreds of separate animators, we just use one and then update all
// clouds for each frame of that single animation.
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
private Random mRandom;
private ViewPager mPager;
private Context mContext;
private long mPrevTime; // Used to track elapsed time for
// animations and fps
private int mMaxNum;
private boolean mIsPlaying;
private Matrix mMatrix = new Matrix();
private int mBitmapWidth;
public CloudView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mRandom = new Random();
if (android.os.Build.VERSION.SDK_INT >= 11)
setLayerType(LAYER_TYPE_HARDWARE, null);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cloud);
mBitmapWidth = mBitmap.getWidth();
// This listener is where the action is for the flak animations. Every
// frame of the animation, we calculate the elapsed time and update every cloud's
// position and rotation according to its speed.
animator.addUpdateListener(new AnimatorUpdateListener() {
Texture cloud;
float secs;
long nowTime;
@Override
public void onAnimationUpdate(ValueAnimator arg0) {
nowTime = System.currentTimeMillis();
secs = (float) (nowTime - mPrevTime) * 0.01f;
mPrevTime = nowTime;
if (clouds.size() < mMaxNum) {
if (mRandom.nextInt(6) == 4) {
createCloud();
}
}
for (int i = 0; i < clouds.size(); i++) {
cloud = clouds.get(i);
cloud.x -= (cloud.speed * secs);
if (cloud.x + mBitmapWidth < 0) {
// If a flake falls off the bottom, send it back to the
// top
clouds.remove(cloud);
}
}
// Force a redraw to see the clouds in their new positions and
// orientations
invalidate();
}
});
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setDuration(10000);
animator.setInterpolator(new AccelerateInterpolator());
}
private void createCloud() {
int count = mPager.getChildCount() - 1;
View lastView = mPager.getChildAt(count);
if (lastView != null) {
Texture cloud = Texture.createTexture(mBitmap,
lastView.getRight() + mBitmapWidth,
DisplayUtils.convertFromDP(mContext, 5.0f + mRandom.nextFloat() * 40.0f),
DisplayUtils.convertFromDP(mContext, mRandom.nextFloat() * 2.0f + 1.0f));
clouds.add(cloud);
cloud = null;
} else
return;
}
public void createCloud(float x) {
Texture cloud = Texture.createTexture(mBitmap, x + mBitmapWidth,
DisplayUtils.convertFromDP(mContext, 5.0f + mRandom.nextFloat() * 40.0f),
DisplayUtils.convertFromDP(mContext, mRandom.nextFloat() * 2.0f + 0.5f));
clouds.add(cloud);
cloud = null;
}
public void setViewPager(ViewPager pager) {
mPager = pager;
}
public void setMaxNum(int num) {
mMaxNum = num;
}
public void setBitmap(int resId) {
mBitmap = BitmapFactory.decodeResource(getResources(), resId);
}
public int getBitmapWidth() {
return mBitmapWidth;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mIsPlaying) {
for (Texture cloud : clouds) {
mMatrix.setTranslate(
cloud.x - mBitmapWidth - mPager.getScrollX() * 0.3f, cloud.y);
canvas.drawBitmap(mBitmap, mMatrix, null);
}
}
}
public void pause() {
if (mIsPlaying)
animator.cancel();
mIsPlaying = false;
}
public void resume() {
if (!mIsPlaying)
animator.start();
mIsPlaying = true;
}
}