[android-beginners] translate just one circle not whole canvas
Hi Group, I am getting used to android animations, and have come across an issue. To keep it easy to explain, I have simplified my code. here I am creating two circles, a red one and a green one. I now want to use a translation animation on just the green circle circle when I click the screen. I have all this working except when i click the screen both circles move. How can I apply a translation animation to just one circle (e.g. in my code mDrawable[0])? Thanks Graham package com.image; import android.app.Activity; import android.os.Bundle; public class image extends Activity { /** Called when the activity is first created. */ private MoveCircleByTouch mMoveCircleByTouch; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //last argument 1 = green, all others = red mMoveCircleByTouch = new MoveCircleByTouch(this); setContentView(mMoveCircleByTouch); } } 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.MotionEvent; import android.view.View; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.view.animation.TranslateAnimation; public class MoveCircleByTouch extends View{ private ShapeDrawable[] mDrawable; private Animation anim; private Animation translate; public MoveCircleByTouch(Context context) { super(context); int x = 20; int y = 20; int width = 50; int height = 50; mDrawable = new ShapeDrawable[2]; //green circle mDrawable[0] = new ShapeDrawable(new OvalShape()); mDrawable[0].getPaint().setColor(0xff74AC23); mDrawable[0].setBounds(x, y, x + width, y + height); /red circle mDrawable[1] = new ShapeDrawable(new OvalShape()); mDrawable[1].getPaint().setColor(0x); mDrawable[1].setBounds(x+50, y+50, x + width+50, y + height +50); } public void onDraw(Canvas canvas){ super.onDraw(canvas); if (translate == null) { createTranslate(mDrawable[0]); } for (Drawable dr : mDrawable) { dr.draw(canvas); } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: ScreenTouched(); invalidate(); break; } return true; } public void ScreenTouched(){ startAnimation(translate); } private void createTranslate(ShapeDrawable mDrawable){ translate = new TranslateAnimation(1, 100, 1, 100); translate.setRepeatCount(1); translate.setDuration(2000); } } --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-beginners] Re: onTouch()
Thanks. Exactly the answer i was looking for. On Aug 4, 4:43 pm, Balwinder Kaur (T-Mobile) balwinder.k...@t- mobile.com wrote: 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(0x); 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); } } }- Hide quoted text - - Show quoted text - --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-beginners] Re: Adding objects to a canvas
:) Thank you. I created an array of objects to fix the issue so have an array of circles the onDraw method is using. On Aug 3, 7:13 pm, Yusuf T. Mobile yusuf.s...@t-mobile.com wrote: The onDraw() function is the phone's way of asking you so how do you want me to draw this canvas, from scratch? If you say draw a circle there!, you get one circle. If you want it to draw two circles, your CustomDrawableView needs to tell it to draw two different circles every time onDraw() is called, for example with two different drawables. Yusuf Saib Android ·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 3, 6:30 am, Graham gxt...@gmail.com wrote: Hi, I am new to the android developing world and have what I think is a very simple question. I have created a circle on a canvas and drawn it successfully. I now wish to add another circle to this already existing canvas in a different position. I have defined my x and y as arguments I pass to custom drawable view but when I try and get another circle drawn it just replaces the one that was already present. How do I keep the first circle and add on a second? (My code below does not show trying to add another circle, it is simply what works with creating a single colour circle). Thanks G 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, 150, 40, green); setContentView(mCustomDrawableView); } } package com.image; import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.view.View; public class CustomDrawableView extends View { private ShapeDrawable mDrawable; public CustomDrawableView(Context context, int x, int y, String Colour) { super(context); //int x = 80; //int y = 90; int width = 50; int height = 50; mDrawable = new ShapeDrawable(new OvalShape()); if (Colour == green){ mDrawable.getPaint().setColor(0xff74AC23);//green } else if (Colour == red) { mDrawable.getPaint().setColor(0x);//red } mDrawable.setBounds(x, y, x + width, y + height); } protected void onDraw(Canvas canvas) { mDrawable.draw(canvas); } }- Hide quoted text - - Show quoted text - --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-beginners] onTouch()
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(0x); 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 -~--~~~~--~~--~--~---
[android-beginners] Adding objects to a canvas
Hi, I am new to the android developing world and have what I think is a very simple question. I have created a circle on a canvas and drawn it successfully. I now wish to add another circle to this already existing canvas in a different position. I have defined my x and y as arguments I pass to custom drawable view but when I try and get another circle drawn it just replaces the one that was already present. How do I keep the first circle and add on a second? (My code below does not show trying to add another circle, it is simply what works with creating a single colour circle). Thanks G 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, 150, 40, green); setContentView(mCustomDrawableView); } } package com.image; import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.view.View; public class CustomDrawableView extends View { private ShapeDrawable mDrawable; public CustomDrawableView(Context context, int x, int y, String Colour) { super(context); //int x = 80; //int y = 90; int width = 50; int height = 50; mDrawable = new ShapeDrawable(new OvalShape()); if (Colour == green){ mDrawable.getPaint().setColor(0xff74AC23);//green } else if (Colour == red) { mDrawable.getPaint().setColor(0x);//red } mDrawable.setBounds(x, y, x + width, y + height); } protected void onDraw(Canvas canvas) { mDrawable.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 -~--~~~~--~~--~--~---
[android-beginners] Traceview: trace file not found
In an attempt to debug my app, I was trying to use 'traceview' (one of the apps in the the android SDK). In the windows edition of the SDK version 1.0, the executable is 'traceview.bat'. I was able to get a tracefile by using the 'android.os.Debug' package and using Debug.startMethodTracing(basename); and Debug.stopMethodTracing(); in my code. I created a virtual sdcard for the emulator to use, and then I ran the app on the emulator and was successful in creating a trace file, which I pulled to a directory. Here was my problem: Whenever I tried to view the trace using 'traceview.bat', it said that the file is not found. This was hard to understand since I specified the right directory. I even went into that directory. It still couldn't find it. I finally resolved it when I specified the whole path using the notation C:/topdir/nextdir/... etc. If you're not familiar with Cygwin (a Unix shell for windows), you also have the choice of specifying a path with /cygdrive/c/topdir/..., but 'traceview' won't parse that. Hope this helps someone. It stalled me for hours. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---