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(0xffff0000);
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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---