整合大量开源库项目(八)能够载入Gif动画的GifImageView
转载请注明出处王亟亟的大牛之路
上周大多数时间都是依据兴起,想到什么做什么写了几个自己定义控件,把Soyi丢在那没怎么动,今天就把写的东西整合进来,顺便把SOyi”个人研发的结构理一下”。
先上一下今天整合之后的效果,以及新加进来的几个库:
依照惯例,贴一下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
通常为我们的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的选择会让第二层的内容大不同样,像这样。
那么难道,我去写一大堆新的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的更多相关文章
- 整合大量开源库项目(五)跳动的TextView JumpingBeans,良好体验的滚动条ConvenientBanner
转载请注明出处:王亟亟的大牛之路 时间过得非常快,这一系列已经写了第五篇了(感觉还要写好久).今天又引入了2个非常好用的库JumpingBeans,ConvenientBanner.首先.先看一下效果 ...
- Android开源库项目集锦
一.兼容类库 ActionBarSherlock : Action Bar是Android 3.0后才開始支持的,ActionBarSherlock是让Action Bar功能支持2.X后的全部平台. ...
- iOS项目中常用的第三方开源库
1.项目使用的第三方开源库 项目使用了CocoaPods(类似java中的maven)管理常用的第三方库,一些特殊的单独引用,下面介绍下比较好用的几个. (1)AFNetworking 目前比较推荐的 ...
- 第三方开源库和jar包的区别
jar包和第三方开源库的根本区别在于,开源库的功能比jar包功能更强大,通过引入库项目可以访问java文件以及该开源库项目下的资源文件,例如图片,layout等文件 jar包中只能放class文件 引 ...
- Android 使用开源库载入网络图片
Android 使用开源库载入网络图片,使用开源库载入图片.单击listview弹出popupwindow弹出框详情查看: Android 单击listview弹出popupwindow弹出框 ,里面 ...
- iOS 项目中用到的一些开源库和第三方组件
iOS 项目中用到的一些 iOS 开源库和第三方组件 分享一下我目前所在公司 iOS 项目中用到的一些 iOS 开源库和第三方组件, 感谢开源, 减少了我们的劳动力, 节约了我们大量的时间, 让我们有 ...
- Android 开源库和项目 3
Android 开源库和项目 Android 开源库和项目 2 1.Matisse Android 图片选择器 -- 知乎开源 github 地址:https://github.com/zhihu/M ...
- 如何在Android Studio项目中导入开源库?
前两天,谷歌发布了Android Studio 1.0的正式版,也有更多的人开始迁移到Android Studio进行开发.然而,网上很多的开源库,控件等还是以前的基于Eclipse进行开发,很多人不 ...
- AndroidStudio怎样导入library项目开源库
AndroidStudio是一款非常强大的android程序开发软件,在里面集成了几乎所有android开发中需要使用的工具,编译.运行.打包.开发.调试等功能一应俱全,可以使用起来非常方便. 今天要 ...
随机推荐
- docker安装tomcat
先在官网上找可用的镜像 我使用的是7-jre8 获取tomcat镜像的命令:$docker pull tomcat:7-jre8 获取完镜像以后,通过命令可以列举出已有的镜像: 列举镜像的命令:$do ...
- JQuery 之 重置表单的方法
由于JQuery中,提交表单是如下方式: $('#formId').submit() 想当然的认为,重置表单,应该如下: $('#formId').reset(); 但是,这样表单无法重置! 经查阅资 ...
- Qt通过ODBC连接SQL Server2008实践总结
Qt连接数据库的方式很多,这里说明一种最常用也是最实用的方式,因为这种方式在Windows上开发程序使用起来非常方便,并且也是远程连接数据库所需要用到的方式. 前提工作: 在Win7下安装了SQL S ...
- JAVA的堆于栈
1. 栈(stack)与堆(heap):都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. 2. 栈的优势:存取速度比堆要快,仅次于直接位于C ...
- MongoDB副本集配置系列五:副本集的在线迁移
MongoDB副本集的在线迁移 查看当前集群的状态: { "setName" : "gechongrepl", "setVersion" : ...
- logrotate日志不轮转呢?_新日志size0
程序,要管理log,留作日后的后续功能处理和统计和领导需要.因为懒得写,所以直接用了syslog函数,把log交给rsyslog去写了.然后用logrotate每天做日志轮转. 两种log分别发送到了 ...
- CentOS yum时出现"Could not retrieve mirrorlist"
问题描述: CentOS 6.x minimal(最小化) 安装, CentOS yum install net-tools 时出现"Could not retrieve mirrorlis ...
- 【转载】Java 网络编程
本文主要是自己在网络编程方面的学习总结,先主要介绍计算机网络方面的相关内容,包括计算机网络基础,OSI参考模型,TCP/IP协议簇,常见的网络协议等等,在此基础上,介绍Java中的网络编程. 一. ...
- UI Automation的两个成熟的框架(QTP 和Selenium)
自己在google code中开源了自己一直以来做的两个自动化的框架,一个是针对QTP的一个是针对Selenium的,显而易见,一个是商业的UI automation工具,一个是开源的自动化工具. 只 ...
- python之模块配置文件ConfigParser(在python3中变化较大)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ConfigParser(在python3中为configparser) #特别注意:py ...