瀏覽代碼

Refactor Cover color selector

Wayne 6 年之前
父節點
當前提交
bab66efef9

+ 21 - 0
src/main/java/com/bomostory/sceneeditmodule/cover/CoverColor.kt

@@ -0,0 +1,21 @@
+package com.bomostory.sceneeditmodule.cover
+
+import android.content.Context
+import com.example.tfat.myapplication.R
+
+enum class CoverColor(val colorResId: Int) {
+    Color1(R.color.coverColor_1),
+    Color2(R.color.coverColor_2),
+    Color3(R.color.coverColor_3),
+    Color4(R.color.coverColor_4),
+    Color5(R.color.coverColor_5),
+    Color6(R.color.coverColor_6),
+    Color7(R.color.coverColor_7),
+    Color8(R.color.coverColor_8),
+    Color9(R.color.coverColor_9),
+    Color10(R.color.coverColor_10);
+
+    fun getColor(context: Context): Int {
+        return context.resources.getColor(colorResId)
+    }
+}

+ 82 - 0
src/main/java/com/bomostory/sceneeditmodule/cover/CoverColorSelectorView.kt

@@ -0,0 +1,82 @@
+package com.bomostory.sceneeditmodule.cover
+
+import android.content.Context
+import android.support.constraint.ConstraintLayout
+import android.util.AttributeSet
+import android.view.View
+import com.example.tfat.myapplication.R
+
+class CoverColorSelectorView @JvmOverloads constructor(
+        context: Context,
+        attrs: AttributeSet? = null,
+        defStyleAttr: Int = 0
+) : ConstraintLayout(context, attrs, defStyleAttr) {
+
+    private val viewToColorMap = HashMap<Int, CoverColor>().apply {
+        put(R.id.iv_coverColorSelector_color1, CoverColor.Color1)
+        put(R.id.iv_coverColorSelector_color2, CoverColor.Color2)
+        put(R.id.iv_coverColorSelector_color3, CoverColor.Color3)
+        put(R.id.iv_coverColorSelector_color4, CoverColor.Color4)
+        put(R.id.iv_coverColorSelector_color5, CoverColor.Color5)
+        put(R.id.iv_coverColorSelector_color6, CoverColor.Color6)
+        put(R.id.iv_coverColorSelector_color7, CoverColor.Color7)
+        put(R.id.iv_coverColorSelector_color8, CoverColor.Color8)
+        put(R.id.iv_coverColorSelector_color9, CoverColor.Color9)
+        put(R.id.iv_coverColorSelector_color10, CoverColor.Color10)
+    }
+
+    private val colorToViewMap = HashMap<CoverColor, Int>().apply {
+        put(CoverColor.Color1, R.id.iv_coverColorSelector_color1)
+        put(CoverColor.Color2, R.id.iv_coverColorSelector_color2)
+        put(CoverColor.Color3, R.id.iv_coverColorSelector_color3)
+        put(CoverColor.Color4, R.id.iv_coverColorSelector_color4)
+        put(CoverColor.Color5, R.id.iv_coverColorSelector_color5)
+        put(CoverColor.Color6, R.id.iv_coverColorSelector_color6)
+        put(CoverColor.Color7, R.id.iv_coverColorSelector_color7)
+        put(CoverColor.Color8, R.id.iv_coverColorSelector_color8)
+        put(CoverColor.Color9, R.id.iv_coverColorSelector_color9)
+        put(CoverColor.Color10, R.id.iv_coverColorSelector_color10)
+    }
+
+    private val colorViewList = ArrayList<View>()
+    private var selectedColor = CoverColor.Color1
+
+    init {
+        inflate(context, R.layout.view_cover_color_selector, this)
+        viewToColorMap.keys.forEach { ivId ->
+            val v = findViewById<View>(ivId)
+            v.setOnClickListener { it ->
+                viewToColorMap[it.id]?.let { color -> select(color) }
+            }
+            colorViewList.add(this)
+        }
+        select(selectedColor)
+    }
+
+    fun select(coverColor: CoverColor) {
+        selectedColor = coverColor
+        colorToViewMap[coverColor]?.let {
+            val v = findViewById<View>(it)
+            colorViewList.forEach {
+                if (it == v) {
+                    it.setToCheckState()
+                } else {
+                    it.setToNonCheckState()
+                }
+            }
+        }
+    }
+
+    private fun View.setToNonCheckState() {
+        setPadding(0)
+    }
+
+    private fun View.setToCheckState() {
+        val padding = resources.getDimension(R.dimen.cover_color_item_padding).toInt()
+        setPadding(padding)
+    }
+
+    private fun View.setPadding(padding: Int) {
+        setPadding(padding, padding, padding, padding)
+    }
+}

