fresco gif 時間

1、使用的是Fresco的三方庫
官網文檔:https://www.fresco-cn.org/docs/progress-bars.html
2、支持播放GIF
這裏只是介紹支持播放GIF的功能,因此步驟也都是GIF的步驟
1.引入Fresco
爲了支持獲取GIF播放一遍的時長,就要引入0.13.0之後的版本,可是0.14.0改變了android studio的分包機制,可能會致使編譯問題,因此仍是選擇0.13.0版本。html

dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.facebook.fresco:fresco:0.13.0' compile 'com.facebook.fresco:animated-gif:0.13.0' }

我這裏實現了在整個屏幕上添加一個GIF view
2.獲取root viewandroid

private static ViewGroup getRootGroup(Activity activity) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { return (ViewGroup) activity.findViewById(android.R.id.content); } else { return (ViewGroup) activity.getWindow().getDecorView().getRootView(); } }

上面獲取rootview
3.new一個SimpleDraweeView(com.facebook.drawee.view.SimpleDraweeView)ide

SimpleDraweeView gifView = (SimpleDraweeView) activity.findViewById(R.id.gif_tip_view); if (gifView == null) { gifView = new SimpleDraweeView(activity); gifView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { //觸摸以後的事件不日後傳遞 return true; } }); gifView.setPadding(0, HtscSystemUtil.getStatusBarHeight(activity), 0, 0); // GIF圖片填充XY軸 gifView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY); gifView.setId(R.id.gif_tip_view); }

4.下面就是關鍵代碼了
不少GIF自己都是能夠循環播放的,可是我這裏想實現GIF播放一遍就中止,而後顯示一個本身想要的界面。
從官方文檔看到以下信息:oop

手動控制動畫圖播放post

也許你但願在代碼中直接控制動畫的播放。這種狀況下,你須要監聽圖片是否加載完畢,而後才能控制動畫的播放:動畫

ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() { @Override public void onFinalImageSet( String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) { if (anim != null) { // 其餘控制邏輯 anim.start(); } } }; Uri uri; DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(uri) .setControllerListener(controllerListener) // 其餘設置(若是有的話) .build(); mSimpleDraweeView.setController(controller);

另外,controller提供對Animatable 的訪問。ui

若是有可用動畫的話,可對動畫進行靈活的控制:spa

Animatable animatable = mSimpleDraweeView.getController().getAnimatable(); if (animatable != null) { animatable.start(); // later animatable.stop(); }

因此個人思路以下:
獲取GIF播放一遍的時長,播放一遍後,讓GIF的animatable中止,並顯示想要顯示的view。如下是個人代碼.net

ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() { @Override public void onFinalImageSet(String id, ImageInfo imageInfo, final Animatable animatable) { if (animatable != null) { int duration = 0; try { Field field = AbstractAnimatedDrawable.class.getDeclaredField("mTotalLoops"); field.setAccessible(true); field.set(animatable, 1); } catch (Exception e) { e.printStackTrace(); } animatable.start(); if (animatable instanceof AbstractAnimatedDrawable) { // 只有fresco 0.13.0+纔有getDuration()的方法 duration = ((AbstractAnimatedDrawable) animatable).getDuration(); } if (duration > 0) { finalImageView.postDelayed(new Runnable() { @Override public void run() { if (animatable.isRunning()) { animatable.stop(); rootGroup.removeView(finalImageView); ViewGroup parent; View gifEndView = getGifEndView(type, rootGroup, finalImageView, activity); if ((parent = (ViewGroup) gifEndView.getParent()) != null) { parent.removeView(gifEndView); } rootGroup.addView(gifEndView); } } }, duration); } } } }; DraweeController draweeController = Fresco.newDraweeControllerBuilder() .setUri(Uri.parse(getGifImgRes(type)))//路徑 .setAutoPlayAnimations(false) .setOldController(gifView.getController()) .setControllerListener(controllerListener) .build(); gifView.setController(draweeController);

兩點小說明:
(1)GIF只播放一遍(反射)3d

try { Field field = AbstractAnimatedDrawable.class.getDeclaredField("mTotalLoops"); field.setAccessible(true); field.set(animatable, 1); } catch (Exception e) { e.printStackTrace(); }

(2)獲取播放一遍的時長

 if (animatable instanceof AbstractAnimatedDrawable) { // 只有fresco 0.13.0+纔有getDuration()的方法 duration = ((AbstractAnimatedDrawable) animatable).getDuration(); }

(3)getGifImgRes(type)接口
就是獲取資源名的string,好比"asset://com.xxx(包名)/mytestgif.gif"
還支持不少其餘的類型
(4)getGifEndView(type, rootGroup, finalImageView, activity)接口
這是播放結束要顯示的view,本身隨便定義就好了
5.用root view把SimpleDraweeView添加進去

ViewGroup parent; if ((parent = (ViewGroup) gifView.getParent()) != null) { parent.removeView(gifView); } rootGroup.addView(gifView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

6.最後一個問題
有沒有想法,想要GIF播放過程當中中止,好比有個跳過按鈕,或者按back鍵能夠跳過?
(1)添加一個跳過按鈕

// 添加一個跳過的按鈕 TextView skipButton = (TextView) activity.findViewById(R.id.gif_tip_skip_bt); if (skipButton == null) { skipButton = new TextView(activity); skipButton.setBackgroundResource(R.drawable.tip_view_skip_bg); skipButton.setText("跳過"); skipButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10); skipButton.setTextColor(activity.getResources().getColor(R.color.white)); skipButton.setGravity(Gravity.CENTER); final TextView finalSkipButton = skipButton; skipButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { animatable.stop(); rootGroup.removeView(finalSkipButton); rootGroup.removeView(finalImageView); } }); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.gravity = Gravity.TOP | Gravity.RIGHT; params.topMargin = HtscSystemUtil.getStatusBarHeight(activity) + HtscSystemUtil.convertDpToPixel(25); params.rightMargin = HtscSystemUtil.convertDpToPixel(10); params.width = HtscSystemUtil.convertDpToPixel(35); params.height = HtscSystemUtil.convertDpToPixel(20); skipButton.setLayoutParams(params); skipButton.setId(R.id.gif_tip_skip_bt); } ViewGroup parent; if ((parent = (ViewGroup) skipButton.getParent()) != null) { parent.removeView(skipButton); } rootGroup.addView(skipButton);

(2)按下back鍵中止播放

 SimpleDraweeView gifView = (SimpleDraweeView) activity.findViewById(R.id.gif_tip_view); if (gifView != null) { DraweeController controller = gifView.getController(); Animatable anim = null; if (controller != null) { anim = controller.getAnimatable(); if (anim != null && anim.isRunning()) { anim.stop(); } } rootGroup.removeView(gifView); }

參考資料:
Fresco(各類特效)——播放gif
http://blog.csdn.net/yy1300326388/article/details/45057677
Android實現加載GIF圖片
http://www.jianshu.com/p/04a6433dd456
安卓中使用fresco加載Gif圖片
http://blog.csdn.net/shlock_fan/article/details/50334273

相關文章
相關標籤/搜索