You need to override the onTouchEvent method in CustomDrawableView @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) {
// get your x and y values from event.getX() and event.getY() case MotionEvent.ACTION_DOWN: // redraw your green or red circle depending on your x,y values. break; } return true; } Hope this helps, Balwinder Kaur Open Source Development Center ·T· · ·Mobile· stick together The views, opinions and statements in this email are those of the author solely in their individual capacity, and do not necessarily represent those of T-Mobile USA, Inc. On Aug 4, 5:10 am, Graham <gxt...@gmail.com> wrote: > Hi again, > > I am getting to grip with the android fundamentals and am curious as > to how the onTouch and the onTouchlisteners work. > > To keep it simple I have used onDraw to draw two circles on a canvas, > a green circle and a red circle (each of type ShapeDrawable - Oval > Shape). I now wish to make it so that when I push the green circle, it > will move to the right and if I push the red circle it will move down. > > I can move the circles with a timer delay so I am aware of how to do > this but I can't capture the onTouch. Can someone please help? > > I have drawn my two circles with the below code (very similar to one > of the exercises): > > ------------------------------------------------ > > package com.image; > > import android.app.Activity; > import android.os.Bundle; > > public class image extends Activity { > /** Called when the activity is first created. */ > > private CustomDrawableView mCustomDrawableView; > //private CustomDrawableView mCustomDrawableView2; > > protected void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > > //last argument 1 = green, all others = red > mCustomDrawableView = new CustomDrawableView(this, 20,20,20,20); > > setContentView(mCustomDrawableView); > > } > > } > > package com.image; > > import android.content.Context; > import android.graphics.Canvas; > import android.graphics.drawable.Drawable; > import android.graphics.drawable.ShapeDrawable; > import android.graphics.drawable.shapes.OvalShape; > import android.view.View; > > public class CustomDrawableView extends View { > private ShapeDrawable[] mDrawables; > > public CustomDrawableView(Context context, int x, int y, int > width, int height) { > super(context); > > mDrawables = new ShapeDrawable[2]; > > mDrawables[0] = new ShapeDrawable(new OvalShape()); // green > one > mDrawables[1] = new ShapeDrawable(new OvalShape()); //red one > > mDrawables[0].getPaint().setColor(0xff74AC23); > mDrawables[1].getPaint().setColor(0xffff0000); > > mDrawables[0].setBounds(x,y,x+width,y+height); //arguments > left, top, right, bottom > mDrawables[1].setBounds(x+30,y+30,x+width+30,y+height+30); > > } > > @Override protected void onDraw(Canvas canvas) { > > //mDrawables[0].draw(canvas); > for (Drawable dr : mDrawables) { > dr.draw(canvas); > } > } > > } > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---