tags:

views:

67

answers:

2

Hy!! I want to make a Whiteboard app, but i don't know how to make only some pixels black and let the others white.

+1  A: 

Check this out. http://www.tutorialforandroid.com/2009/06/drawing-with-canvas-in-android.html

In addition you might also want a web application / service in order to transfer the screen contents to your audience.

Ragunath Jawahar
like that? public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Canvas canvas = new Canvas(); Paint paint = new Paint(); paint.setColor(Color.WHITE); canvas.drawPoint(20, 20, paint); }but what about the view...
Hans Wurst
Please read this part on the link I posted to you."To do this:Copy the whole code from droidnova the replace the following."
Ragunath Jawahar
Do you know how the view know that the paint and the convas are on them?
Hans Wurst
+3  A: 

This should help getting black pixels on your whiteboard:

Scribbler.java:

package org.yourpackage.scribble;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class Scribbler extends Activity {
    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
        drawView.requestFocus();

    }
}

DrawView.java:

package org.yourpackage.scribble;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawView extends View implements OnTouchListener {
    List<Point> points = new ArrayList<Point>();
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);
        paint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {
        for (Point point : points) {
            canvas.drawCircle(point.x, point.y, 2, paint);  
        }
    }

    public boolean onTouch(View view, MotionEvent event) {
        Point point = new Point();
        point.x = event.getX();
        point.y = event.getY();
        points.add(point);
        invalidate();
        return true;
    }
}

class Point {
    float x, y;
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.yourpackage.scribble"
      android:versionCode="1"
      android:versionName="1.0">
    <application>
        <activity android:name=".Scribbler">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest> 

...which turns out to look like:

alt text

You may want to draw lines instead of pixels - this one is just to give you a clue

Martin
PERFEKT!!!But what i don't understand is, why does the new View know that the paint is "on" it?
Hans Wurst
because in the Scribbler activity we set the our DrawView as the active content - and if there happens something on this DrawView we get the coordinates within the onTouch method and then cause a the onDraw method to be called which draws our pixels - onDraw is called automatically because we force a re-draw of the View by calling invaldidate() within the onTouch - you can get information about this behaviour in the "Drawing" section of this link http://developer.android.com/reference/android/view/View.html
Martin