1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.systemui.shared.shadow 17 18 import android.content.Context 19 import android.content.res.TypedArray 20 import android.graphics.Canvas 21 import android.graphics.drawable.Drawable 22 import android.util.AttributeSet 23 import android.widget.TextView 24 import com.android.systemui.shared.R 25 import com.android.systemui.shared.shadow.DoubleShadowTextHelper.ShadowInfo 26 import com.android.systemui.shared.shadow.DoubleShadowTextHelper.applyShadows 27 28 /** Extension of [TextView] which draws two shadows on the text (ambient and key shadows} */ 29 open class DoubleShadowTextView 30 @JvmOverloads 31 constructor( 32 context: Context, 33 attrs: AttributeSet? = null, 34 defStyleAttr: Int = 0, 35 defStyleRes: Int = 0, 36 ) : TextView(context, attrs, defStyleAttr, defStyleRes) { 37 private lateinit var mKeyShadowInfo: ShadowInfo 38 private lateinit var mAmbientShadowInfo: ShadowInfo 39 40 init { 41 updateShadowDrawables( 42 context.obtainStyledAttributes( 43 attrs, 44 R.styleable.DoubleShadowTextView, 45 defStyleAttr, 46 defStyleRes, 47 ) 48 ) 49 } 50 updateShadowDrawablesnull51 private fun updateShadowDrawables(attributes: TypedArray) { 52 val drawableSize: Int 53 val drawableInsetSize: Int 54 try { 55 val keyShadowBlur = 56 attributes.getDimension(R.styleable.DoubleShadowTextView_keyShadowBlur, 0f) 57 val keyShadowOffsetX = 58 attributes.getDimension(R.styleable.DoubleShadowTextView_keyShadowOffsetX, 0f) 59 val keyShadowOffsetY = 60 attributes.getDimension(R.styleable.DoubleShadowTextView_keyShadowOffsetY, 0f) 61 val keyShadowAlpha = 62 attributes.getFloat(R.styleable.DoubleShadowTextView_keyShadowAlpha, 0f) 63 mKeyShadowInfo = 64 ShadowInfo(keyShadowBlur, keyShadowOffsetX, keyShadowOffsetY, keyShadowAlpha) 65 val ambientShadowBlur = 66 attributes.getDimension(R.styleable.DoubleShadowTextView_ambientShadowBlur, 0f) 67 val ambientShadowOffsetX = 68 attributes.getDimension(R.styleable.DoubleShadowTextView_ambientShadowOffsetX, 0f) 69 val ambientShadowOffsetY = 70 attributes.getDimension(R.styleable.DoubleShadowTextView_ambientShadowOffsetY, 0f) 71 val ambientShadowAlpha = 72 attributes.getFloat(R.styleable.DoubleShadowTextView_ambientShadowAlpha, 0f) 73 mAmbientShadowInfo = 74 ShadowInfo( 75 ambientShadowBlur, 76 ambientShadowOffsetX, 77 ambientShadowOffsetY, 78 ambientShadowAlpha, 79 ) 80 drawableSize = 81 attributes.getDimensionPixelSize( 82 R.styleable.DoubleShadowTextView_drawableIconSize, 83 0, 84 ) 85 drawableInsetSize = 86 attributes.getDimensionPixelSize( 87 R.styleable.DoubleShadowTextView_drawableIconInsetSize, 88 0, 89 ) 90 } finally { 91 attributes.recycle() 92 } 93 94 val drawables = arrayOf<Drawable?>(null, null, null, null) 95 for ((index, drawable) in compoundDrawablesRelative.withIndex()) { 96 if (drawable == null) continue 97 drawables[index] = 98 DoubleShadowIconDrawable( 99 mKeyShadowInfo, 100 mAmbientShadowInfo, 101 drawable, 102 drawableSize, 103 drawableInsetSize, 104 ) 105 } 106 setCompoundDrawablesRelative(drawables[0], drawables[1], drawables[2], drawables[3]) 107 } 108 setTextAppearancenull109 override fun setTextAppearance(resId: Int) { 110 super.setTextAppearance(resId) 111 updateShadowDrawables( 112 context.obtainStyledAttributes(resId, R.styleable.DoubleShadowTextView) 113 ) 114 } 115 onDrawnull116 public override fun onDraw(canvas: Canvas) { 117 applyShadows(mKeyShadowInfo, mAmbientShadowInfo, this, canvas) { super.onDraw(canvas) } 118 } 119 } 120