展会信息港展会大全

android播放gif图片
来源:互联网   发布日期:2015-09-29 10:22:40   浏览:3519次  

导读:[文件] GifDecoder.java ~ 7KB 下载(524)001 package com.yarin.android.Examples_05_19;002 003 import android.graphics.Bitmap;004 import android.graphics.Bitmap.Config;005 006 public cla......

[文件] GifDecoder.java ~ 7KB 下载(524)

001 package com.yarin.android.Examples_05_19;

002

003 import android.graphics.Bitmap;

004 import android.graphics.Bitmap.Config;

005

006 public class GifDecoder

007 {

008 private int E0;

009 private int E1[];

010 private int E2;

011 private int E6;

012 private boolean E7;

013 private int E8[];

014 private int width;

015 private int height;

016 private int ED;

017 private boolean EE;

018 private boolean EF;

019 private int F0[];

020 private int F1;

021 private boolean F2;

022 private int F3;

023 private long F4;

024 private int F5;

025 private static final int F6[] = { 8, 8, 4, 2 };

026 private static final int F8[] = { 0, 4, 2, 1 };

027

028 int curFrame;

029 int poolsize;

030 int FA;

031 byte C2[];

032 int FB;

033 int FC;

034 int FD;

035

036

037 public GifDecoder(byte abyte0[])

038 {

039 E0 = -1;

040 E1 = new int[280];

041 E2 = -1;

042 E6 = 0;

043 E7 = false;

044 E8 = null;

045 width = 0;

046 height = 0;

047 ED = 0;

048 EE = false;

049 EF = false;

050 F0 = null;

051 F1 = 0;

052 F5 = 0;

053 curFrame = 0;

054 C2 = abyte0;

055 poolsize = C2.length;

056 FA = 0;

057 }

058

059

060 public boolean moreFrames()

061 {

062 return poolsize - FA >= 16;

063 }

064

065

066 public void nextFrame()

067 {

068 curFrame++;

069 }

070

071

072 public Bitmap decodeImage()

073 {

074 return decodeImage(curFrame);

075 }

076

077

078 public Bitmap decodeImage(int i)

079 {

080 if (i <= E0)

081 {

082 return null;

083 }

084 if (E0 < 0)

085 {

086 if (!E3())

087 {

088 return null;

089 }

090 if (!E4())

091 {

092 return null;

093 }

094 }

095 do

096 {

097 if (!E9(1))

098 {

099 return null;

100 }

101 int j = E1[0];

102 if (j == 59)

103 {

104 return null;

105 }

106 if (j == 33)

107 {

108 if (!E7())

109 {

110 return null;

111 }

112 }

113 else if (j == 44)

114 {

115 if (!E5())

116 {

117 return null;

118 }

119 Bitmap image = createImage();

120 E0++;

121 if (E0 < i)

122 {

123 image = null;

124 }

125 else

126 {

127 return image;

128 }

129 }

130 }

131 while (true);

132 }

133

134

135 public void clear()

136 {

137 C2 = null;

138 E1 = null;

139 E8 = null;

140 F0 = null;

141 }

142

143

144 private Bitmap createImage()

145 {

146 int i = width;

147 int j = height;

148 int j1 = 0;

149 int k1 = 0;

150 int ai[] = new int[4096];

151 int ai1[] = new int[4096];

152 int ai2[] = new int[8192];

153 if (!E9(1))

154 {

155 return null;

156 }

157 int k = E1[0];

158 int[] image = new int[width * height];

159 int ai3[] = E8;

160 if (EE)

161 {

162 ai3 = F0;

163 }

164 if (E2 >= 0)

165 {

166 ai3[E2] = 0xffffff;

167 }

168 int l2 = 1 << k;

169 int j3 = l2 + 1;

170 int k2 = k + 1;

171 int l3 = l2 + 2;

172 int k3 = -1;

173 int j4 = -1;

174 for (int l1 = 0; l1 < l2; l1++)

175 {

176 ai1[l1] = l1;

177 }

178

179 int j2 = 0;

180 E2();

181 j1 = 0;

182 label0: for (int i2 = 0; i2 < j; i2++)

183 {

184 int i1 = 0;

185 do

186 {

187 if (i1 >= i)

188 {

189 break;

190 }

191 if (j2 == 0)

192 {

193 int i4 = E1(k2);

194 if (i4 < 0)

195 {

196 return Bitmap.createBitmap(image, width, height, Config.RGB_565);

197 }

198 if (i4 > l3 || i4 == j3)

199 {

200 return Bitmap.createBitmap(image, width, height, Config.RGB_565);

201 }

202 if (i4 == l2)

203 {

204 k2 = k + 1;

205 l3 = l2 + 2;

206 k3 = -1;

207 continue;

208 }

209 if (k3 == -1)

210 {

211 ai2[j2++] = ai1[i4];

212 k3 = i4;

213 j4 = i4;

214 continue;

215 }

216 int i3 = i4;

217 if (i4 == l3)

218 {

219 ai2[j2++] = j4;

220 i4 = k3;

221 }

222 for (; i4 > l2; i4 = ai[i4])

223 {

224 ai2[j2++] = ai1[i4];

225 }

226

227 j4 = ai1[i4];

228 if (l3 >= 4096)

229 {

230 return Bitmap.createBitmap(image, width, height, Config.RGB_565);

231 }

232 ai2[j2++] = j4;

233 ai[l3] = k3;

234 ai1[l3] = j4;

235 if (++l3 >= 1 << k2 && l3 < 4096)

236 {

237 k2++;

238 }

239 k3 = i3;

240 }

241 int l = ai2[--j2];

242 if (l < 0)

243 {

244 return Bitmap.createBitmap(image, width, height, Config.RGB_565);

245 }

246 if (i1 == 0)

247 {

248 FC = 0;

249 FB = ai3[l];

250 FD = 0;

251 }

252 else if (FB != ai3[l])

253 {

254

255 for (int mm = FD; mm <= FD + FC; mm++)

256 {

257 image[j1 * width + mm] = FB;

258 }

259 FC = 0;

260 FB = ai3[l];

261 FD = i1;

262 if (i1 == i - 1)

263 {

264 image[j1 * width + i1] = ai3[l];

265 }

266 }

267 else

268 {

269 FC++;

270 if (i1 == i - 1)

271 {

272 for (int mm = FD; mm <= FD + FC; mm++)

273 {

274 image[j1 * width + mm] = FB;

275 }

276 }

277 }

278 i1++;

279 }

280 while (true);

281 if (EF)

282 {

283 j1 += F6[k1];

284 do

285 {

286 if (j1 < j)

287 {

288 continue label0;

289 }

290 if (++k1 > 3)

291 {

292 return Bitmap.createBitmap(image, width, height, Config.RGB_565);

293 }

294 j1 = F8[k1];

295 }

296 while (true);

297 }

298 j1++;

299 }

300

301 return Bitmap.createBitmap(image, width, height, Config.RGB_565);

302 }

303

304

305 private int E1(int i)

306 {

307 while (F5 < i)

308 {

309 if (F2)

310 {

311 return -1;

312 }

313 if (F1 == 0)

314 {

315 F1 = E8();

316 F3 = 0;

317 if (F1 <= 0)

318 {

319 F2 = true;

320 break;

321 }

322 }

323 F4 += E1[F3] << F5;

324 F3++;

325 F5 += 8;

326 F1--;

327 }

328 int j = (int) F4 & (1 << i) - 1;

329 F4 >>= i;

330 F5 -= i;

331 return j;

332 }

333

334

335 private void E2()

336 {

337 F5 = 0;

338 F1 = 0;

339 F4 = 0L;

340 F2 = false;

341 F3 = -1;

342 }

343

344

345 private boolean E3()

346 {

347 if (!E9(6))

348 {

349 return false;

350 }

351 return E1[0] == 71 && E1[1] == 73 && E1[2] == 70 && E1[3] ==56 && (E1[4] == 55 || E1[4] == 57) && E1[5] == 97;

352 }

353

354

355 private boolean E4()

356 {

357 if (!E9(7))

358 {

359 return false;

360 }

361 int i = E1[4];

362 E6 = 2 << (i & 7);

363 E7 = EB(i, 128);

364 E8 = null;

365 return !E7 || E6(E6, true);

366 }

367

368

369 private boolean E5()

370 {

371 if (!E9(9))

372 {

373 return false;

374 }

375 width = EA(E1[4], E1[5]);

376 height = EA(E1[6], E1[7]);

377 int i = E1[8];

378 EE = EB(i, 128);

379 ED = 2 << (i & 7);

380 EF = EB(i, 64);

381 F0 = null;

382 return !EE || E6(ED, false);

383 }

384

385

386 private boolean E6(int i, boolean flag)

387 {

388 int ai[] = new int[i];

389 for (int j = 0; j < i; j++)

390 {

391 if (!E9(3))

392 {

393 return false;

394 }

395 ai[j] = E1[0] << 16 | E1[1] << 8 | E1[2] | 0xff000000;

396 }

397

398 if (flag)

399 {

400 E8 = ai;

401 }

402 else

403 {

404 F0 = ai;

405 }

406 return true;

407 }

408

409

410 private boolean E7()

411 {

412 if (!E9(1))

413 {

414 return false;

415 }

416 int i = E1[0];

417 int j = -1;

418 switch (i)

419 {

420 case 249:

421 j = E8();

422 if (j < 0)

423 {

424 return true;

425 }

426 if ((E1[0] & 1) != 0)

427 {

428 E2 = E1[3];

429 }

430 else

431 {

432 E2 = -1;

433 }

434 break;

435 }

436 do

437 {

438 j = E8();

439 }

440 while (j > 0);

441 return true;

442 }

443

444

445 private int E8()

446 {

447 if (!E9(1))

448 {

449 return -1;

450 }

451 int i = E1[0];

452 if (i != 0 && !E9(i))

453 {

454 return -1;

455 }

456 else

457 {

458 return i;

459 }

460 }

461

462

463 private boolean E9(int i)

464 {

465 if (FA + i >= poolsize)

466 {

467 return false;

468 }

469 for (int j = 0; j < i; j++)

470 {

471 int k = C2[FA + j];

472 if (k < 0)

473 {

474 k += 256;

475 }

476 E1[j] = k;

477 }

478

479 FA += i;

480 return true;

481 }

482

483

484 private static final int EA(int i, int j)

485 {

486 return j << 8 | i;

487 }

488

489

490 private static final boolean EB(int i, int j)

491 {

492 return (i & j) == j;

493 }

494 }