+ 98 - 0
src/main/res/layout/view_cover_color_selector.xml

@@ -0,0 +1,98 @@
+<?xml version="1.0" encoding="utf-8"?>
+<merge xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    tools:parentTag="android.support.constraint.ConstraintLayout">
+
+    <LinearLayout
+        android:id="@+id/viewGroup_coverColorSelector_1"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintTop_toTopOf="parent">
+
+
+        <ImageView
+            android:id="@+id/iv_coverColorSelector_color1"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_margin="8dp"
+            android:src="@color/coverColor_1"
+            tools:padding="8dp" />
+
+        <ImageView
+            android:id="@+id/iv_coverColorSelector_color2"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_margin="8dp"
+            android:src="@color/coverColor_2" />
+
+        <ImageView
+            android:id="@+id/iv_coverColorSelector_color3"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_margin="8dp"
+            android:src="@color/coverColor_3" />
+
+        <ImageView
+            android:id="@+id/iv_coverColorSelector_color4"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_margin="8dp"
+            android:src="@color/coverColor_4" />
+
+        <ImageView
+            android:id="@+id/iv_coverColorSelector_color5"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_margin="8dp"
+            android:src="@color/coverColor_5" />
+    </LinearLayout>
+
+    <LinearLayout
+        android:id="@+id/viewGroup_coverColorSelector_2"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal"
+        app:layout_constraintLeft_toLeftOf="@id/viewGroup_coverColorSelector_1"
+        app:layout_constraintTop_toBottomOf="@id/viewGroup_coverColorSelector_1">
+
+        <ImageView
+            android:id="@+id/iv_coverColorSelector_color6"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_margin="8dp"
+            android:src="@color/coverColor_6" />
+
+        <ImageView
+            android:id="@+id/iv_coverColorSelector_color7"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_margin="8dp"
+            android:src="@color/coverColor_7" />
+
+        <ImageView
+            android:id="@+id/iv_coverColorSelector_color8"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_margin="8dp"
+            android:src="@color/coverColor_8" />
+
+        <ImageView
+            android:id="@+id/iv_coverColorSelector_color9"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_margin="8dp"
+            android:src="@color/coverColor_9" />
+
+        <ImageView
+            android:id="@+id/iv_coverColorSelector_color10"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_margin="8dp"
+            android:src="@color/coverColor_10" />
+    </LinearLayout>
+</merge>

+ 5 - 87
src/main/res/layout/view_fron_cover_editor.xml

@@ -93,95 +93,13 @@
         app:layout_constraintRight_toRightOf="@id/et_frontCoverEditor_projectName"
         app:layout_constraintTop_toBottomOf="@id/et_frontCoverEditor_author" />
 
-    <LinearLayout
-        android:id="@+id/viewGroup_frontCoverEditor_colorGroup1"
-        android:layout_width="0dp"
-        android:layout_height="wrap_content"
-        android:layout_marginTop="16dp"
+    <com.bomostory.sceneeditmodule.cover.CoverColorSelectorView
+        android:id="@+id/coverColorSelectorView_frontCoverEditor"
+        android:layout_width="wrap_content"
         app:layout_constraintLeft_toLeftOf="@id/et_frontCoverEditor_projectName"
         app:layout_constraintRight_toRightOf="@id/et_frontCoverEditor_projectName"
