Просмотр исходного кода

1、图片编辑页面开发,增加颜色修改按钮、宽度修改控制、操作按钮
2、注册相关事件并初始化相关内容,对图片编辑控件的对外方法进行增加(颜色修改、宽度修改)

不会爬树的猴 1 год назад
Родитель
Сommit
ecfb52c24b

+ 113 - 5
app/src/main/java/com/cr/pages/FragmentImageEditor.kt

@@ -1,12 +1,18 @@
 package com.cr.pages
 
 import android.graphics.BitmapFactory
+import android.os.Build
 import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import android.widget.LinearLayout
+import android.widget.SeekBar
+import android.widget.TextView
+import androidx.annotation.RequiresApi
 import com.cr.cruav.R
 import com.cr.view.CrImageEditor
+import kotlinx.android.synthetic.main.frag_image_editor.*
 import java.io.File
 import java.io.FileInputStream
 
@@ -16,11 +22,29 @@ import java.io.FileInputStream
  * 创建日期:2023/6/20 11:20
  * 描述:图片编辑页面
  */
-class FragmentImageEditor : CrNavigationFragment() {
+class FragmentImageEditor : CrNavigationFragment(), View.OnClickListener {
     // define: 2023/6/20 图片编辑器
     private var imageEditor: CrImageEditor? = null
+
+    // define: 2023/6/26 绘制颜色控制按钮
+    private var btnLineColor1: LinearLayout? = null
+    private var btnLineColor2: LinearLayout? = null
+    private var btnLineColor3: LinearLayout? = null
+    private var btnLineColor4: LinearLayout? = null
+    private var btnLineColor5: LinearLayout? = null
+
+    // define: 2023/6/26 操作按钮
+    private var btnBack: LinearLayout? = null
+    private var btnReset: LinearLayout? = null
+    private var btnSave: LinearLayout? = null
+    private var btnRecover: LinearLayout? = null
+
+    // define: 2023/6/26 控制线宽度
+    private var seekLineWidth: SeekBar? = null
+    private var lblLineWidth: TextView? = null
+
     // define: 2023/6/20 图片路径
-    private var imagePath:String?=null
+    private var imagePath: String? = null
 
     /**
      * 初始化
@@ -49,26 +73,110 @@ class FragmentImageEditor : CrNavigationFragment() {
     override fun joinControls() {
         // todo: 2023/6/20 挂载图片显示控件
         imageEditor = mainView?.findViewById(R.id.image_editor)
+        // todo: 2023/6/26 挂载颜色控制按钮
+        btnLineColor1 = mainView?.findViewById(R.id.btn_line_color1)
+        btnLineColor1?.setOnClickListener(this)
+        btnLineColor2 = mainView?.findViewById(R.id.btn_line_color2)
+        btnLineColor2?.setOnClickListener(this)
+        btnLineColor3 = mainView?.findViewById(R.id.btn_line_color3)
+        btnLineColor3?.setOnClickListener(this)
+        btnLineColor4 = mainView?.findViewById(R.id.btn_line_color4)
+        btnLineColor4?.setOnClickListener(this)
+        btnLineColor5 = mainView?.findViewById(R.id.btn_line_color5)
+        btnLineColor5?.setOnClickListener(this)
+        // todo: 2023/6/26 挂载操作按钮
+        btnBack = mainView?.findViewById(R.id.btn_back)
+        btnBack?.setOnClickListener(this)
+        btnReset = mainView?.findViewById(R.id.btn_reset)
+        btnReset?.setOnClickListener(this)
+        btnSave = mainView?.findViewById(R.id.btn_save)
+        btnSave?.setOnClickListener(this)
+        btnRecover = mainView?.findViewById(R.id.btn_recover)
+        btnRecover?.setOnClickListener(this)
+        // todo: 2023/6/26 初始化线宽度
+        seekLineWidth = mainView?.findViewById(R.id.seek_line_width)
+        lblLineWidth = mainView?.findViewById(R.id.lbl_line_width)
+        seekLineWidth?.let {
+            it.max = 10
+            it.progress = 3
+            lblLineWidth?.setText(String.format("线条宽度 ${it.progress}"))
+        }
+        seekLineWidth?.setOnSeekBarChangeListener(seekLineWidthListener)
+    }
+
+    /**
+     * 线条宽度修改监听
+     */
+    private val seekLineWidthListener = object :SeekBar.OnSeekBarChangeListener{
+        // todo: 2023/6/26 变化监听
+        override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
+            lblLineWidth?.text = String.format("线条宽度 $p1")
+            imageEditor?.crSetLineWidth(p1.toFloat())
+        }
+
+        override fun onStartTrackingTouch(p0: SeekBar?) {
+        }
+
+        override fun onStopTrackingTouch(p0: SeekBar?) {
+        }
+
+    }
+
+    /**
+     * 视图点击事件
+     * @param view View
+     */
+    override fun onClick(view: View?) {
+        when (view?.id) {
+            R.id.btn_line_color1 -> {
+                imageEditor?.crSetLineColor(self?.context!!.getColor(R.color.line_1))
+            }
+            R.id.btn_line_color2 -> {
+                imageEditor?.crSetLineColor(self?.context!!.getColor(R.color.line_2))
+            }
+            R.id.btn_line_color3 -> {
+                imageEditor?.crSetLineColor(self?.context!!.getColor(R.color.line_3))
+            }
+            R.id.btn_line_color4 -> {
+                imageEditor?.crSetLineColor(self?.context!!.getColor(R.color.line_4))
+            }
+            R.id.btn_line_color5 -> {
+                imageEditor?.crSetLineColor(self?.context!!.getColor(R.color.line_5))
+            }
+            R.id.btn_back -> {
+
+            }
+            R.id.btn_reset -> {
+
+            }
+            R.id.btn_save -> {
+
+            }
+            R.id.btn_recover -> {
+
+            }
+        }
     }
 
     /**
      * 显示图片
      */
-    private fun showImage(){
+    private fun showImage() {
         imagePath?.let {
             var file = File(it)
-            if(file.exists()) {
+            if (file.exists()) {
                 var fis = FileInputStream(it)
                 var bitmap = BitmapFactory.decodeStream(fis)
                 imageEditor?.setImageBitmap(bitmap!!)
             }
         }
     }
+
     /**
      * 设置显示的图片
      * @param imagePath String 图片路径
      */
-    fun crSetImage(imagePath:String){
+    fun crSetImage(imagePath: String) {
         this.imagePath = imagePath
         // todo: 2023/6/20 显示图片
         showImage()

+ 23 - 2
app/src/main/java/com/cr/view/CrImageEditor.kt

@@ -7,6 +7,7 @@ import android.view.GestureDetector
 import android.view.MotionEvent
 import android.view.ScaleGestureDetector
 import androidx.appcompat.widget.AppCompatImageView
+import com.cr.cruav.R
 
 /**
  * 操作系统:MAC系统
@@ -160,9 +161,9 @@ class CrImageEditor @JvmOverloads constructor(
         drawPointPaint.isAntiAlias = true
         drawPointPaint.strokeWidth = 20f
         // todo: 2023/6/20 初始化线画笔
-        drawLinePaint.color = Color.RED
+        drawLinePaint.color = context!!.getColor(R.color.line_4)
         drawLinePaint.isAntiAlias = true
-        drawLinePaint.strokeWidth = 5.0f
+        drawLinePaint.strokeWidth = 3.0f
     }
 
     /**
@@ -213,4 +214,24 @@ class CrImageEditor @JvmOverloads constructor(
         }
         return true
     }
+
+    /**
+     * 设置线的宽度
+     * @param width Float 线的宽度
+     */
+    fun crSetLineWidth(width:Float){
+        drawLinePaint.strokeWidth = width
+        // todo: 2023/6/26 重绘
+        invalidate()
+    }
+
+    /**
+     * 设置线的颜色
+     * @param color Int 线的颜色
+     */
+    fun crSetLineColor(color:Int){
+        drawLinePaint.color = color
+        // todo: 2023/6/26 重绘
+        invalidate()
+    }
 }

+ 34 - 12
app/src/main/res/layout/frag_image_editor.xml

@@ -37,24 +37,44 @@
                 android:gravity="center_vertical">
                 <LinearLayout
                     style="@style/fie_line_color_button"
-                    android:background="@color/line_1"
-                    android:id="@+id/btn_line_color1"/>
+                    android:id="@+id/btn_line_color1">
+                    <TextView
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:background="@color/line_1"/>
+                </LinearLayout>
                 <LinearLayout
                     style="@style/fie_line_color_button"
-                    android:background="@color/line_2"
-                    android:id="@+id/btn_line_color2"/>
+                    android:id="@+id/btn_line_color2">
+                    <TextView
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:background="@color/line_2"/>
+                </LinearLayout>
                 <LinearLayout
                     style="@style/fie_line_color_button"
-                    android:background="@color/line_3"
-                    android:id="@+id/btn_line_color3"/>
+                    android:id="@+id/btn_line_color3">
+                    <TextView
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:background="@color/line_3"/>
+                </LinearLayout>
                 <LinearLayout
                     style="@style/fie_line_color_button"
-                    android:background="@color/line_4"
-                    android:id="@+id/btn_line_color4"/>
+                    android:id="@+id/btn_line_color4">
+                    <TextView
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:background="@color/line_4"/>
+                </LinearLayout>
                 <LinearLayout
                     style="@style/fie_line_color_button"
-                    android:background="@color/line_5"
-                    android:id="@+id/btn_line_color5"/>
+                    android:id="@+id/btn_line_color5">
+                    <TextView
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:background="@color/line_5"/>
+                </LinearLayout>
             </LinearLayout>
         </LinearLayout>
         <View style="@style/view_split_v1"/>
@@ -65,6 +85,7 @@
             android:orientation="vertical">
             <TextView
                 style="@style/lbl_title_common"
+                android:id="@+id/lbl_line_width"
                 android:layout_width="match_parent"
                 android:gravity="center"
                 android:text="@string/fie_title_lien_width" />
@@ -79,9 +100,10 @@
                     android:progressDrawable="@drawable/back_seek_bar"
                     android:thumb="@drawable/seek_bar_thumb"
                     android:max="10"
-                    android:min="0"
+                    android:min="1"
                     android:splitTrack="false"
-                    android:progress="5"/>
+                    android:progress="5"
+                    android:id="@+id/seek_line_width"/>
             </LinearLayout>
         </LinearLayout>
         <View style="@style/view_split_v1"/>

+ 1 - 0
app/src/main/res/values/themes.xml

@@ -351,6 +351,7 @@
         <item name="android:layout_marginLeft">@dimen/cr_4_dp</item>
         <item name="android:layout_marginRight">@dimen/cr_4_dp</item>
         <item name="android:orientation">horizontal</item>
+        <item name="background">@drawable/btn_imgbtn_selector</item>
     </style>
 
     <!--自定义文本框属性-->