Android加载Gif动画实现代码

Android加载Gif动画如何实现?相信大家都很好奇,本文就为大家揭晓,内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:orientation = "vertical" >
< com.example.gifdemo.GifView
android:id = "@+id/gif1"
android:layout_width = "100dp"
android:layout_height = "100dp"
android:layout_gravity = "center_horizontal"
android:enabled = "false" />
</ LinearLayout >
?
1
2
3
4
< declare-styleable name = "GifView" >
< attr name = "gif" format = "reference" />
< attr name = "paused" format = "boolean" />
</ declare-styleable >

主界面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.example.gifdemo;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
private GifView gif1;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gif1 = (GifView) findViewById(R.id.gif1);
// 设置背景gif图片资源
gif1.setMovieResource(R.raw.red);
}
}

自定义view

?
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
package com.example.gifdemo;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
public class GifView extends View {
/**
* 默认为1秒
*/
private static final int DEFAULT_MOVIE_DURATION = 1000 ;
private int mMovieResourceId;
private Movie mMovie;
private long mMovieStart;
private int mCurrentAnimationTime = 0 ;
private float mLeft;
private float mTop;
private float mScale;
private int mMeasuredMovieWidth;
private int mMeasuredMovieHeight;
private boolean mVisible = true ;
private volatile boolean mPaused = false ;
/**
* 构造函数
*/
public GifView(Context context) {
this (context, null );
}
public GifView(Context context, AttributeSet attrs) {
this (context, attrs, 0 );
}
public GifView(Context context, AttributeSet attrs, int defStyle) {
super (context, attrs, defStyle);
setViewAttributes(context, attrs, defStyle);
setBackgroundColor(Color.parseColor( "#FFB6C1" ));
}
@SuppressLint ( "NewApi" )
private void setViewAttributes(Context context, AttributeSet attrs,
int defStyle) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null );
}
// 从描述文件中读出gif的值,创建出Movie实例
final TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.GifView);
mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, - 1 );
mPaused = array.getBoolean(R.styleable.GifView_paused, false );
array.recycle();
if (mMovieResourceId != - 1 ) {
mMovie = Movie.decodeStream(getResources().openRawResource(
mMovieResourceId));
}
}
/**
* 设置gif图资源
*/
public void setMovieResource( int movieResId) {
this .mMovieResourceId = movieResId;
mMovie = Movie.decodeStream(getResources().openRawResource(
mMovieResourceId));
requestLayout();
}
@Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
if (mMovie != null ) {
//gif动画的宽度、高度
int movieWidth = mMovie.width();
int movieHeight = mMovie.height();
//控件的宽度
int maximumWidth = MeasureSpec.getSize(widthMeasureSpec);
//gif图片宽/控件宽
float scaleW = ( float ) movieWidth / ( float ) maximumWidth;
mScale = 1f / scaleW;
mMeasuredMovieWidth = maximumWidth;
mMeasuredMovieHeight = ( int ) (movieHeight * mScale);
setMeasuredDimension(mMeasuredMovieWidth, mMeasuredMovieHeight);
} else {
setMeasuredDimension(getSuggestedMinimumWidth(),
getSuggestedMinimumHeight());
}
}
// @Override
// protected void onLayout(boolean changed, int l, int t, int r, int b) {
// super.onLayout(changed, l, t, r, b);
// mLeft = (getWidth() - mMeasuredMovieWidth) / 2f;
// mTop = (getHeight() - mMeasuredMovieHeight) / 2f;
// mVisible = getVisibility() == View.VISIBLE;
// }
@Override
protected void onDraw(Canvas canvas) {
if (mMovie != null ) {
if (!mPaused) {
updateAnimationTime();
drawMovieFrame(canvas);
invalidateView();
} else {
drawMovieFrame(canvas);
}
}
}
private void updateAnimationTime() {
long now = android.os.SystemClock.uptimeMillis();
// 如果第一帧,记录起始时间
if (mMovieStart == 0 ) {
mMovieStart = now;
}
// 取出动画的时长
int dur = mMovie.duration();
if (dur == 0 ) {
dur = DEFAULT_MOVIE_DURATION;
}
// 算出需要显示第几帧
mCurrentAnimationTime = ( int ) ((now - mMovieStart) % dur);
}
private void drawMovieFrame(Canvas canvas) {
// 设置要显示的帧,绘制即可
mMovie.setTime(mCurrentAnimationTime);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.scale(mScale, mScale);
mMovie.draw(canvas, mLeft / mScale, mTop / mScale);
canvas.restore();
}
@SuppressLint ( "NewApi" )
private void invalidateView() {
if (mVisible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
postInvalidateOnAnimation();
} else {
invalidate();
}
}
}
// --------------------以下方法未调用------------------------------------/
public void setMovie(Movie movie) {
this .mMovie = movie;
requestLayout();
}
public Movie getMovie() {
return mMovie;
}
public void setMovieTime( int time) {
mCurrentAnimationTime = time;
invalidate();
}
public void setPaused( boolean paused) {
this .mPaused = paused;
if (!paused) {
mMovieStart = android.os.SystemClock.uptimeMillis()
- mCurrentAnimationTime;
}
invalidate();
}
public boolean isPaused() {
return this .mPaused;
}
@SuppressLint ( "NewApi" )
@Override
public void onScreenStateChanged( int screenState) {
super .onScreenStateChanged(screenState);
mVisible = screenState == SCREEN_STATE_ON;
invalidateView();
}
@SuppressLint ( "NewApi" )
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super .onVisibilityChanged(changedView, visibility);
mVisible = visibility == View.VISIBLE;
invalidateView();
}
@Override
protected void onWindowVisibilityChanged( int visibility) {
super .onWindowVisibilityChanged(visibility);
mVisible = visibility == View.VISIBLE;
invalidateView();
}
// --------------------------------------------------------/
}

源码下载:http://xiazai.jb51.net/201610/yuanma/AndroidGifDemo(jb51.net).rar


本文由 微信妈妈(公众号买卖ontaobao.cn) 编辑, 转载于 http://www.jb51.net/article/96149.htm

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值