仿照apiDemos的例子,com.example.android.apis.graphics.BitmapDecode,直接修改来用:
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
|
public
class
PlayGifActivity
extends
Activity
{
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(
new
SampleView(
this
,R.drawable.animated_gif));
}
private
static
class
SampleView
extends
View
{
private
Movie mMovie;
private
long
mMovieStart;
// Set to false to use decodeByteArray
private
static
final
boolean
DECODE_STREAM =
true
;
private
static
byte
[] streamToBytes(InputStream is)
{
ByteArrayOutputStream os =
new
ByteArrayOutputStream(
1024
);
byte
[] buffer =
new
byte
[
1024
];
int
len;
try
{
while
((len = is.read(buffer)) >=
0
)
{
os.write(buffer,
0
, len);
}
}
catch
(java.io.IOException e)
{
}
return
os.toByteArray();
}
public
SampleView(Context context,
int
resID)
{
super
(context);
setFocusable(
true
);
java.io.InputStream is;
is = context.getResources().openRawResource(resID);
if
(DECODE_STREAM)
{
mMovie = Movie.decodeStream(is);
}
else
{
byte
[] array = streamToBytes(is);
mMovie = Movie.decodeByteArray(array,
0
, array.length);
}
}
@Override
protected
void
onDraw(Canvas canvas)
{
canvas.drawColor(
0xFFCCCCCC
);
long
now = android.os.SystemClock.uptimeMillis();
if
(mMovieStart ==
0
)
{
// first time
mMovieStart = now;
}
if
(mMovie !=
null
)
{
int
dur = mMovie.duration();
if
(dur ==
0
)
{
dur =
1000
;
}
int
relTime = (
int
) ((now - mMovieStart) % dur);
mMovie.setTime(relTime);
mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height());
invalidate();
//重绘方法,调用下一帧
}
}
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1226163,如需转载请自行联系原作者