-        app:layout_constraintTop_toBottomOf="@id/spinner_frontCoverEditor_category">
-
-        <ImageView
-            android:id="@+id/iv_frontCoverEditor_color1"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_margin="8dp"
-            android:src="#f5a623"
-            tools:padding="8dp" />
-
-        <ImageView
-            android:id="@+id/iv_frontCoverEditor_color2"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_margin="8dp"
-            android:src="#c82185" />
-
-        <ImageView
-            android:id="@+id/iv_frontCoverEditor_color3"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_margin="8dp"
-            android:src="#de0007" />
-
-        <ImageView
-            android:id="@+id/iv_frontCoverEditor_color4"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_margin="8dp"
-            android:src="#008cbf" />
-
-        <ImageView
-            android:id="@+id/iv_frontCoverEditor_color5"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_margin="8dp"
-            android:src="#33691e" />
-    </LinearLayout>
-
-    <LinearLayout
-        android:id="@+id/viewGroup_frontCoverEditor_colorGroup2"
-        android:layout_width="0dp"
         android:layout_height="wrap_content"
-        app:layout_constraintLeft_toLeftOf="@id/viewGroup_frontCoverEditor_colorGroup1"
-        app:layout_constraintRight_toRightOf="@id/viewGroup_frontCoverEditor_colorGroup1"
-        app:layout_constraintTop_toBottomOf="@id/viewGroup_frontCoverEditor_colorGroup1">
-
-        <ImageView
-            android:id="@+id/iv_frontCoverEditor_color6"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_margin="8dp"
-            android:src="#827717" />
-
-        <ImageView
-            android:id="@+id/iv_frontCoverEditor_color7"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_margin="8dp"
-            android:src="#ba5f43" />
-
-        <ImageView
-            android:id="@+id/iv_frontCoverEditor_color8"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_margin="8dp"
-            android:src="#4e342e" />
-
-        <ImageView
-            android:id="@+id/iv_frontCoverEditor_color9"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_margin="8dp"
-            android:src="#424242" />
-
-        <ImageView
-            android:id="@+id/iv_frontCoverEditor_color10"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_margin="8dp"
-            android:src="#37474f" />
-    </LinearLayout>
+        app:layout_constraintTop_toBottomOf="@id/spinner_frontCoverEditor_category" />
 
     <Button
         android:id="@+id/btn_frontCoverEditor_save"
@@ -194,7 +112,7 @@
         android:textColor="@color/cocoa"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintRight_toRightOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/viewGroup_frontCoverEditor_colorGroup2" />
+        app:layout_constraintTop_toBottomOf="@id/coverColorSelectorView_frontCoverEditor" />
 
     <Button
         android:id="@+id/btn_frontCoverEditor_chooseScene"

+ 13 - 0
src/main/res/values/colors_cover.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <color name="coverColor_1">#f5a623</color>
+    <color name="coverColor_2">#c82185</color>
+    <color name="coverColor_3">#de0007</color>
+    <color name="coverColor_4">#008cbf</color>
+    <color name="coverColor_5">#33691e</color>
+    <color name="coverColor_6">#827717</color>
+    <color name="coverColor_7">#ba5f43</color>
+    <color name="coverColor_8">#4e342e</color>
+    <color name="coverColor_9">#424242</color>
+    <color name="coverColor_10">#37474f</color>
+</resources>

+ 2 - 0
src/main/res/values/dimens.xml

@@ -7,4 +7,6 @@
     <dimen name="actor_width">120dp</dimen>
     <dimen name="edit_actor_padding">24dp</dimen>
     <dimen name="dialogue_padding">50dp</dimen>
+
+    <dimen name="cover_color_item_padding">8dp</dimen>
 </resources>