码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Android Glide照片宫格RecyclerView,点击SharedElement共享元素动画查看大图,Kotlin(1)


    Android Glide照片宫格RecyclerView,点击SharedElement共享元素动画查看大图,Kotlin(1)

    1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    2. <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

    1. plugins {
    2. id 'org.jetbrains.kotlin.kapt'
    3. }
    4. implementation 'com.github.bumptech.glide:glide:4.16.0'
    5. kapt 'com.github.bumptech.glide:compiler:4.16.0'

    1. import android.content.Context
    2. import android.content.Intent
    3. import android.os.Bundle
    4. import android.provider.MediaStore
    5. import android.view.LayoutInflater
    6. import android.view.View
    7. import android.view.View.OnClickListener
    8. import android.view.ViewGroup
    9. import android.widget.ImageView
    10. import android.widget.TextView
    11. import androidx.appcompat.app.AppCompatActivity
    12. import androidx.core.app.ActivityOptionsCompat
    13. import androidx.core.util.Pair
    14. import androidx.lifecycle.lifecycleScope
    15. import androidx.recyclerview.widget.GridLayoutManager
    16. import androidx.recyclerview.widget.RecyclerView
    17. import kotlinx.coroutines.Dispatchers
    18. import kotlinx.coroutines.launch
    19. import kotlinx.coroutines.withContext
    20. const val IMAGE_URI = "image_uri"
    21. const val SIZE = 200
    22. class MainActivity : AppCompatActivity() {
    23. override fun onCreate(savedInstanceState: Bundle?) {
    24. super.onCreate(savedInstanceState)
    25. setContentView(R.layout.recyclerview)
    26. val rv = findViewById(R.id.recycler_view)
    27. val spanCount = 7
    28. val layoutManager = GridLayoutManager(this, spanCount)
    29. layoutManager.orientation = GridLayoutManager.VERTICAL
    30. rv.layoutManager = layoutManager
    31. val adapter = MyAdapter()
    32. rv.adapter = adapter
    33. lifecycleScope.launch(Dispatchers.IO) {
    34. val items = readAllImage(this@MainActivity)
    35. withContext(Dispatchers.Main) {
    36. adapter.dataChanged(items)
    37. }
    38. }
    39. }
    40. fun goToSharedElementAnima(view: ImageView, path: String) {
    41. val pair: Pair? = Pair(view, resources.getString(R.string.shared_element))
    42. val intent = Intent(this, ImageViewActivity::class.java)
    43. intent.putExtra(IMAGE_URI, path)
    44. val bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(this, pair).toBundle()
    45. startActivity(intent, bundle)
    46. }
    47. inner class MyAdapter : RecyclerView.Adapter<MyVH>() {
    48. private var items = arrayListOf()
    49. fun dataChanged(items: ArrayList<MyData>) {
    50. this.items = items
    51. notifyDataSetChanged()
    52. }
    53. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVH {
    54. val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
    55. return MyVH(view)
    56. }
    57. override fun getItemCount(): Int {
    58. return items.size
    59. }
    60. override fun onBindViewHolder(holder: MyVH, position: Int) {
    61. val uri = items[holder.adapterPosition].path
    62. holder.itemView.setOnClickListener(object : OnClickListener {
    63. override fun onClick(v: View?) {
    64. goToSharedElementAnima(holder.image, uri)
    65. }
    66. })
    67. GlideApp.with(holder.itemView.context)
    68. .load(uri)
    69. .centerCrop()
    70. .override(SIZE) //注意这个size,在宫格里面显示出来后表示decode完成
    71. .into(holder.image)
    72. holder.pos.text = items[position].index.toString()
    73. }
    74. }
    75. class MyVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
    76. val image: ImageView = itemView.findViewById(R.id.image)
    77. val pos: TextView = itemView.findViewById(R.id.pos)
    78. }
    79. private fun readAllImage(context: Context): ArrayList {
    80. val photos = ArrayList()
    81. //读取所有图片
    82. val cursor = context.contentResolver.query(
    83. MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null
    84. )
    85. var index = 0
    86. while (cursor!!.moveToNext()) {
    87. //路径 uri
    88. val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA))
    89. //图片名称
    90. //val name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME))
    91. //图片大小
    92. //val size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE))
    93. photos.add(MyData(path, index++))
    94. }
    95. cursor.close()
    96. return photos
    97. }
    98. class MyData(var path: String, var index: Int)
    99. }

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:orientation="vertical">
    6. <androidx.recyclerview.widget.RecyclerView
    7. android:id="@+id/recycler_view"
    8. android:layout_width="match_parent"
    9. android:layout_height="match_parent" />
    10. LinearLayout>

    共享元素动画必须设置相同的transitionName值:

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="wrap_content"
    4. android:layout_height="wrap_content"
    5. android:orientation="vertical"
    6. android:padding="1dp">
    7. <ImageView
    8. android:id="@+id/image"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:transitionName="@string/shared_element" />
    12. <TextView
    13. android:id="@+id/pos"
    14. android:layout_width="wrap_content"
    15. android:layout_height="wrap_content"
    16. android:layout_gravity="center_horizontal"
    17. android:textSize="5dp" />
    18. <TextView
    19. android:id="@+id/text"
    20. android:layout_width="wrap_content"
    21. android:layout_height="wrap_content"
    22. android:layout_gravity="center_horizontal"
    23. android:textSize="10dp" />
    24. <View
    25. android:layout_width="20dp"
    26. android:layout_height="1px"
    27. android:layout_gravity="center_horizontal"
    28. android:layout_marginBottom="10dp"
    29. android:background="@color/cardview_dark_background" />
    30. LinearLayout>

    1. import android.os.Bundle
    2. import android.widget.ImageView
    3. import androidx.appcompat.app.AppCompatActivity
    4. class ImageViewActivity : AppCompatActivity() {
    5. override fun onCreate(savedInstanceState: Bundle?) {
    6. super.onCreate(savedInstanceState)
    7. setContentView(R.layout.image)
    8. val imageView = findViewById(R.id.image)
    9. val uri = intent.getStringExtra(IMAGE_URI)
    10. GlideApp.with(this)
    11. .load(uri)
    12. .fitCenter()
    13. .thumbnail(
    14. //一种技巧,规避原始decode耗时造成共享元素动画异常。 thumbnail主要为了加载速度,此处查看大图,原始decode非常耗时,先放一张在上一个页面宫格里面已经decode好的图。
    15. GlideApp.with(this)
    16. .load(uri)
    17. .centerCrop()
    18. .override(SIZE)
    19. )
    20. .error(android.R.drawable.stat_notify_error)
    21. .into(imageView)
    22. }
    23. }

    共享元素动画必须设置相同的transitionName值:

    1. "1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent">
    6. <ImageView
    7. android:id="@+id/image"
    8. android:layout_width="match_parent"
    9. android:layout_height="match_parent"
    10. android:transitionName="@string/shared_element" />
    11. androidx.constraintlayout.widget.ConstraintLayout>

    系统通过transitionName作为识别共享元素动画的要素之一。

    1. <resources>
    2. <string name="shared_element">my_shared_elementstring>
    3. resources>

    Android读取设备所有Video视频,Kotlin-CSDN博客文章浏览阅读81次。【Android设置头像,手机拍照或从本地相册选取图片作为头像】像微信、QQ、微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式:1,让用户通过选择本地相册之类的图片库中已有的图像,裁剪后作为头像。Android设置头像,手机拍照或从本地相册选取图片作为头像_android 头像拍照_zhangphil的博客-CSDN博客。Android图片添加文字水印并保存水印文字图片到指定文件_zhangphil的博客-CSDN博客。Android读取设备所有视频,Kotlin。https://blog.csdn.net/zhangphil/article/details/132173745Android load all photos into RecyclerView,pinch to zoom by ScaleGestureDetector,kotlin(4)_zhangphil的博客-CSDN博客文章浏览阅读77次。Android RecyclerView的StaggeredGridLayoutManager实现交错排列的子元素分组先看实现的结果如图:设计背景:现在的产品对设计的需求越来越多样化,如附录文章2是典型的联系人分组RecyclerView,子元素排列到一个相同的组,但是有些时候,UI要求把这些元素不是垂直方向的,而是像本文开头的图中所示样式排列,这就需要用StaggeredGridLayoutMa。在处理大图的浏览查看动作过程中,往往还有其他额外的事情需要处理,典型的以微信。https://blog.csdn.net/zhangphil/article/details/131296499

  • 相关阅读:
    arcmap 在oracle删除表重新创建提示表名存在解决放啊
    #ubuntu# #常用工具#
    C++之哈希表、哈希桶的实现
    Docker介绍
    docker 学习-- 04 实践2 (lnpmr环境)
    Mathematics-Vocabulary·数学专业英语词汇
    苹果AR设备未来展望:硬件舒适性、软件功能与网络速度等多维度期待
    让人头痛的大事务问题到底要如何解决?
    IDEA 设置主题、背景图片、背景颜色
    记一次 .NET某工控视觉自动化系统 卡死分析
  • 原文地址:https://blog.csdn.net/zhangphil/article/details/134209368
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号