Play GIF

View for images or gif, support square mode

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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Movie;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CustomGifView extends AppCompatImageView {

private int movieMovieResourceId = 0;
private Movie movie = null;
RoundedBitmapDrawable roundedBitmapDrawable = null;
private Long movieStart = 0l;
private int currentAnimationTime = 0;

private Float movieLeft = 0F;
private Float movieTop = 0F;
private Float movieScale = 0F;

private int movieMeasuredMovieWidth = 0;
private int movieMeasuredMovieHeight = 0;
private int cornersRadius = 0;

private static int DEFAULT_MOVIE_VIEW_DURATION = 1000;

volatile Boolean isPaused = false;
private Boolean isVisible = true;
private Boolean empty = true;
private boolean isSquare = false;

private Runnable setDataRunnable = null;

public CustomGifView(@NotNull Context context) {
this(context, null, 0);
}

public CustomGifView(@NotNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public CustomGifView(@NotNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getViewTreeObserver().addOnGlobalLayoutListener(() -> {
int maxHeight = getContext().getResources().getDimensionPixelSize(R.dimen.d_300dp);
int minHeight = getContext().getResources().getDimensionPixelSize(R.dimen.d_72dp);

if(movieMeasuredMovieWidth == 0) {
movieMeasuredMovieWidth = getWidth();
}
if(movieMeasuredMovieHeight == 0) {
movieMeasuredMovieHeight = getHeight();
}

movieMeasuredMovieHeight = Math.min(movieMeasuredMovieHeight, maxHeight);
movieMeasuredMovieHeight = Math.max(movieMeasuredMovieHeight, minHeight);
if(isSquare){
movieMeasuredMovieHeight = movieMeasuredMovieWidth;
}

if(setDataRunnable !=null){
post(setDataRunnable);
}
});
}

@SuppressLint("NewApi")
private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);

TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.CustomGifView, defStyle, R.style.Widget_CustomGifView);

//-1 is default value
movieMovieResourceId = array.getResourceId(R.styleable.CustomGifView_gif, -1);
isPaused = array.getBoolean(R.styleable.CustomGifView_paused, false);

array.recycle();

if (movieMovieResourceId != -1) {
movie = Movie.decodeStream(getResources().openRawResource(movieMovieResourceId));
}
}


public void play() {
if (this.isPaused) {
this.isPaused = false;
/**
* Calculate new movie start time, so that it resumes from the same
* frame.
*/
movieStart = android.os.SystemClock.uptimeMillis() - currentAnimationTime;
invalidate();
}
}

public void pause() {
if (!this.isPaused) {
this.isPaused = true;
invalidate();
}
}

public void setSquare(boolean square) {
isSquare = square;
}

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int maxHeight = getContext().getResources().getDimensionPixelSize(R.dimen.d_300dp);
int minHeight = getContext().getResources().getDimensionPixelSize(R.dimen.d_72dp);
if (movie == null && roundedBitmapDrawable == null) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
} else {
int movieWidth = 0;
int movieHeight = 0;
if (movie != null) {
movieWidth = movie.width();
movieHeight = movie.height();
}
if(roundedBitmapDrawable != null) {
movieWidth = roundedBitmapDrawable.getIntrinsicWidth();
movieHeight = roundedBitmapDrawable.getIntrinsicHeight();
}

if(isSquare){
movieHeight = movieWidth;
}

/*
* Calculate horizontal scaling
*/
float scaleH = 1f;
int measureModeWidth = View.MeasureSpec.getMode(widthMeasureSpec);

if (measureModeWidth != View.MeasureSpec.UNSPECIFIED) {
int maximumWidth = View.MeasureSpec.getSize(widthMeasureSpec);
scaleH = (float) movieWidth / (float) maximumWidth;
}
/*
* calculate vertical scaling
*/
float scaleW = 1f;
int measureModeHeight = View.MeasureSpec.getMode(heightMeasureSpec);

if (measureModeHeight != View.MeasureSpec.UNSPECIFIED) {
int maximumHeight = View.MeasureSpec.getSize(heightMeasureSpec);
scaleW = (float) movieHeight / (float) maximumHeight;
}
/*
* calculate overall scale
*/
if(isSquare && roundedBitmapDrawable.getIntrinsicHeight() <= roundedBitmapDrawable.getIntrinsicWidth()){
movieScale = 1f / Math.max(scaleH, scaleW);
}
else {
movieScale = 1f / Math.min(scaleH, scaleW);
}
movieMeasuredMovieWidth = (int)(movieWidth * movieScale);
movieMeasuredMovieHeight = (int)(movieHeight * movieScale);

movieMeasuredMovieHeight = Math.min(movieMeasuredMovieHeight, maxHeight);
movieMeasuredMovieHeight = Math.max(movieMeasuredMovieHeight, minHeight);
if(isSquare){
movieMeasuredMovieHeight = movieMeasuredMovieWidth;
}

setMeasuredDimension(movieMeasuredMovieWidth, movieMeasuredMovieHeight);
}
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
/*
* Calculate movieLeft / movieTop for drawing in center
*/
movieLeft = (getWidth() - movieMeasuredMovieWidth) / 2f;
movieTop = (getHeight() - movieMeasuredMovieHeight) / 2f;
isVisible = getVisibility() == View.VISIBLE;
}

