Skip to content

Commit b0d91e4

Browse files
committed
add BitmapShader
1 parent 0fdaea9 commit b0d91e4

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

24-findme/app/src/main/res/layout/activity_main.xml

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<TextView
10-
android:layout_width="wrap_content"
11-
android:layout_height="wrap_content"
12-
android:text="Hello World!"
9+
<com.hfad.findme.SpotlightImageView
10+
android:id="@+id/spotlightImageView"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"
1313
app:layout_constraintBottom_toBottomOf="parent"
14-
app:layout_constraintLeft_toLeftOf="parent"
15-
app:layout_constraintRight_toRightOf="parent"
14+
app:layout_constraintEnd_toEndOf="parent"
15+
app:layout_constraintStart_toStartOf="parent"
1616
app:layout_constraintTop_toTopOf="parent" />
1717

1818
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)