1. 程式人生 > >kotlin和java混合開發專案,多模組,分模組開發,分module,元件化開發

kotlin和java混合開發專案,多模組,分模組開發,分module,元件化開發


# picture_dx #歡迎能來到這裡

客服機器人美洽第三方完美接入魔窗

 

該demo用了元件化

為什麼要元件化

近年來,為什麼這麼多團隊要進行元件化實踐呢?元件化究竟能給我們的工程、程式碼帶來什麼好處?我們認為元件化能夠帶來兩個最大的好處:

提高元件複用性

可能有些人會覺得,提高複用性很簡單,直接把需要複用的程式碼做成Android Module,打包AAR並上傳程式碼倉庫,那麼這部分功能就能被方便地引入和使用。但是我們覺得僅僅這樣是不夠的,上傳倉庫的AAR庫是否方便被複用,需要元件化的規則來約束,這樣才能提高複用的便捷性。

降低元件間的耦合

我們需要通過元件化的規則把程式碼拆分成不同的模組,模組要做到高內聚、低耦合。模組間也不能直接呼叫,這需要元件化通訊框架的支援。降低了元件間的耦合性可以帶來兩點直接的好處:第一,程式碼更便於維護;第二,降低了模組的Bug率。

應用
追求的理念;

APK包大小越小越好
應用開啟耗費資源少
UI不是圖產品越炫酷越好還要考慮效能及對應的影響
如以前加了個功能開啟PDF加了個控制元件APK直接加大了6M(上一版本12M,增加50%相當於一個應用程式,對使用者什麼感覺直接就不下載了)
一個提示可以解決的就別整個彈框(開發寫出來就分分鐘的事),但是程式碼量增加,程式碼是一個字母一個字母,一個位元組位元組堆起來的,會讓APK無端端的增大,知道而不原因
關係著重點搞清楚:應用這個是為了什麼,功能還是UI還是效果為了UI效果耗時耗力耗財功能影響了  

##該專案演示原本計劃,及產生過程:
##本來我是用來練習kotlin的語法

  • **寫了幾個類練練手。
  •             然後是分模組(1個主工程6個依賴模組)來寫的,後面慢慢加了好多功能。所以提交上來了,以便記錄和學習。**