@Override
public void onDraw(Canvas canvas) {
if(empty){
canvas.drawColor(Color.TRANSPARENT);
} else {
if (movie != null) {
if (!isPaused) {
updateAnimationTime();
drawMovieFrame(canvas);
invalidateView();
} else {
drawMovieFrame(canvas);
}
}
else if(roundedBitmapDrawable != null){
roundedBitmapDrawable.setBounds(0, 0, movieMeasuredMovieWidth, movieMeasuredMovieHeight);
roundedBitmapDrawable.draw(canvas);
}
}
}

/**
* Draw current GIF frame
*/
private void drawMovieFrame(Canvas canvas) {
movie.setTime(currentAnimationTime);
canvas.save();
canvas.scale(movieScale, movieScale);
movie.draw(canvas, movieLeft / movieScale, movieTop / movieScale);
canvas.restore();

if(cornersRadius > 0){
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setStrokeWidth(cornersRadius*2);

canvas.drawRoundRect(
new RectF(0-cornersRadius, 0-cornersRadius, movieMeasuredMovieWidth+cornersRadius, movieMeasuredMovieHeight+cornersRadius),
2*cornersRadius,
2*cornersRadius,
paint);
}
}

/**
* Invalidates view only if it is isVisible.
* <br></br>
* [.postInvalidateOnAnimation] is used for Jelly Bean and higher.
*/
@SuppressLint("NewApi")
private void invalidateView() {
if (isVisible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
postInvalidateOnAnimation();
} else {
invalidate();
}
}
}

/**
* Calculate current animation time
*/
private void updateAnimationTime() {
long now = android.os.SystemClock.uptimeMillis();

if (movieStart == 0L) {
movieStart = now;
}

int duration = movie.duration();

if (duration == 0) {
duration = DEFAULT_MOVIE_VIEW_DURATION;
}

currentAnimationTime = (int)((now - movieStart) % duration);
}

@SuppressLint("NewApi")
public void onScreenStateChanged(int screenState) {
super.onScreenStateChanged(screenState);
isVisible = screenState == View.SCREEN_STATE_ON;
invalidateView();
}

@SuppressLint("NewApi")
public void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
isVisible = visibility == View.VISIBLE;
invalidateView();
}

public void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
isVisible = visibility == View.VISIBLE;
invalidateView();
}

public void setGif(byte[] data){
if (setDataRunnable != null) {
return;
}
empty = true;

movie = Movie.decodeByteArray(data, 0, data.length);
if(movie == null){
int maxHeight = getContext().getResources().getDimensionPixelSize(R.dimen.d_300dp);
int maxWidth = maxHeight * 2;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);

final BitmapFactory.Options options2 = new BitmapFactory.Options();
options2.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options2);

roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCornerRadius(cornersRadius);
}

movieMeasuredMovieHeight = 0;
movieMeasuredMovieWidth = 0;
setDataRunnable = this::resizeData;

requestLayout();
}

private void resizeData(){
setDataRunnable = null;
if(movie == null && roundedBitmapDrawable != null && roundedBitmapDrawable.getBitmap() != null && movieMeasuredMovieWidth > 0 && movieMeasuredMovieHeight > 0){
roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), scaleBitmap(roundedBitmapDrawable.getBitmap(), movieMeasuredMovieWidth, movieMeasuredMovieHeight));
roundedBitmapDrawable.setCornerRadius(cornersRadius);
}
empty = false;
invalidate();
}

public boolean isPlaying(){
return !this.isPaused;
}

public void setCornersRadius(int cornersRadius) {
this.cornersRadius = cornersRadius;
}

public void clear(){
empty = true;
movieMeasuredMovieHeight = 0;
movieMeasuredMovieWidth = 0;
requestLayout();
}

private int calculateInSampleSize(
BitmapFactory.Options options,
int reqWidth,
int reqHeight
) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
while ((height / inSampleSize) >= reqHeight
&& (width / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}

private Bitmap scaleBitmap(Bitmap originalImage, int wantedWidth, int wantedHeight)
{
Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Matrix m = new Matrix();

float scaleW = (float)wantedWidth / originalImage.getWidth();
float scaleH = (float)wantedHeight / originalImage.getHeight();

float scale = Math.max(scaleW, scaleH);

m.setScale(scale, scale);
canvas.drawBitmap(originalImage, m, new Paint());
return output;
}
}

1
2
3
4
<declare-styleable name="CustomGifView">
<attr name="gif" format="reference" />
<attr name="paused" format="boolean" />
</declare-styleable>