整合大量开源库项目(八)能够载入Gif动画的GifImageView

时间:2022-09-05 12:36:48

转载请注明出处王亟亟的大牛之路

上周大多数时间都是依据兴起,想到什么做什么写了几个自己定义控件,把Soyi丢在那没怎么动,今天就把写的东西整合进来,顺便把SOyi”个人研发的结构理一下”。

先上一下今天整合之后的效果,以及新加进来的几个库:

整合大量开源库项目(八)能够载入Gif动画的GifImageView

依照惯例,贴一下Gradle的配置:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'cn.pedant.sweetalert:library:1.3'
compile 'com.apkfuns.logutils:library:1.0.6'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.squareup.okhttp:okhttp:2.7.0'
compile 'commons-io:commons-io:2.4'
compile 'com.ikimuhendis:ldrawer:0.1'
compile 'com.dodola:listviewext:1.0'
compile 'com.bm.photoview:library:1.3.6'
compile 'com.lsjwzh:materialloadingprogressbar:0.5.8-RELEASE'
compile 'net.frakbot:jumpingbeans:1.3.0'
compile 'com.bigkoo:convenientbanner:1.1.4'
compile files('libs/universal-image-loader-1.9.4.jar')
compile 'com.google.code.gson:gson:2.5'
compile 'com.android.support:recyclerview-v7:23.1.+'
compile 'com.felipecsl:gifimageview:2.0.0'
compile 'com.android.support:support-annotations:23.1.1'
}

是不是加进来的东西越来越多了? 之后还会继续加入(当然,实际项目中不建议使用过多的第三方框架,毕竟大框架的个别功能你是用不到的,而自己却载入了那么多内容,easy加大apk无谓的容积)


这一篇我们加了什么,讲些什么??

GifImageView和简单的代码梳理。(有一定工作经历的小伙伴能够不看第二部分,源代码还是在最以下)

项目地址:https://github.com/felipecsl/GifImageView

作者:http://felipecsl.com

通常为我们的ImageView仅仅支持普通的静态图片的展现(png,jpg等),假设是动图什么的就须要我们自己写了。可是有人给我们写好了,为何不用呢?

楼主这边为大家简单的分析下这个库的实现。

public class GifImageView extends ImageView implements Runnable

↑ 继承于ImageView继承Runnable,也就是说我们的各种绘画的操作事实上是在多线程的环境下进行的。

 private final Runnable updateResults = new Runnable() {
@Override
public void run() {
if (tmpBitmap != null && !tmpBitmap.isRecycled()) {
setImageBitmap(tmpBitmap);
}
}
};

推断临时的tmpBitmap 不为空,而且没有被释放,然后给imageview设置这张图片,这种方法在handle中被多次传递。

 private final Runnable cleanupRunnable = new Runnable() {
@Override
public void run() {
if (tmpBitmap != null && !tmpBitmap.isRecycled()) {
tmpBitmap.recycle();
}
tmpBitmap = null;
gifDecoder = null;
animationThread = null;
shouldClear = false;
}
};

↑ 回收tmpBitmap 而且。清空一系列參数。

  @Override public void run() {
if (shouldClear) {
handler.post(cleanupRunnable);
return;
} final int n = gifDecoder.getFrameCount();
do {
for (int i = 0; i < n; i++) {
if (!animating) {
break;
}
//milliseconds spent on frame decode
long frameDecodeTime = 0;
try {
long before = System.nanoTime();
tmpBitmap = gifDecoder.getNextFrame();
frameDecodeTime = (System.nanoTime() - before) / 1000000;
if (frameCallback != null) {
tmpBitmap = frameCallback.onFrameAvailable(tmpBitmap);
} if (!animating) {
break;
}
handler.post(updateResults);
} catch (final ArrayIndexOutOfBoundsException | IllegalArgumentException e) {
Log.w(TAG, e);
}
if (!animating) {
break;
}
gifDecoder.advance();
try {
int delay = gifDecoder.getNextDelay();
// Sleep for frame duration minus time already spent on frame decode
// Actually we need next frame decode duration here,
// but I use previous frame time to make code more readable
delay -= frameDecodeTime;
if (delay > 0) {
Thread.sleep(framesDisplayDuration > 0 ? framesDisplayDuration : delay);
}
} catch (final Exception e) {
// suppress any exception
// it can be InterruptedException or IllegalArgumentException
}
}
} while (animating);
}

