소스 검색

Set elevation effect below Android KitKat

cooperku_kdanmobile 5 년 전
부모
커밋
a89d6a373d

+ 27 - 0
src/main/java/android/support/v7/widget/MyRoundRectDrawableWithShadow.java

@@ -0,0 +1,27 @@
+package android.support.v7.widget;
+
+import android.content.res.ColorStateList;
+import android.content.res.Resources;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.RectF;
+
+public class MyRoundRectDrawableWithShadow extends RoundRectDrawableWithShadow {
+    public MyRoundRectDrawableWithShadow(Resources resources, int backgroundColor, float radius, float shadowSize, float maxShadowSize) {
+        super(resources, getColor(backgroundColor), radius, shadowSize, maxShadowSize);
+        initRoundRectHelper();
+    }
+
+    private static ColorStateList getColor(int color) {
+        return new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, new int[]{-android.R.attr.state_enabled}, new int[]{-android.R.attr.state_checked}, new int[]{android.R.attr.state_pressed}}, new int[]{color, color, color, color});
+    }
+
+    private void initRoundRectHelper() {
+        RoundRectDrawableWithShadow.sRoundRectHelper = new RoundRectDrawableWithShadow.RoundRectHelper() {
+            @Override
+            public void drawRoundRect(Canvas canvas, RectF bounds, float cornerRadius, Paint paint) {
+                canvas.drawRoundRect(bounds, cornerRadius, cornerRadius, paint);
+            }
+        };
+    }
+}

+ 13 - 1
src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -5,10 +5,10 @@ import android.arch.lifecycle.MutableLiveData
 import android.arch.lifecycle.ViewModel
 import android.content.Context
 import android.graphics.Color
-import android.graphics.Point
 import android.net.Uri
 import android.os.Build
 import android.support.v4.content.ContextCompat
+import android.support.v7.widget.MyRoundRectDrawableWithShadow
 import android.view.Gravity
 import android.view.LayoutInflater
 import android.view.View
@@ -535,6 +535,8 @@ class ReaderViewModel(private val readerModelManager: ReaderModelManager, val ur
 
                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                         popupWindow?.elevation = context.resources.getDimension(R.dimen.reader_context_menu_window_elevation)
+                    } else {
+                        setElevationBelowAndroidKitKat(context, contentView)
                     }
 
                     popupWindow?.isOutsideTouchable = false
@@ -543,6 +545,16 @@ class ReaderViewModel(private val readerModelManager: ReaderModelManager, val ur
                 }
             }
 
+            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)
+                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+                    view?.background = drawable
+                } else {
+                    view?.setBackgroundDrawable(drawable)
+                }
+            }
+
             private fun createMarkupContextMenuView(context: Context): View {
                 /** Just set HIGH_LIGHT then highlight, strike, underline will be triggered. **/
                 /** WTF !!!!!!!!!!! **/