Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
vogella committed Jul 18, 2011
1 parent 59e31ad commit 050eeed
Show file tree
Hide file tree
Showing 230 changed files with 6,942 additions and 6,030 deletions.
2 changes: 1 addition & 1 deletion de.vogella.android.alarm/default.properties
Expand Up @@ -8,4 +8,4 @@
# project structure.

# Project target.
target=android-9
target=android-10
2 changes: 1 addition & 1 deletion de.vogella.android.animation/default.properties
Expand Up @@ -8,4 +8,4 @@
# project structure.

# Project target.
target=android-9
target=android-10
2 changes: 1 addition & 1 deletion de.vogella.android.asyntask/default.properties
Expand Up @@ -8,4 +8,4 @@
# project structure.

# Project target.
target=android-9
target=android-10
3 changes: 2 additions & 1 deletion de.vogella.android.contentprovider/default.properties
Expand Up @@ -10,4 +10,5 @@
# Indicates whether an apk should be generated for each density.
split.density=false
# Project target.
target=android-9
target=android-10

2 changes: 1 addition & 1 deletion de.vogella.android.intent.browser/default.properties
Expand Up @@ -8,4 +8,4 @@
# project structure.

# Project target.
target=android-9
target=android-10
Expand Up @@ -15,7 +15,8 @@ public void onCreate(Bundle savedInstanceState) {
}

public void openBrowser(View view) {
Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://www.vogella.de"));
Intent i = new Intent("android.intent.action.VIEW",
Uri.parse("http://www.vogella.de"));
startActivity(i);
}
}
2 changes: 1 addition & 1 deletion de.vogella.android.intent.browserfilter/default.properties
Expand Up @@ -8,4 +8,4 @@
# project structure.

# Project target.
target=android-9
target=android-10
2 changes: 1 addition & 1 deletion de.vogella.android.intent.explicit/default.properties
Expand Up @@ -8,4 +8,4 @@
# project structure.

# Project target.
target=android-9
target=android-10
2 changes: 1 addition & 1 deletion de.vogella.android.intent.implicit/default.properties
Expand Up @@ -8,4 +8,4 @@
# project structure.

# Project target.
target=android-9
target=android-10
14 changes: 5 additions & 9 deletions de.vogella.android.kidsdrawing/res/layout/main.xml
@@ -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>
16 changes: 13 additions & 3 deletions de.vogella.android.kidsdrawing/res/menu/menu.xml
@@ -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>

2 changes: 2 additions & 0 deletions de.vogella.android.kidsdrawing/res/values/strings.xml
Expand Up @@ -2,4 +2,6 @@
<resources>
<string name="hello">Hello World, FingerPaintActivity!</string>
<string name="app_name">Kids Fingerpaint</string>
<color name="blue">#0000FF</color>
<color name="red">#FF0000</color>
</resources>
@@ -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;
}

}
Expand Up @@ -19,8 +19,8 @@ public void onCreate(Bundle savedInstanceState) {
// could also be done later
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
view = new DrawingView(this, null);
setContentView(view);
setContentView(R.layout.main);
view = (DrawingView) findViewById(R.id.drawingView1);
}

@Override
Expand All @@ -31,7 +31,13 @@ public boolean onCreateOptionsMenu(Menu menu) {

@Override
public boolean onOptionsItemSelected(MenuItem item) {
view.clear();
switch (item.getItemId()) {
case R.id.menuItemClear:
view.clear();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}

0 comments on commit 050eeed

Please sign in to comment.