ソースを参照

Change radius of TextBox buttons in context menu programmatically

cooperku_kdanmobile 5 年 前
コミット
7e89d5b6b0

+ 47 - 7
src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -5,6 +5,11 @@ import android.arch.lifecycle.MutableLiveData
 import android.arch.lifecycle.ViewModel
 import android.content.Context
 import android.graphics.Color
+import android.graphics.PointF
+import android.graphics.drawable.ColorDrawable
+import android.graphics.drawable.Drawable
+import android.graphics.drawable.GradientDrawable
+import android.graphics.drawable.StateListDrawable
 import android.net.Uri
 import android.os.Build
 import android.support.v4.content.ContextCompat
@@ -545,13 +550,17 @@ class ReaderViewModel(private val readerModelManager: ReaderModelManager, val ur
                 }
             }
 
-            private fun setElevationBelowAndroidKitKat(context: Context, view: View?) {
+            private fun setElevationBelowAndroidKitKat(context: Context, view: View) {
                 val elevation = context.resources.getDimension(R.dimen.reader_context_menu_window_elevation)
                 val drawable = MyRoundRectDrawableWithShadow(context.resources, 0x00000000, 0f, elevation, elevation)
+                setViewBackground(view, drawable)
+            }
+
+            private fun setViewBackground(view: View, drawable: Drawable) {
                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
-                    view?.background = drawable
+                    view.background = drawable
                 } else {
-                    view?.setBackgroundDrawable(drawable)
+                    view.setBackgroundDrawable(drawable)
                 }
             }
 
