Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
230 changed files
with
6,942 additions
and
6,030 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
# project structure. | ||
|
||
# Project target. | ||
target=android-9 | ||
target=android-10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
# project structure. | ||
|
||
# Project target. | ||
target=android-9 | ||
target=android-10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
# project structure. | ||
|
||
# Project target. | ||
target=android-9 | ||
target=android-10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
# project structure. | ||
|
||
# Project target. | ||
target=android-9 | ||
target=android-10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
# project structure. | ||
|
||
# Project target. | ||
target=android-9 | ||
target=android-10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
# project structure. | ||
|
||
# Project target. | ||
target=android-9 | ||
target=android-10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
# project structure. | ||
|
||
# Project target. | ||
target=android-9 | ||
target=android-10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
> | ||
<TextView | ||
android:layout_width="fill_parent" | ||
android:layout_height="wrap_content" | ||
android:text="@string/hello" | ||
/> | ||
android:orientation="vertical" android:layout_width="fill_parent" | ||
android:layout_height="fill_parent"> | ||
<de.vogella.android.kidsdrawing.DrawingView | ||
android:id="@+id/drawingView1" android:layout_width="wrap_content" | ||
android:layout_height="wrap_content"></de.vogella.android.kidsdrawing.DrawingView> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<menu | ||
xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<item android:id="@+id/item1" android:title="Clear Screen"></item> | ||
<menu xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<item android:id="@+id/menuItemClear" android:title="Clear Screen"> | ||
</item> | ||
<item android:id="@+id/menuItemLineSize" android:title="Line Size"> | ||
<menu> | ||
<group android:checkableBehavior="single"> | ||
<item android:id="@+id/small" android:title="Small" /> | ||
<item android:id="@+id/medium" android:title="Medium" /> | ||
<item android:id="@+id/large" android:title="Large" /> | ||
</group> | ||
</menu> | ||
</item> | ||
</menu> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
267 changes: 134 additions & 133 deletions
267
de.vogella.android.kidsdrawing/src/de/vogella/android/kidsdrawing/DrawingView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,134 @@ | ||
package de.vogella.android.kidsdrawing; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import android.content.Context; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Paint; | ||
import android.graphics.Path; | ||
import android.graphics.RectF; | ||
import android.util.AttributeSet; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
|
||
public class DrawingView extends View { | ||
|
||
private static final float STROKE_WIDTH = 5f; | ||
|
||
/** Need to track this so the dirty region can accommodate the stroke. **/ | ||
private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2; | ||
|
||
private Paint paint = new Paint(); | ||
private List<Path> paths = new ArrayList<Path>(); | ||
private List<Paint> paints = new ArrayList<Paint>(); | ||
|
||
/** | ||
* Optimizes painting by invalidating the smallest possible area. | ||
*/ | ||
private float lastTouchX; | ||
private float lastTouchY; | ||
private final RectF dirtyRect = new RectF(); | ||
|
||
private int[] colors; | ||
|
||
private Path path; | ||
|
||
private int nextColor; | ||
|
||
private Random random; | ||
|
||
public DrawingView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
|
||
colors = new int[] { Color.BLUE, Color.CYAN, Color.GREEN, | ||
Color.MAGENTA, Color.YELLOW, Color.RED, Color.WHITE }; | ||
random = new Random(); | ||
nextColor = random.nextInt(colors.length); | ||
paint.setAntiAlias(true); | ||
paint.setColor(Color.WHITE); | ||
paint.setStyle(Paint.Style.STROKE); | ||
paint.setStrokeJoin(Paint.Join.ROUND); | ||
paint.setStrokeWidth(STROKE_WIDTH); | ||
} | ||
|
||
/** | ||
* Erases the signature. | ||
*/ | ||
public void clear() { | ||
for (Path path : paths) { | ||
path.reset(); | ||
} | ||
// Repaints the entire view. | ||
invalidate(); | ||
} | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
int i = 0; | ||
for (Path path : paths) { | ||
canvas.drawPath(path, paints.get(i++)); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean onTouchEvent(MotionEvent event) { | ||
float eventX = event.getX(); | ||
float eventY = event.getY(); | ||
|
||
switch (event.getAction()) { | ||
case MotionEvent.ACTION_DOWN: | ||
path = new Path(); | ||
paths.add(path); | ||
|
||
paint = new Paint(); | ||
paints.add(paint); | ||
paint.setAntiAlias(true); | ||
paint.setStyle(Paint.Style.STROKE); | ||
paint.setStrokeJoin(Paint.Join.ROUND); | ||
|
||
paint.setStrokeWidth(STROKE_WIDTH + random.nextInt(5)); | ||
paint.setColor(colors[nextColor++]); | ||
if (nextColor >= colors.length) { | ||
nextColor = 0; | ||
} | ||
path.moveTo(eventX, eventY); | ||
lastTouchX = eventX; | ||
lastTouchY = eventY; | ||
// There is no end point yet, so don't waste cycles invalidating. | ||
|
||
return true; | ||
|
||
case MotionEvent.ACTION_MOVE: | ||
case MotionEvent.ACTION_UP: | ||
|
||
// When the hardware tracks events faster than they are delivered, | ||
// the | ||
// event will contain a history of those skipped points. | ||
int historySize = event.getHistorySize(); | ||
for (int i = 0; i < historySize; i++) { | ||
float historicalX = event.getHistoricalX(i); | ||
float historicalY = event.getHistoricalY(i); | ||
path.lineTo(historicalX, historicalY); | ||
} | ||
|
||
// After replaying history, connect the line to the touch point. | ||
path.lineTo(eventX, eventY); | ||
break; | ||
|
||
default: | ||
return false; | ||
} | ||
|
||
// Include half the stroke width to avoid clipping. | ||
invalidate(); | ||
|
||
lastTouchX = eventX; | ||
lastTouchY = eventY; | ||
|
||
return true; | ||
} | ||
} | ||
package de.vogella.android.kidsdrawing; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import android.content.Context; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Paint; | ||
import android.graphics.Path; | ||
import android.graphics.RectF; | ||
import android.util.AttributeSet; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
|
||
public class DrawingView extends View { | ||
|
||
private static float STROKE_WIDTH = 20f; | ||
|
||
/** Need to track this so the dirty region can accommodate the stroke. **/ | ||
private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2; | ||
|
||
private Paint paint = new Paint(); | ||
private List<Path> paths = new ArrayList<Path>(); | ||
private List<Paint> paints = new ArrayList<Paint>(); | ||
|
||
/** | ||
* Optimizes painting by invalidating the smallest possible area. | ||
*/ | ||
private float lastTouchX; | ||
private float lastTouchY; | ||
private final RectF dirtyRect = new RectF(); | ||
|
||
private int[] colors; | ||
|
||
private Path path; | ||
|
||
private int nextColor; | ||
|
||
private Random random; | ||
|
||
public DrawingView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
|
||
colors = new int[] { Color.BLUE, Color.CYAN, Color.GREEN, | ||
Color.MAGENTA, Color.YELLOW, Color.RED, Color.WHITE }; | ||
random = new Random(); | ||
nextColor = random.nextInt(colors.length); | ||
paint.setAntiAlias(true); | ||
paint.setColor(Color.WHITE); | ||
paint.setStyle(Paint.Style.STROKE); | ||
paint.setStrokeJoin(Paint.Join.ROUND); | ||
paint.setStrokeWidth(STROKE_WIDTH); | ||
} | ||
|
||
/** | ||
* Erases the signature. | ||
*/ | ||
public void clear() { | ||
for (Path path : paths) { | ||
path.reset(); | ||
} | ||
// Repaints the entire view. | ||
invalidate(); | ||
} | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
int i = 0; | ||
for (Path path : paths) { | ||
canvas.drawPath(path, paints.get(i++)); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean onTouchEvent(MotionEvent event) { | ||
float eventX = event.getX(); | ||
float eventY = event.getY(); | ||
|
||
switch (event.getAction()) { | ||
case MotionEvent.ACTION_DOWN: | ||
path = new Path(); | ||
paths.add(path); | ||
|
||
paint = new Paint(); | ||
paints.add(paint); | ||
paint.setAntiAlias(true); | ||
paint.setStyle(Paint.Style.STROKE); | ||
paint.setStrokeJoin(Paint.Join.ROUND); | ||
paint.setStrokeWidth(STROKE_WIDTH); | ||
|
||
paint.setColor(colors[nextColor++]); | ||
if (nextColor >= colors.length) { | ||
nextColor = 0; | ||
} | ||
path.moveTo(eventX, eventY); | ||
lastTouchX = eventX; | ||
lastTouchY = eventY; | ||
// There is no end point yet, so don't waste cycles invalidating. | ||
|
||
return true; | ||
|
||
case MotionEvent.ACTION_MOVE: | ||
case MotionEvent.ACTION_UP: | ||
|
||
// When the hardware tracks events faster than they are delivered, | ||
// the | ||
// event will contain a history of those skipped points. | ||
int historySize = event.getHistorySize(); | ||
for (int i = 0; i < historySize; i++) { | ||
float historicalX = event.getHistoricalX(i); | ||
float historicalY = event.getHistoricalY(i); | ||
path.lineTo(historicalX, historicalY); | ||
} | ||
|
||
// After replaying history, connect the line to the touch point. | ||
path.lineTo(eventX, eventY); | ||
break; | ||
|
||
default: | ||
return false; | ||
} | ||
|
||
// Include half the stroke width to avoid clipping. | ||
invalidate(); | ||
|
||
lastTouchX = eventX; | ||
lastTouchY = eventY; | ||
|
||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.