1 /* <lambda>null2 * 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 package com.android.test.silkfx.materials 17 18 import android.app.Activity 19 import android.content.Intent 20 import android.graphics.Bitmap 21 import android.graphics.BitmapFactory 22 import android.graphics.Color 23 import android.os.Bundle 24 import android.util.TypedValue 25 import android.view.View 26 import android.widget.ImageView 27 import android.widget.SeekBar 28 import android.widget.Switch 29 import android.widget.TextView 30 import com.android.test.silkfx.R 31 32 class GlassActivity : Activity(), SeekBar.OnSeekBarChangeListener { 33 34 lateinit var backgroundButton1: ImageView 35 lateinit var backgroundButton2: ImageView 36 lateinit var backgroundButton3: ImageView 37 lateinit var backgroundView: ImageView 38 lateinit var materialView: GlassView 39 lateinit var lightMaterialSwitch: Switch 40 lateinit var noiseOpacitySeekBar: SeekBar 41 lateinit var materialOpacitySeekBar: SeekBar 42 lateinit var scrimOpacitySeekBar: SeekBar 43 lateinit var zoomSeekBar: SeekBar 44 lateinit var blurRadiusSeekBar: SeekBar 45 lateinit var noiseOpacityValue: TextView 46 lateinit var materialOpacityValue: TextView 47 lateinit var scrimOpacityValue: TextView 48 lateinit var blurRadiusValue: TextView 49 lateinit var zoomValue: TextView 50 lateinit var textOverlay: TextView 51 52 var background: Bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888) 53 set(value) { 54 field = value 55 backgroundView.setImageBitmap(background) 56 materialView.backgroundBitmap = background 57 } 58 59 override fun onCreate(savedInstanceState: Bundle?) { 60 super.onCreate(savedInstanceState) 61 setContentView(R.layout.activity_glass) 62 backgroundButton1 = requireViewById(R.id.background1) 63 backgroundButton2 = requireViewById(R.id.background2) 64 backgroundButton3 = requireViewById(R.id.background3) 65 backgroundView = requireViewById(R.id.background) 66 lightMaterialSwitch = requireViewById(R.id.lightMaterialSwitch) 67 materialView = requireViewById(R.id.materialView) 68 materialOpacitySeekBar = requireViewById(R.id.materialOpacity) 69 blurRadiusSeekBar = requireViewById(R.id.blurRadius) 70 zoomSeekBar = requireViewById(R.id.zoom) 71 noiseOpacitySeekBar = requireViewById(R.id.noiseOpacity) 72 scrimOpacitySeekBar = requireViewById(R.id.scrimOpacity) 73 noiseOpacityValue = requireViewById(R.id.noiseOpacityValue) 74 materialOpacityValue = requireViewById(R.id.materialOpacityValue) 75 scrimOpacityValue = requireViewById(R.id.scrimOpacityValue) 76 blurRadiusValue = requireViewById(R.id.blurRadiusValue) 77 zoomValue = requireViewById(R.id.zoomValue) 78 textOverlay = requireViewById(R.id.textOverlay) 79 80 background = BitmapFactory.decodeResource(resources, R.drawable.background1) 81 82 blurRadiusSeekBar.setOnSeekBarChangeListener(this) 83 materialOpacitySeekBar.setOnSeekBarChangeListener(this) 84 noiseOpacitySeekBar.setOnSeekBarChangeListener(this) 85 scrimOpacitySeekBar.setOnSeekBarChangeListener(this) 86 87 arrayOf(blurRadiusSeekBar, materialOpacitySeekBar, noiseOpacitySeekBar, 88 scrimOpacitySeekBar, zoomSeekBar).forEach { 89 it.setOnSeekBarChangeListener(this) 90 onProgressChanged(it, it.progress, fromUser = false) 91 } 92 93 lightMaterialSwitch.setOnCheckedChangeListener { _, isChecked -> 94 materialView.color = if (isChecked) Color.WHITE else Color.BLACK 95 textOverlay.setTextColor(if (isChecked) Color.BLACK else Color.WHITE) 96 } 97 } 98 99 override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { 100 when (seekBar) { 101 blurRadiusSeekBar -> { 102 materialView.blurRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 103 progress.toFloat(), resources.displayMetrics) 104 blurRadiusValue.text = progress.toString() 105 } 106 materialOpacitySeekBar -> { 107 materialView.materialOpacity = progress / seekBar.max.toFloat() 108 materialOpacityValue.text = progress.toString() 109 } 110 noiseOpacitySeekBar -> { 111 materialView.noiseOpacity = progress / seekBar.max.toFloat() 112 noiseOpacityValue.text = progress.toString() 113 } 114 scrimOpacitySeekBar -> { 115 materialView.scrimOpacity = progress / seekBar.max.toFloat() 116 scrimOpacityValue.text = progress.toString() 117 } 118 zoomSeekBar -> { 119 materialView.zoom = progress / seekBar.max.toFloat() 120 zoomValue.text = progress.toString() 121 } 122 else -> throw IllegalArgumentException("Unknown seek bar") 123 } 124 } 125 126 override fun onStop() { 127 super.onStop() 128 materialView.resetGyroOffsets() 129 } 130 131 override fun onStartTrackingTouch(seekBar: SeekBar?) {} 132 override fun onStopTrackingTouch(seekBar: SeekBar?) {} 133 134 fun onBackgroundClick(view: View) { 135 val resource = when (view) { 136 backgroundButton1 -> R.drawable.background1 137 backgroundButton2 -> R.drawable.background2 138 backgroundButton3 -> R.drawable.background3 139 else -> throw IllegalArgumentException("Invalid button") 140 } 141 142 background = BitmapFactory.decodeResource(resources, resource) 143 } 144 145 fun onPickImageClick(view: View) { 146 val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply { 147 addCategory(Intent.CATEGORY_OPENABLE) 148 type = "image/*" 149 } 150 startActivityForResult(intent, 0) 151 } 152 153 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 154 if (resultCode === RESULT_OK) { 155 data?.data?.also { 156 contentResolver.openFileDescriptor(it, "r").let { 157 background = BitmapFactory.decodeFileDescriptor(it?.fileDescriptor) 158 } 159 } 160 } 161 } 162 }