|
1 | 1 | package com.hfad.clippingmagic
|
2 | 2 |
|
3 | 3 | import android.content.Context
|
4 |
| -import android.graphics.Canvas |
5 |
| -import android.graphics.Color |
6 |
| -import android.graphics.Paint |
7 |
| -import android.graphics.Path |
| 4 | +import android.graphics.* |
| 5 | +import android.os.Build |
8 | 6 | import android.util.AttributeSet
|
9 | 7 | import android.view.View
|
10 | 8 |
|
@@ -33,6 +31,12 @@ class ClippedView @JvmOverloads constructor(
|
33 | 31 | private val rowThree = rowTwo + rectInset + clipRectBottom
|
34 | 32 | private val rowFour = rowThree + rectInset + clipRectBottom
|
35 | 33 | private val textRow = rowFour + (1.5f * clipRectBottom)
|
| 34 | + private var rectF = RectF( |
| 35 | + rectInset, |
| 36 | + rectInset, |
| 37 | + clipRectRight - rectInset, |
| 38 | + clipRectBottom - rectInset |
| 39 | + ) |
36 | 40 |
|
37 | 41 | override fun onDraw(canvas: Canvas) {
|
38 | 42 | super.onDraw(canvas)
|
@@ -85,27 +89,144 @@ class ClippedView @JvmOverloads constructor(
|
85 | 89 | }
|
86 | 90 |
|
87 | 91 | private fun drawDifferenceClippingExample(canvas: Canvas){
|
| 92 | + canvas.save() |
| 93 | + canvas.translate(columnTwo, rowOne) |
| 94 | + |
| 95 | + // create a frame |
| 96 | + canvas.clipRect( |
| 97 | + 2 * rectInset,2 * rectInset, |
| 98 | + clipRectRight - 2 * rectInset, |
| 99 | + clipRectBottom - 2 * rectInset |
| 100 | + ) |
| 101 | + |
| 102 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O){ |
| 103 | + @Suppress("DEPRECATION") |
| 104 | + canvas.clipRect( |
| 105 | + 4 * rectInset,4 * rectInset, |
| 106 | + clipRectRight - 4 * rectInset, |
| 107 | + clipRectBottom - 4 * rectInset, |
| 108 | + Region.Op.DIFFERENCE |
| 109 | + ) |
| 110 | + } else { |
| 111 | + canvas.clipOutRect( |
| 112 | + 4 * rectInset,4 * rectInset, |
| 113 | + clipRectRight - 4 * rectInset, |
| 114 | + clipRectBottom - 4 * rectInset |
| 115 | + ) |
| 116 | + } |
| 117 | + drawClippedRectangle(canvas) |
| 118 | + canvas.restore() |
88 | 119 | }
|
89 | 120 |
|
90 | 121 | private fun drawCircularClippingExample(canvas: Canvas){
|
| 122 | + canvas.save() |
| 123 | + canvas.translate(columnOne, rowTwo) |
| 124 | + path.rewind() |
| 125 | + path.addCircle( |
| 126 | + circleRadius,clipRectBottom - circleRadius, |
| 127 | + circleRadius,Path.Direction.CCW |
| 128 | + ) |
| 129 | + |
| 130 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { |
| 131 | + @Suppress("DEPRECATION") |
| 132 | + canvas.clipPath(path, Region.Op.DIFFERENCE) |
| 133 | + } else { |
| 134 | + canvas.clipOutPath(path) |
| 135 | + } |
| 136 | + drawClippedRectangle(canvas) |
| 137 | + canvas.restore() |
91 | 138 | }
|
92 | 139 |
|
93 | 140 | private fun drawIntersectionClippingExample(canvas: Canvas){
|
| 141 | + canvas.save() |
| 142 | + canvas.translate(columnTwo,rowTwo) |
| 143 | + canvas.clipRect( |
| 144 | + clipRectLeft,clipRectTop, |
| 145 | + clipRectRight - smallRectOffset, |
| 146 | + clipRectBottom - smallRectOffset |
| 147 | + ) |
| 148 | + |
| 149 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { |
| 150 | + @Suppress("DEPRECATION") |
| 151 | + canvas.clipRect( |
| 152 | + clipRectLeft + smallRectOffset, |
| 153 | + clipRectTop + smallRectOffset, |
| 154 | + clipRectRight,clipRectBottom, |
| 155 | + Region.Op.INTERSECT |
| 156 | + ) |
| 157 | + } else { |
| 158 | + canvas.clipRect( |
| 159 | + clipRectLeft + smallRectOffset, |
| 160 | + clipRectTop + smallRectOffset, |
| 161 | + clipRectRight,clipRectBottom |
| 162 | + ) |
| 163 | + } |
| 164 | + drawClippedRectangle(canvas) |
| 165 | + canvas.restore() |
94 | 166 | }
|
95 | 167 |
|
96 | 168 | private fun drawCombinedClippingExample(canvas: Canvas){
|
| 169 | + canvas.save() |
| 170 | + canvas.translate(columnOne, rowThree) |
| 171 | + path.rewind() |
| 172 | + path.addCircle( |
| 173 | + clipRectLeft + rectInset + circleRadius, |
| 174 | + clipRectTop + circleRadius + rectInset, |
| 175 | + circleRadius,Path.Direction.CCW |
| 176 | + ) |
| 177 | + path.addRect( |
| 178 | + clipRectRight / 2 - circleRadius, |
| 179 | + clipRectTop + circleRadius + rectInset, |
| 180 | + clipRectRight / 2 + circleRadius, |
| 181 | + clipRectBottom - rectInset,Path.Direction.CCW |
| 182 | + ) |
| 183 | + canvas.clipPath(path) |
| 184 | + drawClippedRectangle(canvas) |
| 185 | + canvas.restore() |
97 | 186 | }
|
98 | 187 |
|
99 | 188 | private fun drawRoundedRectangleClippingExample(canvas: Canvas){
|
| 189 | + canvas.save() |
| 190 | + canvas.translate(columnTwo, rowThree) |
| 191 | + path.rewind() |
| 192 | + path.addRoundRect( |
| 193 | + rectF,clipRectRight / 4, |
| 194 | + clipRectRight / 4, Path.Direction.CCW |
| 195 | + ) |
| 196 | + canvas.clipPath(path) |
| 197 | + drawClippedRectangle(canvas) |
| 198 | + canvas.restore() |
100 | 199 | }
|
101 | 200 |
|
102 | 201 | private fun drawOutsideClippingExample(canvas: Canvas){
|
| 202 | + canvas.save() |
| 203 | + canvas.translate(columnOne, rowFour) |
| 204 | + canvas.clipRect(2 * rectInset, 2 * rectInset, |
| 205 | + clipRectRight - 2 * rectInset, |
| 206 | + clipRectBottom - 2 * rectInset) |
| 207 | + drawClippedRectangle(canvas) |
| 208 | + canvas.restore() |
103 | 209 | }
|
104 | 210 |
|
105 | 211 | private fun drawTranslatedTextExample(canvas: Canvas){
|
| 212 | + canvas.save() |
| 213 | + paint.color = Color.GREEN |
| 214 | + paint.textAlign = Paint.Align.LEFT |
| 215 | + canvas.translate(columnTwo, textRow) |
| 216 | + canvas.drawText(context.getString(R.string.translated), |
| 217 | + clipRectLeft,clipRectTop,paint) |
| 218 | + canvas.restore() |
106 | 219 | }
|
107 | 220 |
|
108 | 221 | private fun drawSkewedTextExample(canvas: Canvas){
|
| 222 | + canvas.save() |
| 223 | + paint.color = Color.YELLOW |
| 224 | + paint.textAlign = Paint.Align.RIGHT |
| 225 | + canvas.translate(columnTwo,textRow) |
| 226 | + canvas.skew(0.2f, 0.3f) |
| 227 | + canvas.drawText(context.getString(R.string.skewed), |
| 228 | + clipRectLeft, clipRectTop, paint) |
| 229 | + canvas.restore() |
109 | 230 | }
|
110 | 231 |
|
111 | 232 | private fun drawQuickRejectExample(canvas: Canvas){
|
|
0 commit comments