![影象](https://github.com/1136346879/picture_dx/blob/master/image_flod/project.jpg)

##加入recycleview列表資料,
                包含下拉重新整理,上拉載入更多,
                網路訪問是OKGO,(該功能已經封裝好了,零蟲,本計劃單另一個模組,由於需要引用一個罐子包所以放在了掃一掃目錄下,recycleview資料夾下,拉出來就可以直接使用)

        列表載入,重新整理,載入更多
![影象](https://github.com/1136346879/picture_dx/blob/master/image_flod/loadingview_refresh_loadmore.gif) 

  1. ## RecycleView列表帶吸附的標題純手工打造原始碼可見原理是在定義StickyItemDecoration繼承RecyclerView.ItemDecoration 
  2. ##資料庫引用操作,生成表格後增留改查(GreenDao)

    如何引入,如何使用,在程式碼中已經寫得很清楚了,可下載程式碼檢視。

上方標號與下方一至:
##分模組(多模組)編寫的專案目錄,解耦性大,任意一模組都可以隨意拆卸,安裝不影響其他模組內容

  • > * 1,主工程:App(**包含zxing掃一掃功能,檢視拖拽,**)
  • > * 2,基礎類庫:BaseLibrary
  • > * 3,測試類練市搭建:myInstalledPkg
  • > * 4,附屬類庫:提供商
  • > * 5,掃一掃功能:qrode-module(直接可以引入該模組)


 
![圖片](https://github.com/1136346879/picture_dx/blob/master/image_flod/StickyRec.gif)

 

[^程式碼]

  1.     實現'com.google.zxing:核心:3.3.0'
  2.     實現'com.google.zxing:android-core:3.3.0'
  • > * 7,個人中心:usercenter
  • > * 6,圖片裁剪 

    :使用者頭像展示裁剪,圖片搜尋功能(如淘寶,京東,百度等搜尋)
    
![圖片](https://github.com/1136346879/picture_dx/blob/master /image_flod/zxing_saoyisao.gif)

[^程式碼]

    / **
     *去裁剪直接寫這個方法即可  
     * @param originalPath
     * /
    protected fun startCrop(originalPath:String){
        val options = UCrop.Options()
        val toolbarColor = AttrsUtils.getTypeValueColor(this,-1)
        val statusColor = AttrsUtils.getTypeValueColor(this,-1)
        val titleColor = AttrsUtils.getTypeValueColor(this,-1)
        options.setToolbarColor(toolbarColor)//狀態列背景
        options.setStatusBarColor(statusColor)
        options.setToolbarWidgetColor(titleColor)
        options.setCircleDimmedLayer(false)
        options.setShowCropFrame(真)//顯示裁剪框
        options.setShowCropGrid(真)//顯示裁剪框網格
        options.setDragFrameEnabled(true)//裁剪框拖拖
        options.setScaleEnabled(true)//圖片縮放
        options.setRotateEnabled(true)//圖片旋轉
        options.setCompressionQuality(70)//圖片質量
        options.setHideBottomControls(true)
        選項。 setFreeStyleCropEnabled(true)//裁剪
        val isHttp = false
        val imgType = PictureMimeType.getLastImgType(originalPath)
        val uri = if(isHttp)Uri.parse(originalPath)else Uri.fromFile(File(originalPath))
        UCrop.of(uri,Uri .fromFile(File(getDiskCacheDir(this),System.currentTimeMillis()。toString()+“”+ imgType)))。
                withAspectRatio(0F,0F)
                .withMaxResultSize(0,0)
                .withOptions(options).start(
                this)
// finish()
    }
##小心看圖

![影象](https://github.com/1136346879/picture_dx/blob/master/image_flod/suibiancaijian.gif)

        CropImageActivity按正方形裁切圖片
        用於app頭像裁剪,使用者影象裁剪
            (參照該類即可)

            


![圖片](https://github.com/1136346879/picture_dx/blob/master/image_flod/squre_crop.gif)  

                
        
# #圖片上傳
##網路訪問用的是 

 [^ code]

 com.squareup.retrofit2:
         retrofit:$ retrofitVersion “ com.squareup.retrofit2:adapter-rxjava2:$ retrofitVersion”)
    
        / **
         *圖片路徑傳過來
         *
         * /
        private fun loadUpImg(path:String){
        val httpManger = HttpManager。 instance()
    // val file = File(BitmapUtils.compressImageUpload(path))
        val file = File(path)
        val requestFile = RequestBody.create(MediaType.parse(“multipart / form-data”),file)
        val body = MultipartBody .Part.createFormData(“file”,file.name,requestFile)
        httpManger.doHttpDeal(this,httpManger.createService(OrderService :: class.java)!!。pictureSearch(body),
            object:HttpOnNextListener(){
                覆蓋fun onNext(json:String){
                ToastUtilKt.showCustomToast(“Image upload sucessed:$ json”)
                }
                覆蓋fun onError(statusCode:Int,apiErrorModel:ApiErrorModel?){
                super.onError(statusCode,apiErrorModel) )
                ToastUtilKt.showCustomToast(“圖片上傳失敗”)
                }
            },false)
        }    
        
#//記憶體洩漏檢測((ThirdPartBannerZxingAcitivity)開啟掃一掃即可觀察到現象,因類中包含了多個靜態變數)
   掃一掃(自定義仿照微信介面)

    實現'com.google.zxing:core:3.3.0'
    實現'com.google.zxing:android-core:3.3.0'
    
![影象](https://github.com/1136346879/picture_dx/blob/master/image_flod/zxing.gif)
[

^程式碼]
    debugImplementation 'com.squareup.leakcanary:leakcanary-機器人:1.6.1'
    releaseImplementation“COM .squareup.leakcanary:leakcanary-android-no-op:1.6.1'
    點選到相應的頁面之後,如存在記憶體洩漏,該幫助類就會顯示出來
    
##會首先顯示在通知欄中

![影象](https://github.com/1136346879/picture_dx/blob/master/image_flod/leaks3.png)

##通知欄可以點選顯示詳情

![影象](https://github.com/1136346879/picture_dx/blob/master/image_flod/leaks2.png)

##最後在洩漏這個app裡面可以看到相應的記憶體洩漏詳情

![影象](https://github.com/1136346879/picture_dx/blob/master/image_flod/leaks1.png)

##圖片相關問題(顯示裁剪壓縮輪播上傳等)

    ##系統裁剪圖片呼叫

  1.     ###(1)進入相簿

    [^ code]
    
    (許可權rxpremission com.github.tbruyelle:rxpermissions
         val intent = Intent(Intent.ACTION_PICK,null)
            //如果朋友們要限制上傳到伺服器的圖片型別時可以直接寫如:image / jpeg,image / png等的型別
            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,“image / *”)
            startActivityForResult(intent,UCROP_SELECT_PHOTO)

  1.     ###(2)在頁面onActivityResult回撥中,系統-呼叫裁剪 

    優點:穩定性高,無BUG,利於應用
    缺點:裁剪頁面的UI不可以修改是固定的
    
![影象](https://github.com /1136346879/picture_dx/blob/master/image_flod/system_crop.gif)  

    呼叫系統裁剪
    [^ code]
    
          private fun startPhotoZoom(data:Uri?,picWith:Int,picHeight:Int){
            val intent = Intent(“com.android.camera.action.CROP”)
            intent.setDataAndType(data,“image / *“)
            // crop為true是設定在開啟的intent中設定顯示的檢視可以剪裁
            intent.putExtra(”crop“,”true“)
            // aspectX aspectY是寬高的比例
            intent.putExtra(”aspectX“ ,1)
            intent.putExtra(“aspectY”,1.4)
            // outputX,outputY是剪裁圖片的寬高
            intent.putExtra(“outputX”,picWith)
            intent.putExtra(“outputY”,picHeight)
            intent.putExtra(“scale” ”true)
            photoUri = getImageUri()
            intent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri)
            intent.putExtra(“return-data”,false)
            intent.putExtra(“outputFormat”,Bitmap.CompressFormat.JPEG)
            intent.putExtra(“noFaceDetection”,true)//沒有臉檢測
            Log.e(“TAKE_PHOTO”,“TAKE_PHOTO-zoom-CROP_PHOTO”)
            startActivityForResult(intent,CROP_PHOTO)
          }
          
    ###(3)再在回撥onActivityResult中處理裁剪後的邏輯
            如上傳,儲存等。
##圖片輪播用了兩種寫法:(第三方和自定義)
    ###(ThirdPartBannerZxingAcitivity)第三方//輪播圖(新聞列表頭部展示)

            
![image](https://github.com/1136346879/picture_dx /blob/master/image_flod/lubotu.gif)
              

 設定資料就可以  

[^ code]        
                實現'com.youth.banner:banner:1.4.10'
                使用也很簡單:homeBanner //設定banner
                樣式.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
                //設定圖片載入器
                .setImageLoader(GlideImageLoader())
                //設定圖片集合
                .setImages(list_path)
                //設定banner動畫效果
                .setBannerAnimation(Transformer.Default)
                //設定標題集合(當banner樣式有顯示標題時)
                .setBannerTitles(list_title)
                //設定輪播時間
                .setDelayTime(1500)
                //設定指示器位置(當banner模式中有指示器時)
                .setIndicatorGravity(BannerConfig.CENTER)
                // banner設定方法全部呼叫完畢時最後呼叫.start
                ()
                
    ###(CustomerBannerActivityJava)自定義的ImageBanner(圖片載入用的picasso)直接

           
![圖片](https://github.com/ 1136346879 / picture_dx /斑點/主/ image_flod / imageBannergif.gif)

   [^ code]
              imageBanner.setList(imageArray,imageTitle);
              
              
CustomerBannerActivityJava該類中加入自定義loadingview **動畫及圖案純手動打造**
                    
![圖片](https://github.com/1136346879/picture_dx/blob/master/image_flod/loadingview.gif)


自定義檢視拖拽功能,(DragView)懸浮在頁面之上,自動吸附在兩側,可以點選

![影象](https://github.com/1136346879/picture_dx/blob/master/image_flod/dragview.gif)

日曆(完全自定義,可以編寫相應日期,新增日程)
該頁面用半透明風格可以模糊看到後面的頁面

[^ code]
        
    類整合活動風格風格(日曆頁面應用)
     <style name =“Transparent”>
            <item name =“android:windowBackground”> @ color / transparent_background </ item>
            <item name =“android:windowNoTitle”> true </ item>
            <item name =“android:windowIsTranslucent”> true </ item>
        </ style>
        
    類整合RxAppCompatActivity style風格(
    樣式名稱=“TranslucentActivityTheme”parent =“Theme.AppCompat.Light。 NoActionBar“>
        <item name =”android:windowBackground“> @ android:color / transparent </ item>
        <item name =”android:colorBackgroundCacheHint“> @ null </ item>
        <item name =“android:windowIsTranslucent”> true </ item>
        <item name =“android:windowAnimationStyle”> @ android:style / Animation </ item>
        <item name =“android:windowContentOverlay”> @ null </ item>
    </ style>
    
![image](https:// github的.com / 1136346879 / picture_dx /斑點/主/ image_flod / carlanderDate.gif)


##新增資料庫GreenDao頁面,包含完整邏輯,增加改查。相應的資料可以在日誌日誌頁面檢視

##多種狀態切換的檢視(loadingview,錯誤,空,內容)
[^ code]

        在佈局檔案中引入
        <com.classic.common.MultipleStatusView
            android:id =“@ + id / multipleStatusView”
            android:layout_width =“match_parent”
            android:layout_height =“match_parent”
            app:emptyView =“@ layout / layout_empty_view”(傳入相應佈局)
            app:errorView =“@ layout / layout_error_view”(傳入相應佈局)
            app:loadingView =“@ layout / layout_loading_view”(傳入相應佈局)
            app:noNetworkView =“@ layout / layout_network_view”>(傳入相應佈局)
            
            <LinearLayout
                android:focusable =“true”
                android:focusableInTouchMode =“true”
                android:layout_width =“match_parent”
                android:layout_height =“match_parent”
                android:orientation =“vertical”>
            </ LinearLayout>
        </com.classic.common.MultipleStatusView>
        
        在程式碼中
            //展示內容
            mLayoutStatusView?.showContent()
            //展示空內容
            mLayoutStatusView?。 showEmpty()
            //展示錯誤頁面
            mLayoutStatusView?.showError()
        //錯誤頁面點選重新載入
        mLayoutStatusView?.setOnClickListener {ToastUtilKt.showToast(“點選重新載入”)}


## **安卓高階開發交流群:qq 335042824 **
demo(原始碼)地址:https:    //github.com/1136346879/picture_dx