/
SpriteSheetDrawer.java
286 lines (244 loc) · 8.74 KB
/
SpriteSheetDrawer.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package com.daasuu.library.drawer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import com.daasuu.library.callback.AnimCallBack;
import com.daasuu.library.constant.Constant;
import com.daasuu.library.spritesheet.SpriteSheet;
import com.daasuu.library.util.Util;
import java.util.List;
/**
* Class for drawing SpriteSheet on canvas.
*/
public class SpriteSheetDrawer extends BaseDrawer {
/**
* Bitmap to be drawn in FPSTextureView or FPSSurfaceView.
*/
private Bitmap mBitmap;
/**
* If true, Draw a Bitmap in device-specific pixel density.
*/
private boolean mDpSize = false;
/**
* Width of Bitmap in device-specific pixel density.
*/
private float mBitmapDpWidth;
/**
* Height of Bitmap in device-specific pixel density.
*/
private float mBitmapDpHeight;
/**
* The SpriteSheet instance to play back. This includes frame dimensions, and frame data.
*/
private SpriteSheet mSpriteSheet;
/**
* The subset of the SpriteSheet bitmap to be drawn
*/
private final Rect mBitmapRect;
/**
* The rectangle that the SpriteSheet bitmap will be scaled/translated to fit into
*/
private final Rect mBounds;
/**
* member variables needed to skip a tick in mFrequency
*/
private int mDrawingNum = Constant.DEFAULT_DRAWING_NUM;
/**
* By default Sprite instances advance one frame per tick.
* Specifying a frequency for the Sprite (or its related SpriteSheet) will cause it to
* advance based on elapsed time between ticks as appropriate to maintain the target frequency.
* For example, if a Sprite with a frequency of 10 is placed on a Stage being updated at 40fps,
* then the Sprite will advance roughly one frame every 4 ticks. This will not be exact,
* because the time between each tick will vary slightly between frames.
* This feature is dependent on the tick event object being passed into update.
*/
private int mFrequency = Constant.DEFAULT_FREQUENCY;
/**
* Constructor
*
* @param bitmap Bitmap to be drawn in FPSTextureView or FPSSurfaceView.
* @param frameWidth The number of width of each frame
* @param frameHeight The number of height of each frame
* @param frameNum the total number of frames in the specified animation
*/
public SpriteSheetDrawer(@NonNull Bitmap bitmap, float frameWidth, float frameHeight, int frameNum) {
this(bitmap, new SpriteSheet(frameWidth, frameHeight, frameNum, (int) (bitmap.getWidth() / frameWidth)));
}
/**
* Constructor
*
* @param bitmap Bitmap to be drawn in FPSTextureView or FPSSurfaceView.
* @param frameWidth The number of width of each frame
* @param frameHeight The number of height of each frame
* @param frameNum the total number of frames in the specified animation
* @param frameNumPerLine The number of which frame, there is about line 1 of side
*/
public SpriteSheetDrawer(@NonNull Bitmap bitmap, float frameWidth, float frameHeight, int frameNum, int frameNumPerLine) {
this(bitmap, new SpriteSheet(frameWidth, frameHeight, frameNum, frameNumPerLine));
}
/**
* Constructor
*
* @param bitmap Bitmap to be drawn in FPSTextureView or FPSSurfaceView.
* @param spriteSheet The SpriteSheet instance to play back. This includes frame dimensions, and frame data.
*/
public SpriteSheetDrawer(@NonNull Bitmap bitmap, @NonNull SpriteSheet spriteSheet) {
super(new Paint());
this.mBitmap = bitmap;
this.mSpriteSheet = spriteSheet;
mBitmapRect = new Rect();
mBounds = new Rect();
}
/**
* Draw a Bitmap in device-specific pixel density.
*
* @param context Activity or view context
* @return this
*/
public SpriteSheetDrawer dpSize(@NonNull Context context) {
mDpSize = true;
mBitmapDpWidth = Util.convertPixelsToDp(mSpriteSheet.frameWidth, context);
mBitmapDpHeight = Util.convertPixelsToDp(mSpriteSheet.frameHeight, context);
return this;
}
/**
* If true, the Sprite Animation will loop when it reaches the last frame.
*
* @param loop If true, the Sprite Animation will loop when it reaches the last frame.
* @return this
*/
public SpriteSheetDrawer spriteLoop(boolean loop) {
mSpriteSheet.spriteLoop = loop;
return this;
}
/**
* Set Frequency
*
* @param frequency The number to be updated in every times of tick
* @return this
*/
public SpriteSheetDrawer frequency(int frequency) {
mFrequency = frequency;
return this;
}
/**
* It will animation play in the frame number order of the value of this array.
*
* @param list Integer List of Frame num.
* @return this
*/
public SpriteSheetDrawer customFrameList(List<Integer> list) {
mSpriteSheet.customFrameList = list;
return this;
}
/**
* If loopNum is 3, the Sprite Animation will loop 3 times.
* If loopNum is greater than or equal to 0 , spriteLoop parameter is ignored .
*
* @param loopNum Number of SpriteSheet Animation loop
* @return this
*/
public SpriteSheetDrawer spriteLoopNum(int loopNum) {
mSpriteSheet.spriteLoopNum = loopNum;
return this;
}
/**
* Set function of dispatched when a spriteSheet animation reaches its ends.
*
* @param callBack Dispatched when a spriteSheet animation reaches its ends.
* @return this
*/
public SpriteSheetDrawer spriteAnimationEndCallBack(AnimCallBack callBack) {
mSpriteSheet.setSpriteSheetFinishCallback(callBack);
return this;
}
/**
* The left offset and y offset for this display object's registration point. For example, to make a 100x100px Bitmap scale
* around its center, you would set regX and regY to 50.
*
* @param regX The horizontal rotate registration point in pixels
* @param regY The vertical rotate registration point in pixels
* @return this
*/
public SpriteSheetDrawer scaleRegistration(float regX, float regY) {
setScaleRegistration(regX, regY);
return this;
}
/**
* The left offset and y offset for this display object's registration point. For example, to make a 100x100px Bitmap rotate
* around its center, you would set regX and regY to 50.
*
* @param regX The horizontal rotate registration point in pixels
* @param regY The vertical rotate registration point in pixels
* @return this
*/
public SpriteSheetDrawer rotateRegistration(float regX, float regY) {
setRotateRegistration(regX, regY);
return this;
}
/**
* indicates whether to start the SpriteAnimation paused.
*
* @param pause If true, SpriteAnimation pause.
*/
public void spritePause(boolean pause) {
mSpriteSheet.setSpritePause(pause);
}
/**
* Getter SpriteAnimation pause state
*
* @return spritePause
*/
public boolean isSpritePause() {
return mSpriteSheet.isSpritePause();
}
@Override
protected void draw(Canvas canvas, float x, float y) {
if (mBitmap == null) return;
if (mBitmap.isRecycled()) {
mBitmap = null;
return;
}
updateSpriteFrame();
mBitmapRect.set((int) (mSpriteSheet.dx), (int) (mSpriteSheet.dy), (int) (mSpriteSheet.dx + mSpriteSheet.frameWidth), (int) (mSpriteSheet.dy + mSpriteSheet.frameHeight));
if (mDpSize) {
mBounds.set(
(int) x,
(int) y,
(int) (x + mBitmapDpWidth),
(int) (y + mBitmapDpHeight)
);
canvas.drawBitmap(mBitmap, mBitmapRect, mBounds, mPaint);
} else {
mBounds.set(
(int) x,
(int) y,
(int) (x + mSpriteSheet.frameWidth),
(int) (y + mSpriteSheet.frameHeight)
);
canvas.drawBitmap(mBitmap, mBitmapRect, mBounds, mPaint);
}
}
/**
* Move frame of SpriteSheet
*/
private synchronized void updateSpriteFrame() {
if (mDrawingNum != mFrequency) {
mDrawingNum++;
return;
}
mDrawingNum = Constant.DEFAULT_DRAWING_NUM;
mSpriteSheet.updateFrame();
}
@Override
public float getWidth() {
return mSpriteSheet.frameWidth;
}
@Override
public float getHeight() {
return mSpriteSheet.frameHeight;
}
}