引言 在Android中 默认情况下是不允许播放GIF图片动画的 这时需要自己实现来播放GIF动画 在网上搜索了一个可用的实现 贴出来与大家共享 功能实现 ( ) 主Activity实现 package com focus fishme; import android app Activity; import android os Bundle; pub
在Android中
(
package com
import android
import android
publicclass GIFPlayerActivity extends Activity {
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super
GameView gameView = new GameView(this);
setContentView(gameView);
}
}
(
package com
import java
import java
import java
import ntent
import android
import android
import android
import android
publicclass GameView extends View implements Runnable {
private GIFFrameManager mGIFFrameManager = null;
private Paint mPaint = null;
public GameView(Context context) {
super(context);
mGIFFrameManager = GIFFrameManager
new Thread(this)
}
protectedvoid onDraw(Canvas canvas) {
super
mPaint = new Paint();
mGIFFrameManager
Bitmap bitmap = mGIFFrameManager
if (bitmap != null) {
canvas
}
}
publicbyte[] fileConnect(InputStream in) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int ch =
try {
while ((ch = in
out
}
byte[] b = out
out
out = null;
in
in = null;
return b;
} catch (IOException e) {
e
returnnull;
}
}
publicvoid run() {
while (!Thread
try {
Thread
this
} catch (Exception ex) {
ex
Thread
}
}
}
}
(
package com
import java
import android
publicclass GIFFrameManager {
private Vector<Bitmap> frames;
privateint index;
public GIFFrameManager() {
frames = new Vector<Bitmap>(
index =
}
publicvoid addImage(Bitmap image) {
frames
}
publicint size() {
return frames
}
public Bitmap getImage() {
if (size() ==
returnnull;
} else {
return (Bitmap) frames
}
}
publicvoid nextFrame() {
if (index +
index++;
} else {
index =
}
}
publicstatic GIFFrameManager CreateGifImage(byte bytes[]) {
try {
GIFFrameManager GF = new GIFFrameManager();
Bitmap image = null;
GIFEncoder gifdecoder = new GIFEncoder(bytes);
for (; gifdecoder
try {
image = gifdecoder
if (GF != null && image != null) {
GF
}
continue;
} catch (Exception e) {
e
}
break;
}
gifdecoder
gifdecoder = null;
return GF;
} catch (Exception e) {
e
returnnull;
}
}
}
(
package com
import android
import android
publicclass GIFEncoder {
privateint E
privateint E
privateint E
privateint E
privateboolean E
privateint E
privateint width;
privateint height;
privateint ED;
privateboolean EE;
privateboolean EF;
privateint F
privateint F
privateboolean F
privateint F
privatelong F
privateint F
privatestaticfinalint F
privatestaticfinalint F
int curFrame;
int poolsize;
int FA;
byte C
int FB;
int FC;
int FD;
public GIFEncoder(byte abyte
E
E
E
E
E
E
width =
height =
ED =
EE = false;
EF = false;
F
F
F
curFrame =
C
poolsize = C
FA =
}
publicboolean moreFrames() {
return poolsize
}
publicvoid nextFrame() {
curFrame++;
}
public Bitmap decodeImage() {
return decodeImage(curFrame);
}
public Bitmap decodeImage(int i) {
if (i <= E
returnnull;
}
if (E
if (!E
returnnull;
}
if (!E
returnnull;
}
}
do {
if (!E
returnnull;
}
int j = E
if (j ==
returnnull;
}
if (j ==
if (!E
returnnull;
}
} elseif (j ==
if (!E
returnnull;
}
Bitmap image = createImage();
E
if (E
image = null;
} else {
return image;
}
}
} while (true);
}
publicvoid clear() {
C
E