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. Squeak Port for Android

Hi,

I posted this earlier on the Squeak vm-dev mailing list. Posting also
here to have both sides of the audience covered ;) Hopefully someone
gets interested.

I am glad to announce that the Squeak Port for Android Tablets [1] has
reached its first milestone.

Work has been done towards fixing certain usability issues such as
better integration with Android Input Methods, access to local file
system, etc.

The pre-packaged Android application (apk, signed with debug key), and
zipped Image file (Image, changes, sources, demo project) are
available from the project page at Google Code.

Everyone is welcome to test the application on their Android devices.
See the TestDrive [2] Wiki page for detailed instructions. Please
report all issues to the project's issue tracker and Wiki pages.

In my case it works on Pandigital Novel 9" eReader tablet with Android
2.0, not rooted.

PS Two things are still known to be an issue:

1. Network sockets do not function properly; they were not addressed
at this time
2. Android NDK rev. 3 is still required (because of the 4.2.1
toolchain); see [3] for some information

PPS I also have been working on the presentation of this project (see
notes and slides in the wiki). If there is any interested group of
Smalltalk and/or Android users/developers in my area (South-eastern
PA, US) I'd be glad to give an actual presentation.

Thanks for any feedback.

[1] http://code.google.com/p/squeakvm-tablet/

[2] http://code.google.com/p/squeakvm-tablet/wiki/TestDrive

[3] http://squeakvm-tablet.googlecode.com/hg/project/ndk-build

-- 
.

2. List of Android Markets

I found this web site that lists the different markets for Android
apps.

http://www.android-markets-info.com/

Could be useful for developers. What do you think?

serge

-- 
.

3. ListPopupWindow does not anchor correctly after an orientation change

4. Making something happen in an activity from another thread.

5. Eclipse IDE missing Android documentation

6. Problems in creating an android chat application

7. accessing widget in Dialog during unit test