整合大量开源库项目(八)可以加载Gif动画的GifImageView

来源:互联网 发布:台湾人看待统一知乎 编辑:程序博客网 时间:2024/05/11 19:37

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

上周大多数时间都是根据兴起,想到什么做什么写了几个自定义控件,把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'}

是不是加进来的东西越来越多了? 之后还会继续添加(当然,实际项目中不建议使用过多的第三方框架,毕竟大框架的个别功能你是用不到的,而自己却载入了那么多内容,容易加大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的选择会让第二层的内容大不相同,像这样。

这里写图片描述

那么难道,我去写一大堆新的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

记得点个赞哦!

这里写图片描述

3 0