主要实现的run方法。先推断是否clear,默认false.(也就是animationThread 这条工作线程的行为)

然后获取从文件读取的帧的数目(这边仅仅解释下主实现类的内容,Gif事实上就是帧动画)

接下来循环,開始更替图片操作(理论上一帧一画面)

推断假设正在动画效果中。就不进行在此循环操作(由于可能出现手动调用startAnimation()的可能)

接下来就是一帧持续多久,然后替换,然后直到最后一帧的显示结束,再继续。

整个包大概 10来个类,大家能够自己有时间具体读取。

总结:现对于https://github.com/frapontillo/ImageViewEx的实现属于比較轻量级的了。毕竟简单有用是大家更喜欢的。实现大致就是依据传入的数组进行计算,把每一帧的动画进行迭代的呈如今UI界面上,然后在调用StopAnimation()或者clear()之前会形成一个环。

当然这种频繁刷UI界面还是会有一定的性能影响。看你怎么使用了。


接下来再说下“个人研发”模块。那这是什么东西呢?

非常显然看上去就是一个ListView套着一个ListView然后第一层的ListView的选择会让第二层的内容大不同样,像这样。

整合大量开源库项目(八)能够载入Gif动画的GifImageView

那么难道,我去写一大堆新的xml么?

No,找共同点,遵循OOP会发现共同点。

统一的item,统一的呈现页面(一个Title,1个展示图,一个文字描写叙述,一个超级链接)

那就是主要的MVC模式我们在 View。Controller层面的内容是大致同样的那么就仅仅要在Model层做出一些改变就好了。那么从头到尾 我 仅仅须要一个布局文件,像这样

<?

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Activity.CodeActivityPro.CodeActivity"> <android.support.v7.widget.RecyclerView
android:id="@+id/codeListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:scrollbars="none"
android:layoutAnimation="@anim/code_item_anim" />
</RelativeLayout>

然后就是展示页面,像这样

<?xml version="1.0" encoding="utf-8"?

>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="soyi.pro.com.soyi.Activity.CodeActivityPro.ShowCodeViewActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <com.felipecsl.gifimageview.library.GifImageView
android:id="@+id/showCodeViewImage"
android:layout_gravity="center"
android:layout_width="350dp"
android:layout_height="450dp"
android:src="@drawable/tempimage"/> <TextView
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="载入中..."
android:textSize="20dp"
android:id="@+id/jumpText"
android:layout_alignBottom="@id/showCodeViewImage"
android:layout_gravity="center"/>
</LinearLayout>
</ScrollView>

其它一系列就从arrays.xml里面获取内容就好了当然。传递通过intent.putExtra,或者假设要2层都做在一个页面里那就设置点静态变量什么的。记录用户的选择吧。

怎样让你的TextView能够变为链接?

 textView.setText(Html.fromHtml(getIntent().getStringExtra("CodeActivityToShowCodeActivityMSG")  +"<br><a href=\""+getIntent().getStringExtra("CodeActivityToShowCodeActivityGitUrl")+"\">点击链接可訪问项目地址</a>"));
textView.setMovementMethod(LinkMovementMethod.getInstance());

源代码地址:https://github.com/ddwhan0123/SoyiGit

记得点个赞哦!

整合大量开源库项目(八)能够载入Gif动画的GifImageView

