-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalImageView
More file actions
88 lines (67 loc) · 2.5 KB
/
Copy pathDiagonalImageView
File metadata and controls
88 lines (67 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class DiagonalImageView @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatImageView(context, attributeSet, defStyleAttr) {
companion object {
private const val DEFAULT_ANGLE_SIZE = 0
}
@Px
var angle: Float = context.dpToPx(DEFAULT_ANGLE_SIZE)
var angleDirection: String = "right"
private val maskPaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val viewRect = Rect()
private lateinit var resultBtm: Bitmap
private lateinit var mascBtm: Bitmap
private lateinit var srcBtm: Bitmap
init {
attributeSet?.let {
val ta = context.obtainStyledAttributes(it, R.styleable.DiagonalImageView)
angle = ta.getDimension(
R.styleable.DiagonalImageView_heightDiagonal,
context.dpToPx(DEFAULT_ANGLE_SIZE)
)
angleDirection = ta.getString(R.styleable.DiagonalImageView_angleDirection) ?: "right"
ta.recycle()
}
scaleType = ScaleType.CENTER_CROP
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
if (w == 0) return
with(viewRect) {
left = 0
top = 0
right = w
bottom = h
}
prepareBitmap(w, h)
}
override fun onDraw(canvas: Canvas?) {
canvas?.drawBitmap(resultBtm, viewRect, viewRect, null)
}
private fun prepareBitmap(w: Int, h: Int) {
mascBtm = Bitmap.createBitmap(w, h, Bitmap.Config.ALPHA_8)
resultBtm = mascBtm.copy(Bitmap.Config.ARGB_8888, true)
val maskCanvas = Canvas(mascBtm)
val path = Path()
if (angleDirection == "left") {
path.moveTo(0f, 0f)
path.lineTo(w.toFloat(), 0f)
path.lineTo(w.toFloat(), h.toFloat())
path.lineTo(0f, h.toFloat() - angle)
} else {
path.moveTo(0f, 0f)
path.lineTo(w.toFloat(), 0f)
path.lineTo(w.toFloat(), h.toFloat() - angle)
path.lineTo(0f, h.toFloat())
}
path.close()
maskCanvas.drawPath(path, maskPaint)
maskPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
srcBtm = drawable.toBitmap(w, h, Bitmap.Config.ARGB_8888)
val resultCanvas = Canvas(resultBtm)
resultCanvas.drawBitmap(mascBtm, viewRect, viewRect, null)
resultCanvas.drawBitmap(srcBtm, viewRect, viewRect, maskPaint)
}
}