zoukankan      html  css  js  c++  java
  • 整合大量开源库项目(八)能够载入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

    作者: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

    记得点个赞哦!

    这里写图片描写叙述

  • 相关阅读:
    自动跳转至首页(Java Script)
    简单的轮播图(Java Script)
    蓝桥杯 2016年 第七届 四平方和(JAVA)
    蓝桥杯 2016年 第七届 剪邮票(JAVA)
    蓝桥杯 2015年 第六届 生命之树(JAVA)
    蓝桥杯 2015年 第六届 垒骰子(JAVA)
    numpy笔记
    opencv安装和运行
    vs code环境配置注意点
    numpy中matrix使用方法
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/7389032.html
Copyright © 2011-2022 走看看