《Kotlin系列》之基于协程、Flow、MVVM开发一套相册查询器,PhotoPicker、仿微信相册选择

先看看效果:

前言

由于相册查询会使一个持续的过程,所以基于协程+ViewModel 开发的话,会省去很多生命周期的管理,防止内存泄漏和一些引用长持的问题。而Flow 替换RxJava 是由于Flow 的背压模式强于RxJava,也更方便简洁,而且可以对上流和下流管道切换不同的线程。

整个开发逻辑包含以下功能部分

  • 1、列表查询
  • 2、标题列表查询
  • 3、大图预览
  • 4、大图下拉关闭图片返回列表位置
一、列表查询
    /**
     * 加载列表数据
     *
     * @param context
     * @param selectionArgsName
     * @param loadType
     * @param id
     */
    @OptIn(DelicateCoroutinesApi::class)
    @SuppressLint("Range")
    private fun loadListData(
        context: Context?,
        selectionArgsName: String?,
        loadType: Int,
        id: String
    ) {


        if (selectionArgsName != null && selectionArgsName != this.selectionArgsName) {
            albumLoaderBuilder.callBack?.clearData()
            allData.clear()
            isRunning = true
        }
        this.selectionArgsName = selectionArgsName
        this.context = context
        this.loadType = loadType
        this.id = id
        album.clear()
        latelyList.clear()
        allList.clear()

        loaderStatus = LoaderStatus.LOADING
        launch = GlobalScope.launch(Dispatchers.IO) {

            val mCursor = getCursor(selectionArgsName, id, context, loadType)
            var size = 0
            var parentFile: File? = null
            if (mCursor != null) {
                count = mCursor.count
                val size1 = allData.size - 1
                mCursor.moveToPosition(size1)

                while (mCursor.moveToNext() && isRunning && size < albumLoaderBuilder.pageSize) {
                    val path =
                        mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA))
                    if (!isImageFile(path)) {
                        continue
                    }
                    val file = File(path)
                    //如果默认是相机,过滤掉别的图片,获取相机图片
                    val displayName =
                        mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME))
                    parentFile = File(path).parentFile // 获取该图片的父路径名
                    var width =
                        mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media.WIDTH))
                    var height =
                        mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media.HEIGHT))
                    val rotation = Utils.readPictureDegree(path)
                    if (rotation == 90 || rotation == 270) {
                        width =
                            mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media.HEIGHT))
                        height =
                            mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media.WIDTH))
                    }
                    val galleryInfoEntity = GalleryInfoEntity()
                        .setImgName(displayName)
                        .setImgPath(path)
                        .setImgWidth(width)
                        .setImgHeight(height)
                        .setDisplayName(selectionArgsName)
                        .setDisplayId(id)
                    var pdf = 0 //判断此图片所属的相册是否已存在,pdf为0表示图片所属的相册还不存在
                    for (i in album.indices) {
                        if (album[i].dirPath.equals(parentFile.absolutePath, ignoreCase = true)) {
                            pdf = 1
                            album[i].galleryInfo
                            album[i].galleryInfo?.add(galleryInfoEntity)
                        }
                    }
                    if (pdf == 0) {
                        val list = CopyOnWriteArrayList<GalleryInfoEntity>()
                        list.add(galleryInfoEntity)
                        album.add(
                            AlbumInfoEntity()
                                .setAlbumName(parentFile.name)
                                .setDirPath(parentFile.absolutePath)
                                .setGalleryInfo(list)
                        )
                    }
                    if (albumLoaderBuilder.isShowLastModified) {
                    }
                    if (file.lastModified() >= Utils.stateTime) {
                        latelyList.add(galleryInfoEntity)
                    }
                    allList.add(galleryInfoEntity)
                    size++
                }
                if (parentFile != null) {
                    album.add(
                        0, AlbumInfoEntity()
                            .setAlbumName("全部图片")
                            .setDirPath(parentFile.absolutePath)
                            .setGalleryInfo(allList)
                            .setAll(true)
                    )
                    if (latelyList.size > 0) {
                        album.add(
                            AlbumInfoEntity()
                                .setAlbumName("最近照片")
                                .setDirPath(parentFile.absolutePath)
                                .setGalleryInfo(latelyList)
                                .setAll(true)
                        )
                    }
                }
                withContext(Dispatchers.Main) {

                    setData(
                        allList
                    )

                }
            }

        }
    }

二、标题列表查询
  job = GlobalScope.launch(Dispatchers.Main) {

            flow {
                emit(data)
            }.map {
                scanAllAlbumData(it)
            }.flowOn(Dispatchers.IO).collect {
                Log.i("THREAD", "" + (Thread.currentThread() == Looper.getMainLooper().thread))
                mAlbumDataReceiver?.onAlbumDataObserve(it)
            }
        }
 
  private fun scanAllAlbumData(cursor: Cursor): List<AlbumData?> {
        val albumDataList: MutableList<AlbumData?> = ArrayList()
        while (isCursorEnable(cursor) && cursor.moveToNext()) {
            val albumData = AlbumData.valueOf(cursor, mContext)
            albumDataList.add(albumData)
        }
        return albumDataList
    }
三、大图预览

主要使用的是viewpager2 去做的

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewpager_horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:background="@color/color_010101"
            android:orientation="horizontal"
            app:bindPageListener="@{model.pagerListener}"
            app:bindViewPagerAdapter="@{model.previewAdapter}"
            tools:ignore="MissingConstraints" />

      try {
            if (DataBindingUtil.getBinding<ItemPreviewLayoutBinding?>(holder.itemView) == null) {
                ItemPreviewLayoutBinding.bind(holder.itemView)
            }

            val binding = holder.getBinding<ItemPreviewLayoutBinding>()
            binding?.model = model

            binding?.photoView?.scaleType=ImageView.ScaleType.FIT_XY



            binding?.photoView?.let {
                Glide.with(context)
                    .asBitmap()
                    .load(item.imgPath)
                    .dontAnimate()
                    .fitCenter()
                    .placeholder(binding.photoView.drawable)
                    .into(it)
            }

            Log.e("TAG","IMG PATH"+item.imgPath)
        } catch (e: Exception) {
        }

