成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用Retrofit+RxJava+MVP打造一款MaterialDesign風格的APP

移動開發 Android
為了熟悉使用一些開源框架,便決定利用業余時間寫一個APP來熟悉這些框架的使用。提前踩一踩坑,方便以后在公司的項目中使用。使用的接口是聚合數據的和干貨集中營的,非常感謝。

為了熟悉使用一些開源框架,便決定利用業余時間寫一個APP來熟悉這些框架的使用。提前踩一踩坑,方便以后在公司的項目中使用。使用的接口是聚合數據的和干貨集中營的,非常感謝。

效果圖

 

 

 

 

用到的主流框架

  • 首頁側滑欄使用DrawerLayout+NavigationView實現的
  • 使用Realm數據庫實現本地收藏
  • 使用Retrofit+RxJava+RxAndroid實現網絡請求,并對返回結果進行了簡單的封裝
  • 對RecyclerView的Adapter和ViewHolder進行封裝,實現了上拉加載
  • 使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout實現了炫酷的滑動動畫
  • 使用Glide實現了圖片的加載
  • 使用PhotoView實現了圖片的縮放
  • 日歷使用開源的material-calendarview
  • 實現了SwipeRefreshLayout首次進入自動刷新

一、使用DrawerLayout+NavigationView實現側滑欄 

  1. <?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout 
  2.     xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     xmlns:app="http://schemas.android.com/apk/res-auto" 
  4.     android:id="@+id/drawerLayout" 
  5.     android:layout_width="match_parent" 
  6.     android:layout_height="match_parent"
  7.  
  8.     <LinearLayout 
  9.         android:layout_width="match_parent" 
  10.         android:layout_height="match_parent" 
  11.         android:orientation="vertical"
  12.  
  13.        <android.support.v7.widget.Toolbar  
  14.             android:id="@+id/toolbar" 
  15.             android:layout_width="match_parent" 
  16.             android:layout_height="wrap_content" 
  17.             app:titleTextColor="@android:color/white" /> 
  18.  
  19.         <FrameLayout 
  20.             android:id="@+id/fl_main" 
  21.             android:layout_width="match_parent" 
  22.             android:layout_height="match_parent"></FrameLayout> 
  23.     </LinearLayout> 
  24.  
  25.     <android.support.design.widget.NavigationView 
  26.         android:id="@+id/navigation" 
  27.         android:layout_width="match_parent" 
  28.         android:layout_height="match_parent" 
  29.         android:layout_gravity="start" 
  30.         android:fitsSystemWindows="true" 
  31.         app:headerLayout="@layout/drawer_header" 
  32.         app:menu="@menu/drawer_menu"
  33.     </android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>  

DrawerLayout是Androidv4包里自帶的控件,支持左滑和右滑,android:layout_gravity="leftt"代表左滑界面(或者start),android:layout_gravity="right"代碼右滑的界面(或者end),不加layout_gravity的就是主界面。代碼里可以添加ActionBarDrawerToggle控制側滑欄展示與隱藏。

  1. ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolBar, R.string.open, R.string.close); 
  2. mDrawerToggle.syncState(); 
  3. mDrawer.addDrawerListener(mDrawerToggle);  

NavigationView是Google在5.0之后推出的一個控件,主要作為菜單控件使用,分為上下部分,上面的部分為headerLayout,可以自定義布局,下面的部分為menu,作為導航菜單的菜單項 

  1. <?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"
  2.     <item 
  3.         android:id="@+id/drawer_todayInHistory" 
  4.         android:checkable="true" 
  5.         android:icon="@drawable/ic_history" 
  6.         android:title="歷史上的今天" /> 
  7.     <item 
  8.         android:id="@+id/drawer_gril" 
  9.         android:checkable="true" 
  10.         android:icon="@drawable/icon_gril" 
  11.         android:title="妹紙" /> 
  12.     <item 
  13.         android:id="@+id/drawer_like" 
  14.         android:checkable="true" 
  15.         android:icon="@drawable/ic_unlike" 
  16.         android:title="收藏" /> 
  17.     <item 
  18.         android:id="@+id/drawer_about" 
  19.         android:checkable="true" 
  20.         android:icon="@drawable/ic_about" 
  21.         android:title="關于" /></menu>  

點擊事件:

  1. navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {   
  2.     @Override   
  3.     public boolean onNavigationItemSelected(MenuItem item) {   
  4.         //在這里處理item的點擊事件   
  5.         return true;   
  6.     }   
  7. });  

