Sfoglia il codice sorgente

Use ColorSelectView in AnnotationAttributeView

Wayne 5 anni fa
parent
commit
dcd83ee619

+ 13 - 31
reader/src/main/java/com/kdanmobile/reader/view/AnnotationAttributeView.kt

@@ -4,7 +4,6 @@ import android.content.Context
 import android.support.v4.graphics.ColorUtils
 import android.util.AttributeSet
 import android.view.View
-import android.widget.ImageView
 import android.widget.LinearLayout
 import android.widget.SeekBar
 import com.kdanmobile.reader.R
@@ -12,6 +11,7 @@ import com.kdanmobile.reader.annotationattribute.AnnotationAttribute
 import com.kdanmobile.reader.annotationattribute.AnnotationColor
 import com.kdanmobile.reader.annotationattribute.Brush
 import com.kdanmobile.reader.annotationattribute.InkAttribute
+import com.kdanmobile.reader.screen.view.ColorSelectView
 import kotlinx.android.synthetic.main.view_reader_annotation_property.view.*
 
 class AnnotationAttributeView @JvmOverloads constructor(
@@ -53,18 +53,6 @@ class AnnotationAttributeView @JvmOverloads constructor(
             update()
         }
     var onChangeListener: OnChangeListener? = null
-    private val colorBtnMap = HashMap<AnnotationColor, ImageView>().apply {
-        put(AnnotationColor.Color1, iv_readerAnnotationProperty_color1)
-        put(AnnotationColor.Color2, iv_readerAnnotationProperty_color2)
-        put(AnnotationColor.Color3, iv_readerAnnotationProperty_color3)
-        put(AnnotationColor.Color4, iv_readerAnnotationProperty_color4)
-        put(AnnotationColor.Color5, iv_readerAnnotationProperty_color5)
-        put(AnnotationColor.Color6, iv_readerAnnotationProperty_color6)
-        put(AnnotationColor.Color7, iv_readerAnnotationProperty_color7)
-        put(AnnotationColor.Color8, iv_readerAnnotationProperty_color8)
-        put(AnnotationColor.Color9, iv_readerAnnotationProperty_color9)
-        put(AnnotationColor.Color10, iv_readerAnnotationProperty_color10)
-    }
     private var thickness: Int = DEFAULT_THICKNESS
         set(value) {
             field = value
@@ -100,6 +88,7 @@ class AnnotationAttributeView @JvmOverloads constructor(
         }
     private var colorEnum: AnnotationColor = AnnotationColor.Color1
         set(value) {
+            if (field == value) return
             field = value
             updateColor()
             updateColorButtons()
@@ -154,8 +143,15 @@ class AnnotationAttributeView @JvmOverloads constructor(
     }
 
     private fun setupColorButtons() {
-        colorBtnMap.values.forEach {
-            it.setOnClickListener(this::onClickColorBtn)
+        viewGroup_readerAnnotationProperty_color.onColorSelectedListener = object : ColorSelectView.OnColorSelectedListener {
+            override fun onColorSelected(color: Int) {
+                AnnotationColor.values().forEach { annotationColor ->
+                    val c = resources.getColor(annotationColor.colorResId)
+                    if (c == color) {
+                        colorEnum = annotationColor
+                    }
+                }
+            }
         }
     }
 
@@ -221,15 +217,6 @@ class AnnotationAttributeView @JvmOverloads constructor(
         })
     }
 
-    private fun onClickColorBtn(view: View) {
-        colorBtnMap.entries.forEach {
-            if (it.value == view) {
-                colorEnum = it.key
-                return
-            }
-        }
-    }
-
     private fun update() {
         updateOpacity()
         updateThickness()
@@ -277,13 +264,8 @@ class AnnotationAttributeView @JvmOverloads constructor(
     }
 
     private fun updateColorButtons() {
-        colorBtnMap.entries.forEach {
-            if (it.key == colorEnum) {
-                it.value.setImageResource(R.drawable.reader_annotation_property_color_border_select)
-            } else {
-                it.value.setImageResource(R.drawable.reader_annotation_property_color_border)
-            }
-        }
+        val c = resources.getColor(colorEnum.colorResId)
+        viewGroup_readerAnnotationProperty_color.setSelectedColor(c)
     }
 
     interface OnChangeListener {

+ 4 - 59
reader/src/main/res/layout/view_reader_annotation_property.xml

@@ -127,69 +127,14 @@
         android:layout_width="32dp"
         android:layout_height="wrap_content" />
 
-    <HorizontalScrollView
+    <com.kdanmobile.reader.screen.view.ColorSelectView
         android:id="@+id/viewGroup_readerAnnotationProperty_color"
         app:layout_constraintTop_toBottomOf="@id/seekBar_readerAnnotationProperty_opacity"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toRightOf="parent"
         android:layout_marginTop="16dp"
         android:layout_width="0dp"
-        android:layout_height="wrap_content">
-        <LinearLayout
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:gravity="center"
-            android:orientation="horizontal">
-
-            <ImageView
-                style="@style/AnnotationPropertyColorImageView"
-                android:id="@+id/iv_readerAnnotationProperty_color1"
-                android:background="@color/reader_color_selector_color_1" />
-
-            <ImageView
-                style="@style/AnnotationPropertyColorImageView"
-                android:id="@+id/iv_readerAnnotationProperty_color2"
-                android:background="@color/reader_color_selector_color_2" />
-
-            <ImageView
-                style="@style/AnnotationPropertyColorImageView"
-                android:id="@+id/iv_readerAnnotationProperty_color3"
-                android:background="@color/reader_color_selector_color_3" />
-
-            <ImageView
-                style="@style/AnnotationPropertyColorImageView"
-                android:id="@+id/iv_readerAnnotationProperty_color4"
-                android:background="@color/reader_color_selector_color_4" />
-
-            <ImageView
-                style="@style/AnnotationPropertyColorImageView"
-                android:id="@+id/iv_readerAnnotationProperty_color5"
-                android:background="@color/reader_color_selector_color_5" />
-
-            <ImageView
-                style="@style/AnnotationPropertyColorImageView"
-                android:id="@+id/iv_readerAnnotationProperty_color6"
-                android:background="@color/reader_color_selector_color_6" />
-
-            <ImageView
-                style="@style/AnnotationPropertyColorImageView"
-                android:id="@+id/iv_readerAnnotationProperty_color7"
-                android:background="@color/reader_color_selector_color_7" />
-
-            <ImageView
-                style="@style/AnnotationPropertyColorImageView"
-                android:id="@+id/iv_readerAnnotationProperty_color8"
-                android:background="@color/reader_color_selector_color_8" />
-
-            <ImageView
-                style="@style/AnnotationPropertyColorImageView"
-                android:id="@+id/iv_readerAnnotationProperty_color9"
-                android:background="@color/reader_color_selector_color_9" />
-
-            <ImageView
-                style="@style/AnnotationPropertyColorImageView"
-                android:id="@+id/iv_readerAnnotationProperty_color10"
-                android:background="@color/reader_color_selector_color_10" />
-        </LinearLayout>
-    </HorizontalScrollView>
+        android:layout_height="wrap_content"/>
 
     <ImageView
         android:id="@+id/iv_readerAnnotationProperty_triangle1"