四、大图下拉关闭图片返回列表位置**

1、监听滑动后,每个列表的高度,计算出Rect

    inner class PageListener : ViewPager2.OnPageChangeCallback() {

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            val data = previewAdapter?.get()?.data
            currentPosition = position
            data?.let {
                val galleryInfoEntity = data[position]
                previewCheckStatus?.set(galleryInfoEntity.isSelected)

                val data1 = adapter?.get()?.data
                data1?.let {
                    val indexOf = it.indexOf(galleryInfoEntity)
                    Log.i(TAG, "the index of=$indexOf")
                    if (indexOf != -1 && data.size > indexOf) {
                        val viewByPosition = adapter?.get()?.getViewByPosition(indexOf, R.id.img)
                        viewByPosition?.getGlobalVisibleRect(rect)
                        viewByPosition?.height?.let {it1->
                            itemHeight=it1
                        }

                        //设置最小缩放
                    }
                }
            }


        }


    }

2、拖动松手后,计算当前图片的中心的X\Y 以及图片列表小图片的中心点X\Y

 private fun performExitAnimation(
        view: DragPhotoView,
        x: Float,
        y: Float,
        w: Float,
        h: Float,
        scale: Float
    ) {
        view.finishAnimationCallBack()

        val array = IntArray(2) { 0 }


        val mOriginLeft = rect.left
        val mOriginTop = rect.top
        val mOriginHeight = rect.height()
        val mOriginWidth = rect.width()
        val mOriginCenterX = mOriginLeft + mOriginWidth / 2
        val mOriginCenterY = mOriginTop + mOriginHeight / 2


        Log.i(TAG, "mOriginTop=$mOriginTop")
        val currentWidth = w * scale
        val currentHeight = h * scale




        view.getLocationInWindow(array)
        val viewX: Float = w / 2 + x - currentWidth / 2 + array[0]
        val viewY: Float = h / 2 + y - currentHeight / 2 + array[1]



        view.x = viewX
        view.y = viewY


        view.setOutLine(itemHeight,mOriginWidth)




        val centerX = view.x + mOriginWidth / 2
        val centerY = view.y + mOriginHeight / 2

        val translateX = mOriginCenterX - centerX
        val translateY = mOriginCenterY - centerY


        val animatorSet = AnimatorSet()
        animatorSet.duration = 400
        animatorSet.interpolator = DecelerateInterpolator()


        val translateXAnimator: ValueAnimator = ValueAnimator.ofFloat(view.x, view.x + translateX)
        translateXAnimator.addUpdateListener { valueAnimator ->
            view.x = (valueAnimator.animatedValue as Float)
        }

        val translateYAnimator: ValueAnimator =
            ValueAnimator.ofFloat(view.y, view.y + translateY)
        translateYAnimator.addUpdateListener { valueAnimator ->
            view.y = (valueAnimator.animatedValue as Float)

        }



        animatorSet.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animator: Animator?) {}
            override fun onAnimationEnd(animator: Animator) {


                animator.removeAllListeners()
                leftFinish()
            }

            override fun onAnimationCancel(animator: Animator?) {}
            override fun onAnimationRepeat(animator: Animator?) {}
        })

        animatorSet.playTogether(translateXAnimator, translateYAnimator)
        animatorSet.start()
    }

上诉都是贴纸关键核心代码,具体逻辑可以去fork github 代码看看。

https://github.com/ljlstudio/wechat_gallery

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 首先,Kotlin 是一种基于 JVM 的静态类型编程语言,它的语法简洁易懂,支持函数式编程和面向对象编程。 协程Kotlin 中的一种轻量级线程,可以实现异步编程和并发执行。Retrofit 是一款网络请求库,它可以帮助我们轻松地实现网络请求和数据解析。MVVM 是一种软件架构模式,可以将应用程序分为三个部分:模型、视图和视图模型。 下面是一个基于 Kotlin + 协程 + Retrofit + MVVM 的网络请求的优雅实现: 1. 定义 API 接口 首先定义 API 接口,使用 Retrofit 注解来描述请求方法和参数。 ```kotlin interface ApiService { @GET("api/news") suspend fun getNews(@Query("category") category: String): NewsResponse } ``` 2. 创建数据模型 根据 API 接口的返回数据,我们可以创建一个数据模型。 ```kotlin data class News(val title: String, val content: String) data class NewsResponse(val code: Int, val message: String, val newsList: List<News>) ``` 3. 创建 ViewModel ViewModel 是连接数据模型和视图的中间层,它处理数据逻辑并提供可观察的数据。 ```kotlin class NewsViewModel : ViewModel() { private val _newsList = MutableLiveData<List<News>>() val newsList: LiveData<List<News>> = _newsList fun loadNews(category: String) { viewModelScope.launch { val response = retrofit.create(ApiService::class.java).getNews(category) if (response.code == 200) { _newsList.value = response.newsList } } } } ``` 4. 创建视图 视图负责展示数据,并与用户交互。 ```kotlin class NewsActivity : AppCompatActivity() { private val viewModel by viewModels<NewsViewModel>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_news) viewModel.newsList.observe(this, Observer { newsList -> // 更新视图 }) viewModel.loadNews("tech") } } ``` 通过使用 Kotlin + 协程 + Retrofit + MVVM,我们可以实现优雅地网络请求,代码简洁易懂,逻辑清晰。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值