1 package com.android.keyguard 2 3 import android.content.Context 4 import android.graphics.Canvas 5 import android.util.AttributeSet 6 import android.view.View 7 import android.widget.FrameLayout 8 9 class KeyguardClockFrame( 10 context: Context, 11 attrs: AttributeSet, 12 ) : FrameLayout(context, attrs) { 13 private var drawAlpha: Int = 255 14 onSetAlphanull15 protected override fun onSetAlpha(alpha: Int): Boolean { 16 // Ignore alpha passed from View, prefer to compute it from set values 17 drawAlpha = (255 * this.alpha * transitionAlpha).toInt() 18 return true 19 } 20 dispatchDrawnull21 protected override fun dispatchDraw(canvas: Canvas) { 22 saveCanvasAlpha(this, canvas, drawAlpha) { super.dispatchDraw(it) } 23 } 24 25 companion object { 26 @JvmStatic saveCanvasAlphanull27 fun saveCanvasAlpha(view: View, canvas: Canvas, alpha: Int, drawFunc: (Canvas) -> Unit) { 28 if (alpha <= 0) { 29 // Zero Alpha -> skip drawing phase 30 return 31 } 32 33 if (alpha >= 255) { 34 // Max alpha -> no need for layer 35 drawFunc(canvas) 36 return 37 } 38 39 // Find x & y of view on screen 40 var (x, y) = 41 run { 42 val locationOnScreen = IntArray(2) 43 view.getLocationOnScreen(locationOnScreen) 44 Pair(locationOnScreen[0].toFloat(), locationOnScreen[1].toFloat()) 45 } 46 47 val restoreTo = 48 canvas.saveLayerAlpha(-1f * x, -1f * y, x + view.width, y + view.height, alpha) 49 drawFunc(canvas) 50 canvas.restoreToCount(restoreTo) 51 } 52 } 53 } 54