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

相关文章
相关标签/搜索