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. New 15 min refund policy - are users aware?

Hey everyone,


I just received an email from a customer asking a refund of one of my apps,
saying that only a couple of hours have passed after buying it. It looks
like he didn't know about the new 15 minutes policy. Did Google tell users
about this change? Or only developers know about it?

What's funny (or sad?) is that this user thinks that *I* was the one to put
15-minutes on my app:
"While most apps offer a full 24 hours,  15 minutes seems to be a far cry
from the "try -before -you -buy" spirit of the Android market. I urge you to
adopt the 24-hour grace period allowed by your peers for future customers."

Have you also had a similar experiences with regards to the refund policy?

-- 
YuviDroid
Check out Launch-X <http://android.yuvalsharon.net/launchx.php> (a widget to
quickly access your favorite apps and contacts!)
http://android.yuvalsharon.net

-- 
.

2. met kenal... ijin mendarat...

Hallo teman2..

met kenal .. saya safitry ozawa,

baru nyobain main robot froyo 2.2.1 di archos 101 internet tablet

berharap bisa dapet banyak elmu2 di marih


salam

safitry ozawa

-- 
===============
"

3. Setting Wallpaper

4. How to actually see log messages in ADT, or anywhere for that matter?

5. Terimakasih ya.. OOT : Happy Birthday Suhi Enya

6. How to block a BufferedReader when no input is available?

7. Android 2.3 Gingerbread for Nexus One & Galaxy S