整合大量开源库项目(八)能够载入Gif动画的GifImageView的更多相关文章

  1. 整合大量开源库项目(五)跳动的TextView JumpingBeans&comma;良好体验的滚动条ConvenientBanner

    转载请注明出处:王亟亟的大牛之路 时间过得非常快,这一系列已经写了第五篇了(感觉还要写好久).今天又引入了2个非常好用的库JumpingBeans,ConvenientBanner.首先.先看一下效果 ...

  2. Android开源库项目集锦

    一.兼容类库 ActionBarSherlock : Action Bar是Android 3.0后才開始支持的,ActionBarSherlock是让Action Bar功能支持2.X后的全部平台. ...

  3. iOS项目中常用的第三方开源库

    1.项目使用的第三方开源库 项目使用了CocoaPods(类似java中的maven)管理常用的第三方库,一些特殊的单独引用,下面介绍下比较好用的几个. (1)AFNetworking 目前比较推荐的 ...

  4. 第三方开源库和jar包的区别

    jar包和第三方开源库的根本区别在于,开源库的功能比jar包功能更强大,通过引入库项目可以访问java文件以及该开源库项目下的资源文件,例如图片,layout等文件 jar包中只能放class文件 引 ...

  5. Android 使用开源库载入网络图片

    Android 使用开源库载入网络图片,使用开源库载入图片.单击listview弹出popupwindow弹出框详情查看: Android 单击listview弹出popupwindow弹出框 ,里面 ...

  6. iOS 项目中用到的一些开源库和第三方组件

    iOS 项目中用到的一些 iOS 开源库和第三方组件 分享一下我目前所在公司 iOS 项目中用到的一些 iOS 开源库和第三方组件, 感谢开源, 减少了我们的劳动力, 节约了我们大量的时间, 让我们有 ...

  7. Android 开源库和项目 3

    Android 开源库和项目 Android 开源库和项目 2 1.Matisse Android 图片选择器 -- 知乎开源 github 地址:https://github.com/zhihu/M ...

  8. 如何在Android Studio项目中导入开源库?

    前两天,谷歌发布了Android Studio 1.0的正式版,也有更多的人开始迁移到Android Studio进行开发.然而,网上很多的开源库,控件等还是以前的基于Eclipse进行开发,很多人不 ...

  9. AndroidStudio怎样导入library项目开源库

    AndroidStudio是一款非常强大的android程序开发软件,在里面集成了几乎所有android开发中需要使用的工具,编译.运行.打包.开发.调试等功能一应俱全,可以使用起来非常方便. 今天要 ...

随机推荐

  1. winform采集网站美女图片程序---多线程篇

    设定思路: 采集目标: http://www.8kmm.com,   已知网址列表(List保存),  应用多线程(Thread)读取该列表, 获取url时不能重复(加锁Lock). 允许无序采集! ...

  2. git的基本操作

    今天给同事培训了一下git的使用流程,简单记录一下 1,基本概念, 远程库和本地库. 2, git clone git://url/*.git clone远程的代码库到本地 3. 创建本地分支 当前是 ...

  3. Known plaintext attack

    When you find a ZIP/RAR file with password protected in the evidence, you may try dictionary attack ...

  4. 【IOCP】 IOCP模型属于一种通讯模型- 较难

    http://baike.baidu.com/link?url=e9vXkKd2aHp8VDr1XTURdwQB4K85r28IYjeMwRIyuaXtsrCsXHY1eohiFgsDXRYRlj6x ...

  5. MyEclipse下查看Java API帮助文档

    每次重装JDK或者升级JDK时,都会忘了如何使MyEclipse关联帮助文档.然后,再花十几分钟重新google搜索,麻烦! 首先下载Javadoc api帮助文档,google搜一下就行了. MyE ...

  6. Python Monkey patch猴子补丁

    monkey patch (猴子补丁)   用来在运行时动态修改已有的代码,而不需要修改原始代码. 简单的monkey patch 实现:[python] #coding=utf-8 def orig ...

  7. NSURLSessionDataTask

    #import "ViewController.h" @interface ViewController ()<NSURLSessionDelegate,NSURLSessi ...

  8. python3-day4&lpar;re正则表达式&comma;冒泡&rpar;

    一.正则表达式常用 1.re.match:只尝试从字符串的查找,后面不作查找. 例子: import re text="aabcdefg123123" m=re.match('a' ...

  9. &lpar;asp&period;net MVC学习&rpar;System&period;Web&period;Mvc&period;UrlHelper的学习与使用

    上一次学习了HtmlHelper帮助类,这次我们学习一下UrlHelper帮 助类,看类名也都知道这个类是用来帮我们生成URL在ASP.NET MVC应用程序中.让我们来看看该类给我们带来了哪些方便的 ...

  10. leetcode&lowbar;question&lowbar;64 Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...