Skip to content

Commit 9273550

Browse files
committed
add first clipping method
1 parent 0a6dbc5 commit 9273550

File tree

12 files changed

+151
-134
lines changed

12 files changed

+151
-134
lines changed

23-clipping-magic/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ An app to learn how to clip objects on a canvas.
88

99
## Features
1010

11+
- setting up the project and the shapes.
12+
- handling dimensions on bigger screens.
1113
- understanding the drawing algorithm.
1214
- creating a method to draw the shapes.
1315
- implementing the clipping methods.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.hfad.clippingmagic
2+
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
8+
import android.util.AttributeSet
9+
import android.view.View
10+
11+
class ClippedView @JvmOverloads constructor(
12+
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
13+
) : View(context, attrs, defStyleAttr) {
14+
private val paint = Paint().apply {
15+
isAntiAlias = true
16+
strokeWidth = resources.getDimension(R.dimen.strokeWidth)
17+
textSize = resources.getDimension(R.dimen.textSize)
18+
}
19+
private val path = Path()
20+
private val clipRectRight = resources.getDimension(R.dimen.clipRectRight)
21+
private val clipRectBottom = resources.getDimension(R.dimen.clipRectBottom)
22+
private val clipRectTop = resources.getDimension(R.dimen.clipRectTop)
23+
private val clipRectLeft = resources.getDimension(R.dimen.clipRectLeft)
24+
private val rectInset = resources.getDimension(R.dimen.rectInset)
25+
private val smallRectOffset = resources.getDimension(R.dimen.smallRectOffset)
26+
private val circleRadius = resources.getDimension(R.dimen.circleRadius)
27+
private val textOffset = resources.getDimension(R.dimen.textOffset)
28+
private val textSize = resources.getDimension(R.dimen.textSize)
29+
private val columnOne = rectInset
30+
private val columnTwo = columnOne + rectInset + clipRectRight
31+
private val rowOne = rectInset
32+
private val rowTwo = rowOne + rectInset + clipRectBottom
33+
private val rowThree = rowTwo + rectInset + clipRectBottom
34+
private val rowFour = rowThree + rectInset + clipRectBottom
35+
private val textRow = rowFour + (1.5f * clipRectBottom)
36+
37+
override fun onDraw(canvas: Canvas) {
38+
super.onDraw(canvas)
39+
drawBackAndUnclippedRectangle(canvas)
40+
drawDifferenceClippingExample(canvas)
41+
drawCircularClippingExample(canvas)
42+
drawIntersectionClippingExample(canvas)
43+
drawCombinedClippingExample(canvas)
44+
drawRoundedRectangleClippingExample(canvas)
45+
drawOutsideClippingExample(canvas)
46+
drawSkewedTextExample(canvas)
47+
drawTranslatedTextExample(canvas)
48+
// drawQuickRejectExample(canvas)
49+
}
50+
51+
private fun drawClippedRectangle(canvas: Canvas){
52+
canvas.clipRect(
53+
clipRectLeft,clipRectTop,
54+
clipRectRight,clipRectBottom
55+
)
56+
canvas.drawColor(Color.WHITE)
57+
58+
paint.color = Color.RED
59+
canvas.drawLine(
60+
clipRectLeft,clipRectTop,
61+
clipRectRight,clipRectBottom,paint
62+
)
63+
64+
paint.color = Color.GREEN
65+
canvas.drawCircle(
66+
circleRadius,clipRectBottom - circleRadius,
67+
circleRadius,paint
68+
)
69+
70+
paint.color = Color.BLUE
71+
paint.textSize = textSize
72+
paint.textAlign = Paint.Align.RIGHT
73+
canvas.drawText(
74+
context.getString(R.string.clipping),
75+
clipRectRight,textOffset,paint
76+
)
77+
}
78+
79+
private fun drawBackAndUnclippedRectangle(canvas: Canvas){
80+
canvas.drawColor(Color.GRAY)
81+
canvas.save()
82+
canvas.translate(columnOne, rowOne)
83+
drawClippedRectangle(canvas)
84+
canvas.restore()
85+
}
86+
87+
private fun drawDifferenceClippingExample(canvas: Canvas){
88+
}
89+
90+
private fun drawCircularClippingExample(canvas: Canvas){
91+
}
92+
93+
private fun drawIntersectionClippingExample(canvas: Canvas){
94+
}
95+
96+
private fun drawCombinedClippingExample(canvas: Canvas){
97+
}
98+
99+
private fun drawRoundedRectangleClippingExample(canvas: Canvas){
100+
}
101+
102+
private fun drawOutsideClippingExample(canvas: Canvas){
103+
}
104+
105+
private fun drawTranslatedTextExample(canvas: Canvas){
106+
}
107+
108+
private fun drawSkewedTextExample(canvas: Canvas){
109+
}
110+
111+
private fun drawQuickRejectExample(canvas: Canvas){
112+
}
113+
}

23-clipping-magic/app/src/main/java/com/hfad/clippingmagic/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import android.os.Bundle
66
class MainActivity : AppCompatActivity() {
77
override fun onCreate(savedInstanceState: Bundle?) {
88
super.onCreate(savedInstanceState)
9-
setContentView(R.layout.activity_main)
9+
setContentView(ClippedView(this))
1010
}
1111
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<dimen name="clipRectRight">120dp</dimen>
4+
<dimen name="clipRectBottom">120dp</dimen>
5+
<dimen name="rectInset">10dp</dimen>
6+
<dimen name="smallRectOffset">50dp</dimen>
7+
<dimen name="circleRadius">40dp</dimen>
8+
<dimen name="textOffset">25dp</dimen>
9+
<dimen name="strokeWidth">6dp</dimen>
10+
</resources>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<dimen name="clipRectRight">90dp</dimen>
4+
<dimen name="clipRectBottom">90dp</dimen>
5+
<dimen name="clipRectTop">0dp</dimen>
6+
<dimen name="clipRectLeft">0dp</dimen>
7+
<dimen name="rectInset">8dp</dimen>
8+
<dimen name="smallRectOffset">40dp</dimen>
9+
<dimen name="circleRadius">30dp</dimen>
10+
<dimen name="textOffset">20dp</dimen>
11+
<dimen name="strokeWidth">4dp</dimen>
12+
<dimen name="textSize">18sp</dimen>
13+
</resources>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<resources>
22
<string name="app_name">Clipping Magic</string>
3+
<string name="clipping">Clipping</string>
4+
<string name="translated">translated text</string>
5+
<string name="skewed">"Skewed and "</string>
36
</resources>
11.8 KB
Loading
24.9 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
<resources>
22
<string name="app_name">FindMe</string>
3+
<string name="instructions_title">
4+
<b>How to play:</b>
5+
</string>
6+
<string name="instructions">
7+
\t \u2022 Find the Android hidden behind the dark surface. \n
8+
\t \u2022 Touch and hold the screen for the spotlight. \n
9+
\t \u2022 Once you find the Android, lift your finger to end the game. \n \n
10+
\t \u2022 To restart the game touch the screen again.
11+
</string>
312
</resources>

25-to-do-notes/.github/ISSUE_TEMPLATE/advanced-android-issue-template-for-testing-codelab.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)