@@ -612,19 +621,50 @@ class ReaderViewModel(private val readerModelManager: ReaderModelManager, val ur
             private fun createTextBoxContextMenuView(context: Context): View {
                 val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
                 val contentView = layoutInflater.inflate(R.layout.view_context_menu_textbox, null)
-                contentView.findViewById<TextView>(R.id.btnDelete_contextMenu).setOnClickListener {
+
+                fun createTextBoxSelector(topLeft: Boolean, topRight: Boolean, bottomRight: Boolean, bottomLeft: Boolean): Drawable {
+                    val radius = context.resources.getDimension(R.dimen.reader_context_menu_text_box_radius)
+                    val topLeftRadius = if (topLeft) PointF(radius, radius) else PointF()
+                    val topRightRadius = if (topRight) PointF(radius, radius) else PointF()
+                    val bottomRightRadius = if (bottomRight) PointF(radius, radius) else PointF()
+                    val bottomLeftRadius = if (bottomLeft) PointF(radius, radius) else PointF()
+                    val cornerRadii = floatArrayOf(topLeftRadius.x, topLeftRadius.y, topRightRadius.x, topRightRadius.y, bottomRightRadius.x, bottomRightRadius.y, bottomLeftRadius.x, bottomLeftRadius.y)
+
+                    val normalDrawable = GradientDrawable()
+                    normalDrawable.cornerRadii = cornerRadii
+                    normalDrawable.setColor(ContextCompat.getColor(context, R.color.reader_contextMenu_bgNormal))
+                    val pressedDrawable = GradientDrawable()
+                    pressedDrawable.cornerRadii = cornerRadii
+                    pressedDrawable.setColor(ContextCompat.getColor(context, R.color.reader_contextMenu_bgPressed))
+                    val drawable = StateListDrawable()
+                    drawable.addState(intArrayOf(android.R.attr.state_pressed), pressedDrawable)
+                    drawable.addState(intArrayOf(android.R.attr.state_enabled), normalDrawable)
+                    return drawable
+                }
+
+                val btnDelete = contentView.findViewById<TextView>(R.id.btnDelete_contextMenu)
+                val btnStyle = contentView.findViewById<TextView>(R.id.btnStyle_contextMenu)
+                val btnEdit = contentView.findViewById<TextView>(R.id.btnEdit_contextMenu)
+                val btnCopy = contentView.findViewById<TextView>(R.id.btnCopy_contextMenu)
+
+                setViewBackground(btnDelete, createTextBoxSelector(true, false, false, true))
+                setViewBackground(btnStyle, createTextBoxSelector(false, false, false, false))
+                setViewBackground(btnEdit, createTextBoxSelector(false, false, false, false))
+                setViewBackground(btnCopy, createTextBoxSelector(false, true, true, false))
+
+                btnDelete.setOnClickListener {
                     textBoxContextMenuActionListener?.onDelete()
                     dismissPopupWindow()
                 }
-                contentView.findViewById<TextView>(R.id.btnStyle_contextMenu).setOnClickListener {
+                btnStyle.setOnClickListener {
                     textBoxContextMenuActionListener?.onEditStyle()
                     dismissPopupWindow()
                 }
-                contentView.findViewById<TextView>(R.id.btnEdit_contextMenu).setOnClickListener {
+                btnEdit.setOnClickListener {
                     textBoxContextMenuActionListener?.onEditText()
                     dismissPopupWindow()
                 }
-                contentView.findViewById<TextView>(R.id.btnCopy_contextMenu).setOnClickListener {
+                btnCopy.setOnClickListener {
                     textBoxContextMenuActionListener?.onCopy()
                     dismissPopupWindow()
                 }

+ 1 - 1
src/main/res/drawable/bg_context_menu_item_normal.xml

@@ -1,5 +1,5 @@
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle" >
     <solid android:color="@color/reader_contextMenu_bgNormal" />
-    <corners android:radius="4dp"/>
+    <corners android:radius="@dimen/reader_context_menu_text_box_radius"/>
 </shape>

+ 1 - 1
src/main/res/drawable/bg_context_menu_item_pressed.xml

@@ -1,5 +1,5 @@
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle" >
     <solid android:color="@color/reader_contextMenu_bgPressed" />
-    <corners android:radius="4dp"/>
+    <corners android:radius="@dimen/reader_context_menu_text_box_radius"/>
 </shape>

+ 0 - 5
src/main/res/drawable/bg_context_menu_item_text_box_center.xml

@@ -1,5 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<selector xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:drawable="@drawable/bg_context_menu_item_text_box_center_pressed" android:state_pressed="true"/>
-    <item android:drawable="@drawable/bg_context_menu_item_text_box_center_normal"/>
-</selector>

+ 0 - 4
src/main/res/drawable/bg_context_menu_item_text_box_center_normal.xml

@@ -1,4 +0,0 @@
-<shape xmlns:android="http://schemas.android.com/apk/res/android"
-    android:shape="rectangle" >
-    <solid android:color="@color/reader_contextMenu_bgNormal" />
-</shape>

+ 0 - 4
src/main/res/drawable/bg_context_menu_item_text_box_center_pressed.xml

@@ -1,4 +0,0 @@
-<shape xmlns:android="http://schemas.android.com/apk/res/android"
-    android:shape="rectangle" >
-    <solid android:color="@color/reader_contextMenu_bgPressed" />
-</shape>

+ 0 - 5
src/main/res/drawable/bg_context_menu_item_text_box_left.xml

@@ -1,5 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<selector xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:drawable="@drawable/bg_context_menu_item_text_box_left_pressed" android:state_pressed="true"/>
-    <item android:drawable="@drawable/bg_context_menu_item_text_box_left_normal"/>
-</selector>

+ 0 - 5
src/main/res/drawable/bg_context_menu_item_text_box_left_normal.xml

@@ -1,5 +0,0 @@
-<shape xmlns:android="http://schemas.android.com/apk/res/android"
-    android:shape="rectangle" >
-    <solid android:color="@color/reader_contextMenu_bgNormal" />
-    <corners android:topLeftRadius="4dp" android:bottomLeftRadius="4dp"/>
-</shape>

+ 0 - 5
src/main/res/drawable/bg_context_menu_item_text_box_left_pressed.xml

@@ -1,5 +0,0 @@
-<shape xmlns:android="http://schemas.android.com/apk/res/android"
-    android:shape="rectangle" >
-    <solid android:color="@color/reader_contextMenu_bgPressed" />
-    <corners android:topLeftRadius="4dp" android:bottomLeftRadius="4dp"/>
-</shape>

+ 0 - 5
src/main/res/drawable/bg_context_menu_item_text_box_right.xml

@@ -1,5 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<selector xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:drawable="@drawable/bg_context_menu_item_text_box_right_pressed" android:state_pressed="true"/>
-    <item android:drawable="@drawable/bg_context_menu_item_text_box_right_normal"/>
-</selector>

+ 0 - 5
src/main/res/drawable/bg_context_menu_item_text_box_right_normal.xml

@@ -1,5 +0,0 @@
-<shape xmlns:android="http://schemas.android.com/apk/res/android"
-    android:shape="rectangle" >
-    <solid android:color="@color/reader_contextMenu_bgNormal" />
-    <corners android:topRightRadius="4dp" android:bottomRightRadius="4dp"/>
-</shape>

+ 0 - 5
src/main/res/drawable/bg_context_menu_item_text_box_right_pressed.xml

@@ -1,5 +0,0 @@
-<shape xmlns:android="http://schemas.android.com/apk/res/android"
-    android:shape="rectangle" >
-    <solid android:color="@color/reader_contextMenu_bgPressed" />
-    <corners android:topRightRadius="4dp" android:bottomRightRadius="4dp"/>
-</shape>

+ 1 - 1
src/main/res/drawable/bg_context_menu_window.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android">
     <solid android:color="@color/reader_contextMenu_bgNormal"/>
-    <corners android:radius="4dp"/>
+    <corners android:radius="@dimen/reader_context_menu_text_box_radius"/>
 </shape>

+ 0 - 4
src/main/res/layout/view_context_menu_textbox.xml

@@ -9,24 +9,20 @@
     <TextView
         android:id="@+id/btnDelete_contextMenu"
         style="@style/ContextMenuItemTheme"
-        android:background="@drawable/bg_context_menu_item_text_box_left"
         android:text="@string/contextMenu_textbox_delete"/>
 
     <TextView
         android:id="@+id/btnStyle_contextMenu"
         style="@style/ContextMenuItemTheme"
-        android:background="@drawable/bg_context_menu_item_text_box_center"
         android:text="@string/contextMenu_textbox_style"/>
 
     <TextView
         android:id="@+id/btnEdit_contextMenu"
         style="@style/ContextMenuItemTheme"
-        android:background="@drawable/bg_context_menu_item_text_box_center"
         android:text="@string/contextMenu_textbox_edit"/>
 
     <TextView
         android:id="@+id/btnCopy_contextMenu"
         style="@style/ContextMenuItemTheme"
-        android:background="@drawable/bg_context_menu_item_text_box_right"
         android:text="@string/contextMenu_textbox_copy"/>
 </LinearLayout>

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

@@ -30,4 +30,5 @@
     <dimen name="reader_context_menu_window_position_offset">10dp</dimen>
     <dimen name="reader_context_menu_window_position_margin">40dp</dimen>
     <dimen name="reader_context_menu_window_elevation">4dp</dimen>
+    <dimen name="reader_context_menu_text_box_radius">4dp</dimen>
 </resources>