[文件] GameView.java ~ 2KB 下载(297)

01 package com.yarin.android.Examples_05_19;

02

03 import java.io.ByteArrayOutputStream;

04 import java.io.InputStream;

05 import android.content.Context;

06 import android.graphics.Bitmap;

07 import android.graphics.Canvas;

08 import android.view.View;

09

10 public class GameView extends View implements Runnable

11 {

12 Context mContext = null;

13

14 /* 声明GifFrame对象 */

15 GifFrame mGifFrame = null;

16

17 public GameView(Context context)

18 {

19 super(context);

20

21 mContext = context;

22 /* 解析GIF动画 */

23 mGifFrame=GifFrame.CreateGifImage(fileConnect(this.getResources().openRawResource(R.drawable.gif1)));

24 /* 开启线程 */

25 new Thread(this).start();

26 }

27

28 public void onDraw(Canvas canvas)

29 {

30 super.onDraw(canvas);

31 /* 下一帧 */

32 mGifFrame.nextFrame();

33 /* 得到当前帧的图片 */

34 Bitmap b=mGifFrame.getImage();

35

36 /* 绘制当前帧的图片 */

37 if(b!=null)

38 canvas.drawBitmap(b,10,10,null);

39 }

40

41

42 /**

43 * 线程处理

44 */

45 public void run()

46 {

47 while (!Thread.currentThread().isInterrupted())

48 {

49 try

50 {

51 Thread.sleep(100);

52 }

53 catch (InterruptedException e)

54 {

55 Thread.currentThread().interrupt();

56 }

57 //使用postInvalidate可以直接在线程中更新界面

58 postInvalidate();

59 }

60 }

61

62 /* 读取文件 */

63 public byte[] fileConnect(InputStream is)

64 {

65 try

66 {

67 ByteArrayOutputStream baos = new ByteArrayOutputStream();

68 int ch = 0;

69 while( (ch = is.read()) != -1)

70 {

71 baos.write(ch);

72 }

73 byte[] datas = baos.toByteArray();

74 baos.close();

75 baos = null;

76 is.close();

77 is = null;

78 return datas;

79 }

80 catch(Exception e)

81 {

82 return null;

83 }

84 }

85 }

