SImple Draw Program --Advice Needed

by joshbeck » Sun, 02 Nov 2008 17:14:32 GMT


Sponsored Links
 I watered this code down as much as possible for easy reading.
Here is what it does:

It draws a bitmap image to the screen. --darkcleanlinux
(This part works.)

It's the OnTouchEven(MotionEvent event) function that doesn't work.

-I'm not sure I'm using the mCanvas object correctly.

I would think that it would place a big dot on the screen when
touched.
Instead, it crashes.


Any advice for a newb?

Thanks

--~--~---------~--~----~------------~-------~--~----~



SImple Draw Program --Advice Needed

by joshbeck » Sun, 02 Nov 2008 17:15:29 GMT


 Here's the code:

Thank You!

package com.android.picturetest;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;

public class BitmapMesh extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));

    }

        private static class SampleView extends View {
        private  Canvas mCanvas;
        private final Paint mPaint;
        private Bitmap mBitmap;



        public SampleView(Context context) {

                super(context);
                 mPaint = new Paint();
                 mPaint.setAntiAlias(true);
             mPaint.setARGB(255, 255, 255, 255);
             mBitmap = BitmapFactory.decodeResource(getResources(),
 
R.drawable.darkcleanlinux);


                }

        @Override protected void onDraw(Canvas canvas) {

            canvas.drawBitmap(mBitmap, 0, 0, null);

        }

       @Override public boolean onTouchEvent(MotionEvent event) {

            draw2(event.getX(),event.getY());
            return true;
        }

       private void draw2(float X, float Y) {


            mCanvas.drawCircle(X, Y, 200, mPaint);

        }

    }

}
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


SImple Draw Program --Advice Needed

by Steve Oldmeadow » Sun, 02 Nov 2008 17:40:25 GMT


 I'd say it crashes because mCanvas is null when you call draw2.  How
did you expect mCanvas to be instantiated?

Even so - the correct approach is to call invalidate on your view and
use the canvas object that gets passed in to the onDraw.  So in your
onTouchEvent you probably want to store the x and y coordinates and
set a flag to indicate the user has touched the screen.  Then call
invalidate on your view - this will cause onDraw to be called at some
later time.  Then in your onDraw you can check your flag and if it is
set then draw the circle at the coordinates you stored in the
onTouchEvent.
--~--~---------~--~----~------------~-------~--~----~



SImple Draw Program --Advice Needed

by joshbeck » Sun, 02 Nov 2008 23:04:42 GMT


 Thanks a million. I'll try this today and report back.

I appreciate it.

When you say 'set a flag.' Do you mean like an int? 0 or 1 kind of
thing?

Thanks again,
Pros



--~--~---------~--~----~------------~-------~--~----~



SImple Draw Program --Advice Needed

by joshbeck » Sun, 02 Nov 2008 23:06:16 GMT


 


One more thing:
If I set a flag in OnTouchEvent that is something like this
int myflag = 1;

What is the best way to make it global so that onDraw will recognize
it?


--~--~---------~--~----~------------~-------~--~----~



SImple Draw Program --Advice Needed

by joshbeck » Sun, 02 Nov 2008 23:23:54 GMT


 




Ok, this keeps the background and draws a circle on top of it when I
touch the screen.
Question: How do you think I might get it to keep the old circles when
I draw new ones.
Right now when I click the canvas is completely redrawn and the first
circle I created is lost.

Thanks, that helped a lot.




import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;

public class BitmapMesh extends Activity {
        SampleView mView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = new SampleView(this);
        setContentView(mView);

    }

        private static class SampleView extends View {
        private  Canvas mCanvas;
        private final Paint mPaint;
        private Bitmap mBitmap;
        public float XX;
        public float YY;
        public float ZZ;

        public SampleView(Context context) {

                super(context);
                 ZZ = 0;

                 mPaint = new Paint();
                 mPaint.setAntiAlias(true);
             mPaint.setARGB(255, 255, 255, 255);
             mBitmap = BitmapFactory.decodeResource(getResources(),
 
R.drawable.darkcleanlinux);


                }

        @Override protected void onDraw(Canvas canvas) {
            mCanvas = canvas;
            if(ZZ != 0){
                canvas.drawBitmap(mBitmap, 0, 0, null);
                canvas.drawCircle(XX, YY, 20, mPaint);
            }
            else
            canvas.drawBitmap(mBitmap, 0, 0, null);

        }

       @Override public boolean onTouchEvent(MotionEvent event) {
            invalidate();

            XX= event.getX();
            YY= event.getY();
            ZZ = 1;
            return true;
        }

       private void draw2(float X, float Y) {


            mCanvas.drawCircle(X, Y, 200, mPaint);

        }

    }

}
--~--~---------~--~----~------------~-------~--~----~



SImple Draw Program --Advice Needed

by Steve Oldmeadow » Mon, 03 Nov 2008 08:18:54 GMT


 


You are losing the old circles because (I assume) you are clearing the
screen when you draw the mBitmap.  However, that is a pretty normal
thing to do in your onDraw.  If you want to draw the previous circles
you need to remember their coordinates in some kind of data structure
and then iterate over that structure and draw all the circles.

Have a look here:  
 http://java.sun.com/docs/books/tutorial/collections/intro/index.html 
for some details about collections in Java.  Something like ArrayList
will work for you and be easy to implement.  If you move to this
approach then in the onDraw you add a new object (maybe a Point) to
the ArrayList and then in the onDraw iterate over your ArrayList and
draw all the circles.  In this case you won't need your ZZ flag any
more but for future reference a boolean would have been a better data
type for your flag.


--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Eclipse : Unknown error: SDK is not loaded yet. * upgrade failed *

Are you still having this problem?  Do you get SDK ping errors?

If this is the problem, Google engineers have released a new version
of their ADT plugin.  Install that one.

If it is something else, let me know. I can do my best to help you.
You might have to go into the options and direct it to the location of
the <android-sdk-location>\tools location manually if you didn't put
it in the standerd directory (which on Vista I cannot remember for the
life of me what it is).

Andrew



--~--~---------~--~----~------------~-------~--~----~

2. Want to earn online $400 to $500 per month !!!!

hey !!!!!!!!!!!!!!!!!!!!!!1

there is an oppurtunity for u all to earn $400 to $500 per month
really and that works

just go on with the link to know the details

http://www.123maza.com/1011/telecommunication

go ahead and fill your pocket :) :)

--~--~---------~--~----~------------~-------~--~----~

3. About TouchEvent..! in class Mapview or javascript

4. Launching a Dialog from Overlay/MapActivity

5. Webservices an Android.

6. Change Tab Widget icon for currently active tab

7. how to add other status to power-off menu