Debian Codegraph @

tests/SilkFX/src/com/android/test/silkfx/hdr/GlowingCard.kt

raw
1 /*
2 * Copyright (C) 2020 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
17 package com.android.test.silkfx.hdr
18
19 import android.content.Context
20 import android.graphics.Canvas
21 import android.graphics.Color
22 import android.graphics.LinearGradient
23 import android.graphics.Paint
24 import android.graphics.RectF
25 import android.graphics.Shader
26 import android.util.AttributeSet
27 import com.android.test.silkfx.common.BaseDrawingView
28
29 class GlowingCard : BaseDrawingView {
30
31 constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
32
33 val radius: Float
34 var COLOR_MAXIMIZER = 1f
35
36 init {
37 radius = 10.dp()
38 }
39
40 fun setGlowIntensity(multiplier: Float) {
41 COLOR_MAXIMIZER = multiplier
42 invalidate()
43 }
44
45 override fun setPressed(pressed: Boolean) {
46 super.setPressed(pressed)
47 invalidate()
48 }
49
50 override fun onDraw(canvas: Canvas) {
51 super.onDraw(canvas)
52 val paint = Paint()
53 paint.isAntiAlias = true
54 val rect = RectF(0f, 0f, width.toFloat(), height.toFloat())
55 val glowColor = Color.pack(.5f * COLOR_MAXIMIZER, .4f * COLOR_MAXIMIZER,
56 .75f * COLOR_MAXIMIZER, 1f, scRGB)
57
58 if (isPressed) {
59 paint.setColor(Color.pack(2f, 2f, 2f, 1f, scRGB))
60 paint.strokeWidth = 4.dp()
61 paint.style = Paint.Style.FILL
62 paint.shader = LinearGradient(rect.left, rect.bottom, rect.right, rect.top,
63 glowColor,
64 Color.pack(0f, 0f, 0f, 0f, scRGB),
65 Shader.TileMode.CLAMP)
66 canvas.drawRoundRect(rect, radius, radius, paint)
67 }
68
69 rect.inset(3.dp(), 3.dp())
70
71 paint.setColor(Color.pack(.14f, .14f, .14f, .8f, scRGB))
72 paint.style = Paint.Style.FILL
73 paint.shader = null
74 canvas.drawRoundRect(rect, radius, radius, paint)
75
76 rect.inset(5.dp(), 5.dp())
77 paint.textSize = 14.dp()
78 paint.isFakeBoldText = true
79
80 paint.color = Color.WHITE
81 canvas.drawText("glow = scRGB{${Color.red(glowColor)}, ${Color.green(glowColor)}, " +
82 "${Color.blue(glowColor)}}", rect.left, rect.centerY(), paint)
83 canvas.drawText("(press to activate)", rect.left, rect.bottom, paint)
84 }
85 }