[文件] GifFrame.java ~ 2KB 下载(312)

01 package com.yarin.android.Examples_05_19;

02

03 import java.util.Vector;

04 import android.graphics.Bitmap;

05

06 public class GifFrame

07 {

08 /** 保存gif中所有帧的向量 */

09 private Vector frames;

10

11 /** 当前播放的帧的索引 */

12 private int index;

13

14 public GifFrame()

15 {

16 frames = new Vector(1);

17 index = 0;

18 }

19

20 /** 添加一帧 */

21 public void addImage(Bitmap image)

22 {

23 frames.addElement(image);

24 }

25

26 /** 返回帧数 */

27 public int size()

28 {

29 return frames.size();

30 }

31

32 /** 得到当前帧的图片 */

33 public Bitmap getImage()

34 {

35 if (size() == 0)

36 {

37 return null;

38 }

39 else

40 {

41 return (Bitmap) frames.elementAt(index);

42 }

43 }

44

45 /** 下一帧 */

46 public void nextFrame()

47 {

48 if (index + 1 < size())

49 {

50 index++;

51 }

52 else

53 {

54 index = 0;

55 }

56 }

57

58 /** 创建GifFrame */

59 public static GifFrame CreateGifImage(byte abyte0[])

60 {

61 try

62 {

63 GifFrame GF = new GifFrame();

64 Bitmap image = null;

65 GifDecoder gifdecoder = new GifDecoder(abyte0);

66 for (; gifdecoder.moreFrames(); gifdecoder.nextFrame())

67 {

68 try

69 {

70 image = gifdecoder.decodeImage();

71 if (GF != null && image != null)

72 {

73 GF.addImage(image);

74 }

75 continue;

76 }

77 catch (Exception e)

78 {

79 e.printStackTrace();

80 }

81 break;

82 }

83 gifdecoder.clear();

84 gifdecoder = null;

85 return GF;

86 }

87 catch (Exception e)

88 {

89 e.printStackTrace();

90 return null;

91 }

92 }

93 }

赞助本站

人工智能实验室

相关热词: android播放gif图片

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港