FragmentImageEditor.kt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package com.cr.pages
  2. import android.os.Bundle
  3. import android.view.LayoutInflater
  4. import android.view.View
  5. import android.view.ViewGroup
  6. import android.widget.LinearLayout
  7. import android.widget.SeekBar
  8. import android.widget.TextView
  9. import com.cr.common.CrColorManager
  10. import com.cr.common.CrFileManager
  11. import com.cr.common.DataManager
  12. import com.cr.cruav.R
  13. import com.cr.data.CrUtil
  14. import com.cr.view.CrImageEditor
  15. /**
  16. * 操作系统:MAC系统
  17. * 创建者:王成
  18. * 创建日期:2023/6/20 11:20
  19. * 描述:图片编辑页面
  20. */
  21. class FragmentImageEditor : CrNavigationFragment(), View.OnClickListener {
  22. /**
  23. * 对外接口
  24. */
  25. interface IChangeListener{
  26. // todo: 2023/9/28 保存照片
  27. fun onSave(oldName:String,newName:String)
  28. // todo: 2023/9/28 恢复
  29. fun onRecover(oldName:String,newName:String)
  30. }
  31. // define: 2023/6/20 图片编辑器
  32. private var imageEditor: CrImageEditor? = null
  33. // define: 2023/6/26 绘制颜色控制按钮
  34. private var btnLineColor1: LinearLayout? = null
  35. private var btnLineColor2: LinearLayout? = null
  36. private var btnLineColor3: LinearLayout? = null
  37. private var btnLineColor4: LinearLayout? = null
  38. private var btnLineColor5: LinearLayout? = null
  39. // define: 2023/6/26 操作按钮
  40. private var btnBack: LinearLayout? = null
  41. private var btnReset: LinearLayout? = null
  42. private var btnSave: LinearLayout? = null
  43. private var btnRecover: LinearLayout? = null
  44. // define: 2023/6/26 控制线宽度
  45. private var seekLineWidth: SeekBar? = null
  46. private var lblLineWidth: TextView? = null
  47. // define: 2023/6/20 图片路径
  48. private var imagePath: String? = null
  49. // todo: 2023/9/28 监听
  50. private var listener:IChangeListener?= null // define: 2023/9/28 监听
  51. /**
  52. * 初始化
  53. * @param inflater LayoutInflater
  54. * @param container ViewGroup?
  55. * @param savedInstanceState Bundle?
  56. * @return View?
  57. */
  58. override fun onCreateView(
  59. inflater: LayoutInflater,
  60. container: ViewGroup?,
  61. savedInstanceState: Bundle?
  62. ): View? {
  63. mainView = inflater.inflate(R.layout.frag_image_editor, null)
  64. self = this
  65. // todo: 2023/6/20 挂载控件
  66. joinControls()
  67. // todo: 2023/6/20 显示图片
  68. showImage()
  69. return mainView
  70. }
  71. /**
  72. * 重写挂载控件
  73. */
  74. override fun joinControls() {
  75. // todo: 2023/6/20 挂载图片显示控件
  76. imageEditor = mainView?.findViewById(R.id.image_editor)
  77. // todo: 2023/6/26 挂载颜色控制按钮
  78. btnLineColor1 = mainView?.findViewById(R.id.btn_line_color1)
  79. btnLineColor1?.setOnClickListener(this)
  80. btnLineColor2 = mainView?.findViewById(R.id.btn_line_color2)
  81. btnLineColor2?.setOnClickListener(this)
  82. btnLineColor3 = mainView?.findViewById(R.id.btn_line_color3)
  83. btnLineColor3?.setOnClickListener(this)
  84. btnLineColor4 = mainView?.findViewById(R.id.btn_line_color4)
  85. btnLineColor4?.setOnClickListener(this)
  86. btnLineColor5 = mainView?.findViewById(R.id.btn_line_color5)
  87. btnLineColor5?.setOnClickListener(this)
  88. // todo: 2023/6/26 挂载操作按钮
  89. btnBack = mainView?.findViewById(R.id.btn_back)
  90. btnBack?.setOnClickListener(this)
  91. btnReset = mainView?.findViewById(R.id.btn_reset)
  92. btnReset?.setOnClickListener(this)
  93. btnSave = mainView?.findViewById(R.id.btn_save)
  94. btnSave?.setOnClickListener(this)
  95. btnRecover = mainView?.findViewById(R.id.btn_recover)
  96. btnRecover?.setOnClickListener(this)
  97. // todo: 2023/6/26 初始化线宽度
  98. seekLineWidth = mainView?.findViewById(R.id.seek_line_width)
  99. lblLineWidth = mainView?.findViewById(R.id.lbl_line_width)
  100. seekLineWidth?.let {
  101. it.max = 10
  102. it.progress = 3
  103. lblLineWidth?.setText(String.format("线条宽度 ${it.progress}"))
  104. }
  105. seekLineWidth?.setOnSeekBarChangeListener(seekLineWidthListener)
  106. }
  107. /**
  108. * 线条宽度修改监听
  109. */
  110. private val seekLineWidthListener = object :SeekBar.OnSeekBarChangeListener{
  111. // todo: 2023/6/26 变化监听
  112. override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
  113. lblLineWidth?.text = String.format("线条宽度 $p1")
  114. imageEditor?.crSetLineWidth(p1.toFloat())
  115. }
  116. // todo: 2023/9/28 开始变化
  117. override fun onStartTrackingTouch(p0: SeekBar?) {
  118. }
  119. // todo: 2023/9/28 停止变化
  120. override fun onStopTrackingTouch(p0: SeekBar?) {
  121. }
  122. }
  123. /**
  124. * 视图点击事件
  125. * @param view View
  126. */
  127. override fun onClick(view: View?) {
  128. when (view?.id) {
  129. R.id.btn_line_color1 -> {
  130. imageEditor?.crSetLineColor(CrColorManager.getColor(R.color.line_1))
  131. }
  132. R.id.btn_line_color2 -> {
  133. imageEditor?.crSetLineColor(CrColorManager.getColor(R.color.line_2))
  134. }
  135. R.id.btn_line_color3 -> {
  136. imageEditor?.crSetLineColor(CrColorManager.getColor(R.color.line_3))
  137. }
  138. R.id.btn_line_color4 -> {
  139. imageEditor?.crSetLineColor(CrColorManager.getColor(R.color.line_4))
  140. }
  141. R.id.btn_line_color5 -> {
  142. imageEditor?.crSetLineColor(CrColorManager.getColor(R.color.line_5))
  143. }
  144. R.id.btn_back -> {
  145. // todo: 2023/9/28 回退一步
  146. imageEditor?.crSetUndo()
  147. }
  148. R.id.btn_reset -> {
  149. // todo: 2023/9/28 重绘
  150. imageEditor?.crReset()
  151. }
  152. R.id.btn_save -> {
  153. // todo: 2023/9/28 保存文件
  154. var fileName = imageEditor?.crSaveImage()
  155. if(fileName != null){
  156. // todo: 2023/9/28 反馈
  157. val oldName = CrFileManager.getFileName(this.imagePath!!)
  158. this.listener?.onSave(oldName!!,fileName)
  159. }else{
  160. showError("保存失败")
  161. }
  162. }
  163. R.id.btn_recover -> {
  164. // todo: 2023/9/28 找到原来的照片
  165. val imgName = CrFileManager.getFileName(this.imagePath!!)
  166. val oldName = DataManager.queryEditImage(imgName!!)
  167. if(oldName == null){
  168. showError("文件已不存在,无法恢复!")
  169. }else{
  170. this.imagePath = String.format("%s%s",CrUtil.IMAGE_PATH,oldName)
  171. // todo: 2023/9/28 反馈
  172. this.listener?.onRecover(oldName!!,imgName)
  173. }
  174. crSetImage(this.imagePath!!)
  175. }
  176. }
  177. }
  178. /**
  179. * 显示图片
  180. */
  181. private fun showImage() {
  182. imagePath?.let {
  183. imageEditor?.crSetBitmap(it)
  184. }
  185. }
  186. /**
  187. * 设置显示的图片
  188. * @param imagePath String 图片路径
  189. */
  190. fun crSetImage(imagePath: String) {
  191. this.imagePath = imagePath
  192. // todo: 2023/6/20 显示图片
  193. showImage()
  194. }
  195. /**
  196. * 设置监听
  197. * @param listener IChangeListener 监听
  198. */
  199. fun crSetChangeListener(listener:IChangeListener){
  200. this.listener = listener
  201. }
  202. }