自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用Retrofit+RxJava+MVP打造一款MaterialDesign風(fēng)格的APP

移動(dòng)開發(fā) Android
為了熟悉使用一些開源框架,便決定利用業(yè)余時(shí)間寫一個(gè)APP來熟悉這些框架的使用。提前踩一踩坑,方便以后在公司的項(xiàng)目中使用。使用的接口是聚合數(shù)據(jù)的和干貨集中營(yíng)的,非常感謝。

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

效果圖

 

 

 

 

用到的主流框架

  • 首頁(yè)側(cè)滑欄使用DrawerLayout+NavigationView實(shí)現(xiàn)的
  • 使用Realm數(shù)據(jù)庫(kù)實(shí)現(xiàn)本地收藏
  • 使用Retrofit+RxJava+RxAndroid實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求,并對(duì)返回結(jié)果進(jìn)行了簡(jiǎn)單的封裝
  • 對(duì)RecyclerView的Adapter和ViewHolder進(jìn)行封裝,實(shí)現(xiàn)了上拉加載
  • 使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout實(shí)現(xiàn)了炫酷的滑動(dòng)動(dòng)畫
  • 使用Glide實(shí)現(xiàn)了圖片的加載
  • 使用PhotoView實(shí)現(xiàn)了圖片的縮放
  • 日歷使用開源的material-calendarview
  • 實(shí)現(xiàn)了SwipeRefreshLayout首次進(jìn)入自動(dòng)刷新

一、使用DrawerLayout+NavigationView實(shí)現(xiàn)側(cè)滑欄 

  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控制側(cè)滑欄展示與隱藏。

  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之后推出的一個(gè)控件,主要作為菜單控件使用,分為上下部分,上面的部分為headerLayout,可以自定義布局,下面的部分為menu,作為導(dǎo)航菜單的菜單項(xiàng) 

  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="關(guān)于" /></menu>  

點(diǎn)擊事件:

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

獲取頭部(headerLayout)內(nèi)控件:

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

設(shè)置菜單列表圖標(biāo)顏色:

默認(rèn)情況下,菜單圖標(biāo)顏色為灰色,可以通過一下設(shè)置圖標(biāo)顏色

  1. app:itemIconTint="" 

添加分割線:

只需將菜單分成多個(gè)Group,每個(gè)Group設(shè)置一個(gè)Id,那么Group之間就會(huì)有分割線: 

  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="關(guān)于"/> 
  22. </menu>  

二、Glide加載圖片

設(shè)置綁定生命周期

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

常規(guī)用法: 

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

加載Git圖片:

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

動(dòng)態(tài)緩存清理:

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

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

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

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

圓角轉(zhuǎn)換: 

  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.  

圓形圖片轉(zhuǎn)換: 

  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.                 });  
責(zé)任編輯:龐桂玉 來源: 安卓巴士Android開發(fā)者門戶
相關(guān)推薦

2021-12-07 10:23:27

鴻蒙HarmonyOS應(yīng)用

2016-12-28 13:55:16

Android框架MVP

2014-12-16 10:11:22

2020-12-03 09:00:02

Java外賣系統(tǒng)

2020-12-07 11:50:14

Java學(xué)習(xí)系統(tǒng)eclipse

2015-08-18 09:11:34

杜長(zhǎng)偉APP

2020-11-05 09:27:48

JavaScript開發(fā)技術(shù)

2022-02-17 10:26:17

JavaScript掃雷游戲前端

2021-08-03 12:47:58

鴻蒙HarmonyOS應(yīng)用

2014-06-20 10:32:42

APP上癮設(shè)計(jì)

2013-07-16 10:09:15

2021-12-30 08:56:57

Python摸魚倒計(jì)界面Python基礎(chǔ)

2021-11-01 10:26:07

CanvasAPI畫布技術(shù)HTML5

2020-05-11 13:40:48

編程新冠App

2022-03-04 09:05:55

StarRocks數(shù)據(jù)湖數(shù)據(jù)質(zhì)量

2021-11-17 15:36:04

鴻蒙HarmonyOS應(yīng)用

2015-11-27 09:18:11

AngularJSWeb應(yīng)用

2020-03-12 09:20:41

微軟瀏覽器Windows

2019-05-06 11:49:10

DomTerm終端模擬器Linux

2017-01-13 08:37:57

PythonAlphaGoMuGo
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)