From b67559bde35fcfe14c5507c81e10f6b47f0f89ca Mon Sep 17 00:00:00 2001 From: Yenaly <2021214976@stu.cqupt.edu.cn> Date: Mon, 25 Jul 2022 19:13:24 +0800 Subject: [PATCH] add PhotoView. --- .idea/misc.xml | 3 + README.md | 11 +++ app/build.gradle.kts | 1 + app/src/main/AndroidManifest.xml | 9 +++ .../java/com/yenaly/duanzile/Constants.kt | 2 + .../duanzile/ui/activity/PicViewActivity.kt | 71 +++++++++++++++++++ .../duanzile/ui/adapter/DuanziRvAdapter.kt | 8 +++ .../duanzile/ui/adapter/PicViewVpAdapter.kt | 65 +++++++++++++++++ .../ui/adapter/SimpleDuanziRvAdapter.kt | 12 +++- .../duanzile/ui/viewmodel/PicViewViewModel.kt | 14 ++++ .../drawable/ic_baseline_arrow_back_24.xml | 5 ++ app/src/main/res/layout/activity_pic_view.xml | 69 ++++++++++++++++++ app/src/main/res/layout/item_pic_view.xml | 20 ++++++ app/src/main/res/values/colors.xml | 3 + app/src/main/res/values/strings.xml | 1 + 15 files changed, 291 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/yenaly/duanzile/ui/activity/PicViewActivity.kt create mode 100644 app/src/main/java/com/yenaly/duanzile/ui/adapter/PicViewVpAdapter.kt create mode 100644 app/src/main/java/com/yenaly/duanzile/ui/viewmodel/PicViewViewModel.kt create mode 100644 app/src/main/res/drawable/ic_baseline_arrow_back_24.xml create mode 100644 app/src/main/res/layout/activity_pic_view.xml create mode 100644 app/src/main/res/layout/item_pic_view.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index ec7cbcb..2154910 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,6 +5,7 @@ + @@ -13,6 +14,7 @@ + @@ -26,6 +28,7 @@ + diff --git a/README.md b/README.md index 2461761..0b3f68e 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,17 @@ 段子乐第三方**考核用**APP,项目本身全部采用 Kotlin 编写,标准使用 MVVM 架构,Material 3 最新视觉标准,适配 Android 12 动态颜色模式,最低支持 Android 5(API 21)。 +## 使用库 + +1. `Flow`(切换线程,网络数据分发,ViewModel 缓存) +2. `Retrofit`(网络请求) +3. `Jetpack`(基本界面) +4. `Glide`(展示图像) +5. `Jzvd`(展示视频) +6. `PhotoView`(展示图像) +7. `Paging`(分页加载) +8. `SwipeRefreshLayout`(滑动刷新布局) + ## 截图 - 主页一览 diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 95544ad..80c93ea 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -64,6 +64,7 @@ dependencies { implementation("androidx.paging:paging-runtime:3.1.1") implementation("androidx.paging:paging-runtime-ktx:3.1.1") implementation("cn.jzvd:jiaozivideoplayer:7.7.0") + implementation("com.github.chrisbanes:PhotoView:2.3.0") annotationProcessor("com.github.bumptech.glide:compiler:4.13.2") testImplementation("junit:junit:4.13.2") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bcca3ee..149418c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -66,6 +66,15 @@ tools:ignore="LockedOrientationActivity"> + + + + diff --git a/app/src/main/java/com/yenaly/duanzile/Constants.kt b/app/src/main/java/com/yenaly/duanzile/Constants.kt index 6c214e7..7b96c66 100644 --- a/app/src/main/java/com/yenaly/duanzile/Constants.kt +++ b/app/src/main/java/com/yenaly/duanzile/Constants.kt @@ -17,3 +17,5 @@ const val TO_TEXT_VIDEO_SPLIT_FRAGMENT = "TO_TEXT_VIDEO_SPLIT_FRAGMENT" const val TO_DUANZI_ACTIVITY_ID = "TO_DUANZI_ACTIVITY_ID" const val TO_FOLLOW_FAN_ACTIVITY_ID = "TO_FOLLOW_FAN_ACTIVITY_ID" + +const val TO_PIC_VIEW_URLS = "TO_PIC_VIEW_URLS" \ No newline at end of file diff --git a/app/src/main/java/com/yenaly/duanzile/ui/activity/PicViewActivity.kt b/app/src/main/java/com/yenaly/duanzile/ui/activity/PicViewActivity.kt new file mode 100644 index 0000000..bbb75a5 --- /dev/null +++ b/app/src/main/java/com/yenaly/duanzile/ui/activity/PicViewActivity.kt @@ -0,0 +1,71 @@ +package com.yenaly.duanzile.ui.activity + +import android.os.Bundle +import android.widget.LinearLayout +import androidx.viewpager2.widget.ViewPager2 +import com.yenaly.duanzile.R +import com.yenaly.duanzile.TO_PIC_VIEW_URLS +import com.yenaly.duanzile.databinding.ActivityPicViewBinding +import com.yenaly.duanzile.ui.adapter.PicViewVpAdapter +import com.yenaly.duanzile.ui.viewmodel.PicViewViewModel +import com.yenaly.yenaly_libs.base.YenalyActivity +import com.yenaly.yenaly_libs.utils.SystemStatusUtil +import com.yenaly.yenaly_libs.utils.intentExtra +import com.yenaly.yenaly_libs.utils.statusBarHeight + +/** + * @ProjectName : BlViewer + * @Author : Yenaly Liew + * @Time : 2022/06/01 001 22:59 + * @Description : Description... + */ +class PicViewActivity : YenalyActivity() { + + private lateinit var picViewAdapter: PicViewVpAdapter + private val position = 0 + private val urlsList by intentExtra(TO_PIC_VIEW_URLS, emptyList()) + + override fun setUiStyle() { + SystemStatusUtil.fullScreen(window, true) + } + + override fun initData(savedInstanceState: Bundle?) { + + overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out) + + picViewAdapter = PicViewVpAdapter(urlsList) + + binding.pvaViewPager.adapter = picViewAdapter + binding.pvaViewPager.setCurrentItem(position, false) + val emptyLayoutParams = + LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, statusBarHeight) + binding.emptyView.layoutParams = emptyLayoutParams + + val totalPage = urlsList.size + binding.picPosition.text = getString(R.string.pic_view_position, position + 1, totalPage) + binding.pvaViewPager.registerOnPageChangeCallback( + object : ViewPager2.OnPageChangeCallback() { + override fun onPageSelected(position: Int) { + super.onPageSelected(position) + binding.picPosition.text = + getString(R.string.pic_view_position, position + 1, totalPage) + } + } + ) + + binding.btnBack.setOnClickListener { + finish() + overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out) + } + } + + override fun onBackPressed() { + super.onBackPressed() + overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out) + } + + override fun onNavigateUp(): Boolean { + overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out) + return super.onNavigateUp() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/yenaly/duanzile/ui/adapter/DuanziRvAdapter.kt b/app/src/main/java/com/yenaly/duanzile/ui/adapter/DuanziRvAdapter.kt index d08aadf..dca7518 100644 --- a/app/src/main/java/com/yenaly/duanzile/ui/adapter/DuanziRvAdapter.kt +++ b/app/src/main/java/com/yenaly/duanzile/ui/adapter/DuanziRvAdapter.kt @@ -18,6 +18,7 @@ import com.yenaly.duanzile.ftpDecrypt import com.yenaly.duanzile.logic.model.DuanziListModel import com.yenaly.duanzile.ui.activity.DuanziActivity import com.yenaly.duanzile.ui.activity.MainActivity +import com.yenaly.duanzile.ui.activity.PicViewActivity import com.yenaly.duanzile.ui.activity.UserActivity import com.yenaly.yenaly_libs.utils.activity import com.yenaly.yenaly_libs.utils.shareText @@ -229,6 +230,13 @@ class DuanziRvAdapter : ) } } + viewHolder.binding.image.setOnClickListener { + val position = viewHolder.bindingAdapterPosition + val item = getItem(position) + item?.let { + startActivity(TO_PIC_VIEW_URLS to listOf(item.joke.imageURL.ftpDecrypt())) + } + } } return viewHolder diff --git a/app/src/main/java/com/yenaly/duanzile/ui/adapter/PicViewVpAdapter.kt b/app/src/main/java/com/yenaly/duanzile/ui/adapter/PicViewVpAdapter.kt new file mode 100644 index 0000000..706264f --- /dev/null +++ b/app/src/main/java/com/yenaly/duanzile/ui/adapter/PicViewVpAdapter.kt @@ -0,0 +1,65 @@ +package com.yenaly.duanzile.ui.adapter + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.view.isInvisible +import androidx.core.view.isVisible +import androidx.recyclerview.widget.RecyclerView +import com.bumptech.glide.Glide +import com.yenaly.duanzile.R +import com.yenaly.duanzile.databinding.ItemPicViewBinding +import com.yenaly.yenaly_libs.utils.SystemStatusUtil +import com.yenaly.yenaly_libs.utils.activity +import com.yenaly.yenaly_libs.utils.setSystemBarIconLightMode + +/** + * @project Duanzile + * @author Yenaly Liew + * @time 2022/07/25 025 18:32 + */ +class PicViewVpAdapter(private val data: List) : + RecyclerView.Adapter() { + + private var recyclerView: RecyclerView? = null + + private val context: Context + get() { + checkNotNull(recyclerView) { + "You have not attached adapter to RecyclerView!" + } + return recyclerView!!.context + } + + override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { + super.onAttachedToRecyclerView(recyclerView) + this.recyclerView = recyclerView + } + + inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { + val binding = ItemPicViewBinding.bind(view) + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + val view = + LayoutInflater.from(parent.context).inflate(R.layout.item_pic_view, parent, false) + val viewHolder = ViewHolder(view) + val activity = context.activity ?: throw NullPointerException("???") + val fullToolbar: View = activity.findViewById(R.id.pva_full_toolbar) + viewHolder.binding.photoView.setOnPhotoTapListener { _, _, _ -> + fullToolbar.isInvisible = fullToolbar.isVisible + SystemStatusUtil.fullScreen(activity.window, true, true) + activity.window.setSystemBarIconLightMode(fullToolbar.isInvisible) + } + return viewHolder + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + Glide.with(context).load(data[position]).into(holder.binding.photoView) + } + + override fun getItemCount(): Int { + return data.size + } +} \ No newline at end of file diff --git a/app/src/main/java/com/yenaly/duanzile/ui/adapter/SimpleDuanziRvAdapter.kt b/app/src/main/java/com/yenaly/duanzile/ui/adapter/SimpleDuanziRvAdapter.kt index e4287b3..56c2f3a 100644 --- a/app/src/main/java/com/yenaly/duanzile/ui/adapter/SimpleDuanziRvAdapter.kt +++ b/app/src/main/java/com/yenaly/duanzile/ui/adapter/SimpleDuanziRvAdapter.kt @@ -11,13 +11,12 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions -import com.yenaly.duanzile.R -import com.yenaly.duanzile.TO_DUANZI_ACTIVITY_ID +import com.yenaly.duanzile.* import com.yenaly.duanzile.databinding.ItemDuanziSimplifiedBinding import com.yenaly.duanzile.ftpDecrypt -import com.yenaly.duanzile.isLogin import com.yenaly.duanzile.logic.model.DuanziListModel import com.yenaly.duanzile.ui.activity.DuanziActivity +import com.yenaly.duanzile.ui.activity.PicViewActivity import com.yenaly.duanzile.ui.activity.UserActivity import com.yenaly.yenaly_libs.utils.activity import com.yenaly.yenaly_libs.utils.shareText @@ -193,6 +192,13 @@ class SimpleDuanziRvAdapter : ) } } + viewHolder.binding.image.setOnClickListener { + val position = viewHolder.bindingAdapterPosition + val item = getItem(position) + item?.let { + startActivity(TO_PIC_VIEW_URLS to listOf(item.joke.imageURL.ftpDecrypt())) + } + } } return viewHolder diff --git a/app/src/main/java/com/yenaly/duanzile/ui/viewmodel/PicViewViewModel.kt b/app/src/main/java/com/yenaly/duanzile/ui/viewmodel/PicViewViewModel.kt new file mode 100644 index 0000000..ff00a1f --- /dev/null +++ b/app/src/main/java/com/yenaly/duanzile/ui/viewmodel/PicViewViewModel.kt @@ -0,0 +1,14 @@ +package com.yenaly.duanzile.ui.viewmodel + +import android.app.Application +import com.yenaly.yenaly_libs.base.YenalyViewModel + +/** + * @ProjectName : BlViewer + * @Author : Yenaly Liew + * @Time : 2022/06/01 001 23:03 + * @Description : Description... + */ +class PicViewViewModel(application: Application) : YenalyViewModel(application) { + +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_baseline_arrow_back_24.xml b/app/src/main/res/drawable/ic_baseline_arrow_back_24.xml new file mode 100644 index 0000000..9d84252 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_arrow_back_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_pic_view.xml b/app/src/main/res/layout/activity_pic_view.xml new file mode 100644 index 0000000..6c83774 --- /dev/null +++ b/app/src/main/res/layout/activity_pic_view.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_pic_view.xml b/app/src/main/res/layout/item_pic_view.xml new file mode 100644 index 0000000..1fd7ede --- /dev/null +++ b/app/src/main/res/layout/item_pic_view.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 2a2e56c..8d4c82e 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -9,4 +9,7 @@ #FFFFFFFF #FFFBFE + + #66000000 + #66F5F5F5 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d7a329e..efbdf30 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -24,5 +24,6 @@ 编辑资料 私信 赞 · 踩 + %d of %d 搜索 \ No newline at end of file