獲取頭部(headerLayout)內控件:

  1. View headView=navigationView.getHeaderView(0); 

設置菜單列表圖標顏色:

默認情況下,菜單圖標顏色為灰色,可以通過一下設置圖標顏色

  1. app:itemIconTint="" 

添加分割線:

只需將菜單分成多個Group,每個Group設置一個Id,那么Group之間就會有分割線: 

  1. <menuxmlns:android="http://schemas.android.com/apk/res/android"
  2. <groupandroid:id="@+id/g1"
  3. <item 
  4. android:id="@+id/favorite" 
  5. android:icon="@mipmap/ic_launcher" 
  6. android:title="歷史上的今天"/> 
  7. <item 
  8. android:id="@+id/wallet" 
  9. android:icon="@mipmap/ic_launcher" 
  10. android:title="收藏"/> 
  11. </group
  12. <groupandroid:id="@+id/g2"
  13. <item 
  14. android:id="@+id/photo" 
  15. android:icon="@mipmap/ic_launcher" 
  16. android:title="妹子"/> 
  17. </group
  18. <item 
  19. android:id="@+id/file" 
  20. android:icon="@mipmap/ic_launcher" 
  21. android:title="關于"/> 
  22. </menu>  

二、Glide加載圖片

設置綁定生命周期

  1. Glide.with(Context context);// 綁定Context 
  2.   Glide.with(Activity activity);// 綁定Activity 
  3.   Glide.with(FragmentActivity activity);// 綁定FragmentActivity 
  4.   Glide.with(Fragment fragment);// 綁定Fragment  

常規用法: 

  1. Glide.with(context) 
  2.                 .load(imageUrl)//圖片路徑 
  3.                 .placeholder(R.drawable.ic_launcher)//設置加載中圖片 
  4.                 .error(R.drawable.ic_launcher)//設置加載失敗圖片 
  5.                 .skipMemoryCache(true)//設置跳過內存緩存 
  6.                 .diskCacheStrategy(DiskCacheStrategy.ALL)//設置緩存策略:all:緩存源資源和轉換后的資源/none:不作任何磁盤緩存 /source:緩存源資源 /result:緩存轉換后的資源 
  7.                 .priority(Priority.NORMAL)//設置下載優先級 
  8.                 .animate(R.anim.item_alpha_in)//設置加載動畫 
  9.                 .thumbnail(0.1f)//設置縮略圖支持(先加載縮略圖,再加載全圖) 
  10.                 .override(400,400)//設置加載尺寸 
  11.                 .centerCrop()//設置動態變換 
  12.                 .into(imageView);  

加載Git圖片:

  1. Glide.with(this).load(imageUrl).asGif().into(imageView); 

動態緩存清理:

  1. Glide.get(this).clearDiskCache();//清理磁盤緩存 需要在子線程中執行 Glide.get(this).clearMemory();//清理內存緩存 可以在UI主線程中進行 

加載圓角圖片或圓形圖片:

  1. Glide.with(this).load(imageUrl).transform(new GlideRoundTransform(this)).into(imageView); 

需要自定義Transform,這里提供一個圓角和一個圓形的Transform:

圓角轉換: 

  1. public class GlideRoundTransform extends BitmapTransformation { 
  2.  
  3.     private static float radius = 0f; 
  4.  
  5.     public GlideRoundTransform(Context context) { 
  6.         this(context, 4); 
  7.     } 
  8.  
  9.     public GlideRoundTransform(Context context, int dp) { 
  10.         super(context); 
  11.         this.radius = Resources.getSystem().getDisplayMetrics().density * dp; 
  12.     } 
  13.  
  14.     @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 
  15.         return roundCrop(pool, toTransform); 
  16.     } 
  17.  
  18.     private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { 
  19.         if (source == nullreturn null
  20.  
  21.         Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); 
  22.         if (result == null) { 
  23.             result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); 
  24.         } 
  25.  
  26.         Canvas canvas = new Canvas(result); 
  27.         Paint paint = new Paint(); 
  28.         paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); 
  29.         paint.setAntiAlias(true); 
  30.         RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); 
  31.         canvas.drawRoundRect(rectF, radius, radius, paint); 
  32.         return result; 
  33.     } 
  34.  
  35.     @Override public String getId() { 
  36.         return getClass().getName() + Math.round(radius); 
  37.     } 
  38.  

