| 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.Paint |
| 22 |
import android.graphics.RadialGradient |
| 23 |
import android.graphics.RectF |
| 24 |
import android.graphics.Shader |
| 25 |
import android.util.AttributeSet |
| 26 |
import com.android.test.silkfx.common.BaseDrawingView |
| 27 |
import kotlin.math.min |
| 28 |
|
| 29 |
class RadialGlow : BaseDrawingView { |
| 30 |
|
| 31 |
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) |
| 32 |
|
| 33 |
var glowToggle = false |
| 34 |
|
| 35 |
val glowColor = color(4f, 3.3f, 2.8f) |
| 36 |
val bgColor = color(.15f, .15f, .15f) |
| 37 |
val fgColor = color(.51f, .52f, .50f, .4f) |
| 38 |
var glow: RadialGradient |
| 39 |
|
| 40 |
init { |
| 41 |
glow = RadialGradient(0f, 0f, 100.dp(), glowColor, bgColor, Shader.TileMode.CLAMP) |
| 42 |
isClickable = true |
| 43 |
setOnClickListener { |
| 44 |
glowToggle = !glowToggle |
| 45 |
invalidate() |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { |
| 50 |
super.onSizeChanged(w, h, oldw, oldh) |
| 51 |
glow = RadialGradient(0f, 0f, |
| 52 |
min(w, h).toFloat(), glowColor, bgColor, Shader.TileMode.CLAMP) |
| 53 |
} |
| 54 |
|
| 55 |
override fun onDraw(canvas: Canvas) { |
| 56 |
super.onDraw(canvas) |
| 57 |
val radius = 10.dp() |
| 58 |
|
| 59 |
val paint = Paint() |
| 60 |
paint.isDither = true |
| 61 |
paint.isAntiAlias = true |
| 62 |
paint.textSize = 18.dp() |
| 63 |
paint.textAlign = Paint.Align.CENTER |
| 64 |
|
| 65 |
val rect = RectF(0f, 0f, width.toFloat(), height.toFloat()) |
| 66 |
|
| 67 |
paint.setColor(bgColor) |
| 68 |
canvas.drawRoundRect(rect, radius, radius, paint) |
| 69 |
|
| 70 |
if (glowToggle) { |
| 71 |
paint.shader = glow |
| 72 |
canvas.save() |
| 73 |
val frac = (drawingTime % 5000) / 5000f |
| 74 |
canvas.translate(rect.width() * frac, rect.height() - (rect.height() * frac)) |
| 75 |
canvas.drawPaint(paint) |
| 76 |
canvas.restore() |
| 77 |
paint.shader = null |
| 78 |
invalidate() |
| 79 |
} |
| 80 |
|
| 81 |
paint.setColor(fgColor) |
| 82 |
val innerRect = RectF(rect) |
| 83 |
innerRect.inset(rect.width() / 4, rect.height() / 4) |
| 84 |
canvas.drawRoundRect(innerRect, radius, radius, paint) |
| 85 |
|
| 86 |
paint.setColor(color(1f, 1f, 1f)) |
| 87 |
canvas.drawText("Tap to toggle animation", rect.centerX(), innerRect.top - 4.dp(), paint) |
| 88 |
canvas.drawText("Outside text", rect.centerX(), rect.bottom - 4.dp(), paint) |
| 89 |
canvas.drawText("Inside text", innerRect.centerX(), innerRect.bottom - 4.dp(), paint) |
| 90 |
} |
| 91 |
} |