Android 提高用戶體驗之骨架屏

前幾天公司app作優化,提出了不要菊花圖,緣由是用戶進入界面的時候彈出對話框壓迫感太強而且在ios端因爲沒有返回按鈕要等請求完才能操做,很坑爹。恰巧那天又看到《玉剛說》的公衆號推了一篇骨架屏的文章,看了一下而且本身嘗試了一下其餘的接入總結一下坑點。android

關於這方面的第三方庫

一個封裝了Recycview的遮罩控件,使用起來比下面的要方便。ios

一個封裝了遮罩層的庫git

一個使佈局閃爍的庫,不少第三方庫都依賴於它github


遮罩庫bash

僅僅支持圖片跟文本,設置值時會去除遮罩。具體實現能夠看看源碼,app

遮罩控件,支持多種控件,可是須要嵌套多層會影響一點性能maven

實現方法

-c

如效果圖,實現的思想有兩種。一種是寫多一個遮罩佈局,請求成功後再替換。這個也是上面第三方庫的實現方法另外一種是遮罩控件嵌套在原佈局裏,顯示成功後調用方法隱藏。不管那種方式最好叫設計師給好一個遮罩item的原型尺寸,不然遇到複雜佈局時本身很難調節大小。ide

最簡單的方法

只須要把RecyclerView換成ShimmerRecyclerView調用顯示、隱藏方法便可。這種方法也須要寫一個遮罩佈局,不然是默認的。佈局

  • 接入
repositories {
    jcenter()
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation 'com.github.sharish:ShimmerRecyclerView:v1.3'
}
複製代碼
  • 使用
<com.cooltechworks.views.shimmer.ShimmerRecyclerView xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/shimmer_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" app:shimmer_demo_child_count="10" app:shimmer_demo_grid_child_count="2" app:shimmer_demo_layout="@layout/layout_demo_grid" app:shimmer_demo_layout_manager_type="grid" app:shimmer_demo_angle="20" />
複製代碼

調用對應的顯示隱藏方法性能

shimmerRecycler.showShimmerAdapter();

shimmerRecycler.hideShimmerAdapter();
複製代碼

Skeleton+佈局

這種方法比較自由,除了RecyclerView之外還能用在其餘佈局上

  • 接入
repositories {
    jcenter()
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation 'com.github.sharish:ShimmerRecyclerView:v1.3'
}
複製代碼
  • 使用
    RecyclerView:
skeletonScreen = Skeleton.bind(recyclerView)
                              .adapter(adapter)
                              .load(R.layout.item_skeleton_news)
                              .show();
複製代碼

其餘View:

skeletonScreen = Skeleton.bind(rootView)
                              .load(R.layout.layout_img_skeleton)
                              .show();
複製代碼

提供的方法

.shimmer(true)      // whether show shimmer animation.                      default is true
.count(10)          // the recycler view item count.                        default is 10
.color(color)       // the shimmer color.                                   default is #a2878787
.angle(20)          // the shimmer angle.                                   default is 20;
.duration(1000)     // the shimmer animation duration.                      default is 1000;
.frozen(false)      // whether frozen recyclerView during skeleton showing  default is true; 
複製代碼

消失時調用

skeletonScreen.hide()
複製代碼

遮罩控件

上面的方法都要寫一個佈局,若是不想寫多一個佈局的可使用遮罩控件

  1. github.com/elye/loader…
    -c

在setText或者給src設值時自動去除遮罩。能夠設置默認遮罩的百分比長度還能夠設置帶一點點的閃爍動畫等等。可是隻有文本跟圖片控件可用,以及不能根據文本長度來設置遮罩,由於setText以後會自動消失,因此在某些比較複雜的佈局裏若是沒有設計師提供尺寸圖是作不出效果的。

  • 接入
dependencies {
    implementation 'com.elyeproj.libraries:loaderviewlibrary:1.5.0'
}
複製代碼
  • 使用
<com.elyeproj.loaderviewlibrary.LoaderImageView
     android:layout_width="100dp"
     android:layout_height="100dp" />
複製代碼
<com.elyeproj.loaderviewlibrary.LoaderTextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:width_weight="0.4" 
     app:height_weight="0.8"
     app:use_gradient="true"
      app:corners="16"
       app:custom_color="@android:color/holo_green_dark" />
複製代碼

若是須要從新加載

myLoaderTextView.resetLoader();
複製代碼

2.github.com/rasoulmiri/…

-c

除了上圖的效果外還支持其餘效果,而且支持任意佈局,由於它的使用方法是在原有佈局上包裹一層。這種方式的好處是,能夠不用設計師出遮罩圖,套一層佈局在外面後設置包裹內容便可,只是會比較耗費一點渲染性能。

  • 接入
allprojects {
    repositories {
	    ...
	    maven { url 'https://jitpack.io' }
    }
}
複製代碼
dependencies {
      compile 'com.github.rasoulmiri:Skeleton:v1.0.9'
}
複製代碼
  • 使用
<io.rmiri.skeleton.SkeletonGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

         <io.rmiri.skeleton.SkeletonView ...>
            <View ... />
        </io.rmiri.skeleton.SkeletonView>

        <io.rmiri.skeleton.SkeletonView ...>
            <View ... />
        </io.rmiri.skeleton.SkeletonView>

</io.rmiri.skeleton.SkeletonGroup>
複製代碼

支持動畫監聽

skeletonGroup.setSkeletonListener(new SkeletonGroup.SkeletonListener() {
      @Override
      public void onStartAnimation() {
	...
      }

      @Override
      public void onFinishAnimation() {
	...
      }
 });
複製代碼

建議閱讀

骨架屏 (Skeleton Screen) 在 Android 中的應用


最開始堅持的地方,記錄學習與生活的點點滴滴
相關文章
相關標籤/搜索