6

I'm trying to make an application which enables user to touch the screen and draw image based on users' finger coordinates. Here's my code :

public class DrawingBoard extends View { Drawable editIcon = getResources().getDrawable(R.drawable.icon); Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background); float xPos = 0; float yPos = 0; public DrawingBoard (Context context) { // TODO Auto-generated constructor stub super (context); } @Override protected void onDraw (Canvas canvas) { super.onDraw(canvas); canvas.save(); canvas.drawBitmap(mBitmap, 0, 0, null); canvas.translate(xPos, yPos); editIcon.draw(canvas); canvas.restore(); invalidate(); } @Override public boolean onTouchEvent (MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN : xPos = event.getX(); yPos = event.getY(); break; } return true; } } } 

But, whenever I try to click on a screen in emulator, there's no image shown....

pls point out my mistake... THX

2 Answers 2

3

You don't have invalidate() in onTouchEvent()

 @Override protected void onDraw (Canvas canvas) { super.onDraw(canvas); canvas.save(); canvas.drawBitmap(mBitmap, 0, 0, null); canvas.translate(xPos, yPos); editIcon.draw(canvas); canvas.restore(); // invalidate(); } @Override public boolean onTouchEvent (MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN : xPos = event.getX(); yPos = event.getY(); invalidate(); // add it here break; } return true; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Why do we need invalidate in onTouchEvent if I am making use of xPos and yPos directly in onDraw?
0

I have posted an answer for this question many times,This code is working 100 %. if u have still any query then u can contact me

BUT THIS CODE WILL WORK FOR DRAWING AN IMAGE ON GOOGLE MAPS:

public boolean onTouchEvent(MotionEvent event, MapView mapView) { if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Toast.makeText(getBaseContext(), "lat and longtd is \n "+ p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_LONG).show(); // mapView.getOverlays().add(new MarkerOverlay(p)); mapView.invalidate(); } return true; } 

and also define another(2nd) overlay class... where this event will get.

 class MarkerOverlay extends Overlay { private GeoPoint p; private Projection projection; public MarkerOverlay(GeoPoint p) { this.p = p; } @Override public boolean draw(Canvas canvas, MapView mapView,boolean shadow, long when) { super.draw(canvas, mapView, shadow); //---translate the GeoPoint to screen pixels--- Point screenPts = new Point(); mapView.getProjection().toPixels(p, screenPts); //---add the marker--- Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.pir_pictr); canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null); return true; } } 

2 Comments

What does this question have to do with Google maps??
@SimonAndréForsberg Dude, why you give down vote for this answer.are you made??? you don't see. this code will draw an image over Google Maps on touch event. I don't know whats problem with you. If you don't understand it, then make a equerry to me, i will elaborate it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.