整合大量開源庫項目(八)能夠載入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

記得點個贊哦!

這里寫圖片描寫敘述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM