MainActivity:
Java-Quellcode
- package com.example.drawproject;
- import android.support.v7.app.ActionBarActivity;
- import android.support.v7.app.ActionBar;
- import android.support.v4.app.Fragment;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.os.Build;
- public class MainActivity extends Activity {
- Button b1; //Gelb
- Button b2; //Blau
- Button b3; //Grün
- Integer Pcount2 = DrawArea.getCountValue(); //get the count value
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.drawlayout);
- DrawArea da = new DrawArea(this, null);
- if(Pcount2 % 2 ==0){ //use it -> it's always the same number, instead of the increased
- Intent nextScreen = new Intent(this, ChangeArea.class);
- startActivity(nextScreen);
- }
- else{
- Pcount2++;
- }
- }
- }
View Klasse
Java-Quellcode
- package com.example.drawproject;
- import android.content.Context;
- import android.graphics.*;
- import android.os.Bundle;
- import android.util.AttributeSet;
- import android.util.SparseArray;
- import android.view.MotionEvent;
- import android.view.View;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- public class DrawArea extends View {
- private List<Stroke> _allStrokes; //all strokes that need to be drawn
- private SparseArray<Stroke> _activeStrokes; //use to retrieve the currently drawn strokes
- private Random _rdmColor = new Random();
- static int count = 1; // count ist 1
- public DrawArea(Context context, AttributeSet attrs) {
- super(context, attrs);
- _allStrokes = new ArrayList<Stroke>();
- _activeStrokes = new SparseArray<Stroke>();
- setFocusable(true);
- setFocusableInTouchMode(true);
- }
- public void onDraw(Canvas canvas) {
- if (_allStrokes != null) {
- for (Stroke stroke: _allStrokes) {
- if (stroke != null) {
- Path path = stroke.getPath();
- Paint painter = stroke.getPaint();
- if ((path != null) && (painter != null)) {
- if(count%2 != 0){
- canvas.drawPath(path, painter);
- }
- }
- }
- }
- }
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- final int action = event.getActionMasked();
- final int pointerCount = event.getPointerCount();
- switch (action) {
- case MotionEvent.ACTION_DOWN: {
- count++; // count wird um 1 erhöht
- if(count%2 != 1)
- {pointDown((int)event.getX(), (int)event.getY(), event.getPointerId(0));
- break;
- }
- if (count%2 != 0){
- for (int pc = 0; pc < pointerCount; pc++) {
- pointDown((int)event.getX(pc), (int)event.getY(pc), event.getPointerId(pc));
- }
- }
- }
- case MotionEvent.ACTION_MOVE: {
- break;
- }
- case MotionEvent.ACTION_UP: {
- break;
- }
- }
- invalidate();
- return true;
- }
- public static int getCountValue(){
- int count2 = count; //count wird zur übergabe vorbereitet...
- return count2;
- }
- private void pointDown(int x, int y, int id) {
- Integer value = ChangeArea.getColorValue();
- if(count%2 !=1){
- //create a paint with random color
- Paint paint = new Paint();
- paint.setStyle(Paint.Style.STROKE);
- if (value == 1){
- paint.setStrokeWidth(15);
- }else
- if (value == 2){
- paint.setStrokeWidth(20);
- }else
- if (value == 3){
- paint.setStrokeWidth(5);
- }
- else{
- paint.setStrokeWidth(10);
- }
- if (value == 1){
- paint.setColor(Color.YELLOW);
- }else
- if (value == 2){
- paint.setColor(Color.BLUE);
- }else
- if (value == 3){
- paint.setColor(Color.GREEN);
- }
- else{
- paint.setColor(_rdmColor.nextInt()); //Here should the values be added!
- }
- //create the Stroke
- Point pt = new Point(x, y);
- Stroke stroke = new Stroke(paint);
- stroke.addPoint(pt);
- _activeStrokes.put(id, stroke);
- _allStrokes.add(stroke);
- }
- if (count%2 != 0){
- //retrieve the stroke and add new point to its path
- Stroke stroke = _activeStrokes.get(id);
- if (stroke != null) {
- Point pt = new Point(x, y);
- stroke.addPoint(pt);
- }
- }
- }
- }