圓形圖片轉換: 

  1. public class GlideCircleTransform extends BitmapTransformation { 
  2.     public GlideCircleTransform(Context context) { 
  3.         super(context); 
  4.     } 
  5.  
  6.     @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 
  7.         return circleCrop(pool, toTransform); 
  8.     } 
  9.  
  10.     private static Bitmap circleCrop(BitmapPool pool, Bitmap source) { 
  11.         if (source == nullreturn null
  12.  
  13.         int size = Math.min(source.getWidth(), source.getHeight()); 
  14.         int x = (source.getWidth() - size) / 2; 
  15.         int y = (source.getHeight() - size) / 2; 
  16.  
  17.         // TODO this could be acquired from the pool too 
  18.         Bitmap squared = Bitmap.createBitmap(source, x, y, sizesize); 
  19.  
  20.         Bitmap result = pool.get(sizesize, Bitmap.Config.ARGB_8888); 
  21.         if (result == null) { 
  22.             result = Bitmap.createBitmap(sizesize, Bitmap.Config.ARGB_8888); 
  23.         } 
  24.  
  25.         Canvas canvas = new Canvas(result); 
  26.         Paint paint = new Paint(); 
  27.         paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); 
  28.         paint.setAntiAlias(true); 
  29.         float r = size / 2f; 
  30.         canvas.drawCircle(r, r, r, paint); 
  31.         return result; 
  32.     } 
  33.  
  34.     @Override public String getId() { 
  35.         return getClass().getName(); 
  36.     } 
  37.  

獲取Bitmap 

  1. Glide.with(this) 
  2.                 .load(imageUrl) 
  3.                 .asBitmap() 
  4.                 .into(new SimpleTarget<Bitmap>() { 
  5.                     @Override 
  6.                     public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { 
  7.                         imageView.setImageBitmap(mBitmap); 
  8.                     } 
  9.                 });  
責任編輯:龐桂玉 來源: 安卓巴士Android開發者門戶
相關推薦

2021-12-07 10:23:27

鴻蒙HarmonyOS應用

2014-12-16 10:11:22

2020-12-03 09:00:02

Java外賣系統

2016-12-28 13:55:16

Android框架MVP

2020-12-07 11:50:14

Java學習系統eclipse

2015-08-18 09:11:34

杜長偉APP

2020-11-05 09:27:48

JavaScript開發技術

2022-02-17 10:26:17

JavaScript掃雷游戲前端

2021-12-30 08:56:57

Python摸魚倒計界面Python基礎

2021-11-01 10:26:07

CanvasAPI畫布技術HTML5

2020-05-11 13:40:48

編程新冠App

2022-03-04 09:05:55

StarRocks數據湖數據質量

2021-11-17 15:36:04

鴻蒙HarmonyOS應用

2014-06-20 10:32:42

APP上癮設計

2013-07-16 10:09:15

2015-11-27 09:18:11

AngularJSWeb應用

2021-08-03 12:47:58

鴻蒙HarmonyOS應用

2020-03-12 09:20:41

微軟瀏覽器Windows

2019-05-06 11:49:10

DomTerm終端模擬器Linux

2017-01-13 08:37:57

PythonAlphaGoMuGo
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美成ee人免费视频 | 亚洲视频1区 | 中文字幕亚洲欧美日韩在线不卡 | 免费观看毛片 | 久久国产精品视频 | 91在线精品视频 | 亚洲一区三区在线观看 | 久久99久久99久久 | 国产美女特级嫩嫩嫩bbb片 | 一区二区在线不卡 | 一区二区不卡 | 97精品国产97久久久久久免费 | 91精品一区二区三区久久久久久 | 午夜精品一区二区三区免费视频 | 91免费视频| 一区二区久久 | 极品久久 | 亚洲综合大片69999 | 三级成人片 | 欧美日韩亚洲国产综合 | h片在线观看免费 | 岛国av一区二区 | 国产成人精品a视频一区www | 欧美成人免费在线视频 | 午夜99| 午夜精品久久久久久久久久久久久 | 羞羞色视频 | 欧美中文一区 | 欧美日韩不卡 | 91极品尤物在线播放国产 | 久久久久久久国产 | 精品日本久久久久久久久久 | 久久久久久99 | 欧美在线观看一区 | 亚洲精品456| 福利一区二区在线 | 少妇午夜一级艳片欧美精品 | 日本精品一区二区三区在线观看视频 | 国产精品69毛片高清亚洲 | 国产黄色大片在线观看 | 99国产精品久久久久久久 |