1
+ package com.hfad.findme
2
+
3
+ import android.content.Context
4
+ import android.graphics.*
5
+ import android.util.AttributeSet
6
+ import androidx.appcompat.widget.AppCompatImageView
7
+
8
+ class SpotlightImageView @JvmOverloads constructor(
9
+ context : Context , attrs : AttributeSet ? = null
10
+ ) : AppCompatImageView(context, attrs) {
11
+ private var paint = Paint ()
12
+ private var shouldDrawSpotlight = false
13
+ private var gameOver = false
14
+
15
+ private lateinit var winnerRect: RectF
16
+ private var androidBitmapX = 0f
17
+ private var androidBitmapY = 0f
18
+
19
+ private val bitmapAndroid = BitmapFactory .decodeResource(
20
+ resources,
21
+ R .drawable.android
22
+ )
23
+ private val spotlight = BitmapFactory .decodeResource(resources, R .drawable.mask)
24
+
25
+ private var shader: Shader
26
+ // private val shaderMatrix = Matrix()
27
+
28
+ init {
29
+ val bitmap = Bitmap .createBitmap(spotlight.width, spotlight.height, Bitmap .Config .ARGB_8888 )
30
+ val canvas = Canvas (bitmap)
31
+ val shaderPaint = Paint (Paint .ANTI_ALIAS_FLAG )
32
+ // Draw a black rectangle
33
+ shaderPaint.color = Color .BLACK
34
+ canvas.drawRect(0.0f , 0.0f , spotlight.width.toFloat(), spotlight.height.toFloat(), shaderPaint)
35
+ // Mask out the spotlight
36
+ shaderPaint.xfermode = PorterDuffXfermode (PorterDuff .Mode .DST_OUT )
37
+ canvas.drawBitmap(spotlight, 0.0f , 0.0f , shaderPaint)
38
+ // Create the shader
39
+ // You can also use LinearGradient, RadialGradient, SweepGradient and ComposeShader
40
+ // The other tiling modes are REPEAT and MIRROR
41
+ shader = BitmapShader (bitmap, Shader .TileMode .CLAMP , Shader .TileMode .CLAMP )
42
+ // shader = BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT)
43
+ paint.shader = shader
44
+ }
45
+
46
+ override fun onDraw (canvas : Canvas ) {
47
+ super .onDraw(canvas)
48
+ // Draw the BitmapShader texture
49
+ /* canvas.drawColor(Color.YELLOW)
50
+ // canvas.drawRect(0.0f, 0.0f, spotlight.width.toFloat(), spotlight.height.toFloat(), paint)
51
+ shaderMatrix.setTranslate(
52
+ 100f,
53
+ 550f
54
+ )
55
+ shader.setLocalMatrix(shaderMatrix)
56
+ canvas.drawRect(0.0f, 0.0f, width.toFloat(), height.toFloat() / 2, paint)*/
57
+ }
58
+ }
0 commit comments