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