整合大量開源庫項目(八)可以載入Gif動畫的GifImageView

轉載請註明出處王亟亟的大牛之路

上週大多數時間都是依據興起,想到什麼作什麼寫了幾個本身定義控件,把Soyi丟在那沒怎麼動,今天就把寫的東西整合進來,順便把SOyi」我的研發的結構理一下」。java

先上一下今天整合以後的效果,以及新加進來的幾個庫:android

這裏寫圖片描寫敘述

依照慣例,貼一下Gradle的配置:git

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無謂的容積)github


這一篇咱們加了什麼,講些什麼??

GifImageView和簡單的代碼梳理。(有必定工做經歷的小夥伴可以不看第二部分,源代碼仍是在最如下)數組

項目地址:https://github.com/felipecsl/GifImageViewmarkdown

做者:http://felipecsl.com多線程

一般爲咱們的ImageView僅僅支持普通的靜態圖片的展示(png,jpg等),假設是動圖什麼的就需要咱們本身寫了。但是有人給咱們寫好了,爲什麼不用呢?app

樓主這邊爲你們簡單的分析下這個庫的實現。框架

public class GifImageView extends ImageView implements Runnable

↑ 繼承於ImageView繼承Runnable,也就是說咱們的各類繪畫的操做事實上是在多線程的環境下進行的。ide

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

記得點個贊哦!

這裏寫圖片描寫敘述

相關文章
相關標籤/搜索