Hi I am creating a game with three ImageViews that gradually move down and then when they reach a certain point the ImageViews return to the top of the screen and so the ImageViews move in a continuous cycle. I have tried using a translate animation to move the ImageViews around. However all the ImageViews remain stationary which means that I haven't set the the translate animation up correctly.
Here is the code that I have used (speedtwo is a double, and timetaken is a long that I set up)
double speedtwominus = speedtwo * -1;
final TranslateAnimation anim = new TranslateAnimation(0, 0, 0, (float) speedtwo);
anim.setDuration(timetaken);
final TranslateAnimation finish = new TranslateAnimation(0, 0, 0, (float) speedtwominus);
anim.setDuration(0);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
numone.startAnimation(anim);
}
#Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
numone.startAnimation(finish);
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
});
Related
TranslateAnimation is use when you want to move image in different possintion like
left to right
up to down
one XYscalse position to other XYscale position
Syntax
TranslateAnimation animation = new TranslateAnimation(StartinXscale,StartingYscale,EndXscale,EndYscale);
Methods
animation.setDuration(millisecond);//move speed.
animation.setRepeatCount(int value);//how many time you want to move it from starting to ending position.
animation.setRepeatMode(int value);//mode like goto destination and return back to main position.
imageView.startAnimation(animation);//Start animation on imageView
Can we use above code twice time in one activity?
MyCode
private void animationAction() {
float StartX = 500.0f;
float StartY = -300.0f;
float EndX = -300.0f;
float EndY = 500.0f;
int i = 0;
for (i = 0; i <2; i++) {
TranslateAnimation animation = new TranslateAnimation(StartX,StartX + EndX, StartY, StartY + EndY);
animation.setDuration(3000);
animation.setRepeatCount(5);
animation.setRepeatMode(2);
animation.setFillAfter(true);
img_animation.startAnimation(animation);
EndX = 300.0f;
}
In MyCode result was only execute one time
// Animation
private Animation animMove;
private Animation animFadein;
//Method to preform 2 animation in sequence
private void doAnimation() {
// load the animation
animMove = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);
animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
// start move up animation on brnetwork text
imageView.startAnimation(animMove);
// set animation listener
animMove.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.background);
relMain.setBackgroundDrawable(drawable);
llInput.startAnimation(animFadein);
llInput.setVisibility(View.VISIBLE);
tvSignUp.startAnimation(animFadein);
tvVideo.startAnimation(animFadein);
tvSignUp.setVisibility(View.VISIBLE);
tvVideo.setVisibility(View.VISIBLE);
}
});
// set animation listener
animFadein.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
}
after MOVE animation i have to perform FEDIN animation
in your case you can replace FEDIN by your Next Animation.
i want to touch button and drag into other button position you can get clear idea by this image.
Guiding Image
Please help me
I Also Found This very helpful.
Touch and drag image in android
but i want to drag to other button position not some were else on screen.i don't have any idea how i would detect button is entered in other button dimensions.
So far i am doing is just animation but i don't want just straight forward moving button by click.
i want user to move by himself button to other button position.
Code
public class AnimationActivity extends Activity implements OnClickListener {
public Button btn_a1, btn_a2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainsec);
btn_a1 = (Button) findViewById(R.id.btn_a1);
btn_a2 = (Button) findViewById(R.id.btn_a2);
btn_a1.setOnClickListener(this);
btn_a2.setOnClickListener(this);
}
#Override
public void onClick(final View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_a1: {
int direction = -1;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
RelativeLayout.LayoutParams absParams = (RelativeLayout.LayoutParams) btn_a1
.getLayoutParams();
final float xDelta = (displaymetrics.widthPixels / 2)
- absParams.leftMargin - (btn_a1.getWidth() / 2);
final Animation animation = new TranslateAnimation(0, xDelta
* direction, 0, 0);
AnimationListener animationOutListener = new AnimationListener() {
public void onAnimationEnd(Animation animation) {
btn_a2.setBackgroundDrawable(R.id.blank);// Unselect
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btn_a2.setBackgroundDrawable(R.id.red);// Select
}
};
animation.setAnimationListener(animationOutListener);
animation.setDuration(1000);
btn_a2.startAnimation(animation);
break;
}
case R.id.btn_a2: {
int direction = 1;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
RelativeLayout.LayoutParams absParams = (RelativeLayout.LayoutParams) btn_a1
.getLayoutParams();
final float xDelta = (displaymetrics.widthPixels / 2)
- absParams.leftMargin - (btn_a1.getWidth() / 2);
final Animation animation = new TranslateAnimation(0, xDelta
* direction, 0, 0);
AnimationListener animationOutListener = new AnimationListener() {
public void onAnimationEnd(Animation animation) {
btn_a1.setBackgroundDrawable(R.id.blank);// Unselect
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btn_a1.setBackgroundDrawable(R.id.red);// Select
}
};
animation.setDuration(1000);
btn_a1.startAnimation(animation);
break;
}
}
}
How about this:
You would have to evaluate the buttons parentViews onTouchEvents, and if you hit it, you can start the onClick method associated with the button, or when you lift your finger, what ever suits your app.
But while finger down/move you can just change the buttons coordinates and trigger a redraw ont he parent view.
You might have to create a new View that supports moving Buttons. I dont know if you can do that inside of a layout.
In my android app I have a view which is laid on top of another view. When I click a button this top view is animated and goes out of screen. The problem is that it is like the view is still "there" as it captures all touch events and they do not get passed to the view which is behind.
This is the code that animates the view away:
TranslateAnimation animation = new TranslateAnimation (0, 0, 0, height);
animation.setDuration(1000);
animation.setFillAfter(true);
topView.startAnimation(animation);
What can I do to solve this problem?
Create an animation Listener and remove the view from its parent when the animation is over.
Animation animation = new AlphaAnimation(0 ,0);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation animation) {
parentView.removeView(topView)
}
});
I have 3 horizontal draggable views. For example, if I drop first on the second - the second and the third must move to the right.
The initial position:
The code:
case DragEvent.ACTION_DROP:
targetIndex = ((ViewInfo) targetView.getTag()).index;
int lastIndex = getChildCount() - 1;
droppedView = (View) event.getLocalState();
droppedIndex = ((ViewInfo) droppedView.getTag()).index;
if (droppedView == this) {
return false;
}
for (int i = targetIndex; i <= lastIndex; i++) {
if (getChildAt(i) != droppedView) {
moveChildToRight(i);
}
}
private void moveChildToRight(int index) {
....
TranslateAnimation anim = new TranslateAnimation(0, 200, 0, 0);
anim.setDuration(1000);
anim.setFillAfter(true);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
Log.d(TAG, "Animation ended");
requestLayout();
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
child.startAnimation(anim);
}
OK, after drop the 2nd and the 3rd views move to the right:
After that I long-press to the empty space between 1st and 2nd+3rd views, and what do I see? Drag of the 2nd view is initiated! But it's in fact shown more righter, than it can be touched. The "visual" representation of the 2nd or 3rd views are not touchable after animation:
Why?
I realize this is over a year old, but I'll answer in case someone else finds the page.
This is how translate animation (view animation) works. It moves the object visually, but doesn't update the actually X/Y property. For example, if your views were buttons, then touching in the original area would show the click on the where the button visually is, but clicking on where you see the button would do nothing, as the actual button never moved.
I have an ImageView in my screen and I want to make it shake (rotate left and then rotate right).
I know how to animate an ImageView, this is my code:
new RotateAnimation(20f, 50f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);
// Start animating the image
final ImageView splash = (ImageView) findViewById(R.id.mobileshake);
splash.startAnimation(anim);
The problem is, right now the Imageview is looping one animation, but I want 2 animations to loop (rotate left and then rotate right).
How can I do this?
Sorry for my bad English..
You can combine two (or more) animations using an AnimationSet.
There is an example of a "Shake" animation in API Demos using TranslateAnimation defined in xml. You can achieve the result you are looking for by following a similar approach.
I figured it out by doing the following and works very smooth :)
final RotateAnimation anim1 = new RotateAnimation(20f, 50f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim1.setInterpolator(new LinearInterpolator());
//anim1.setRepeatCount(Animation.INFINITE);
anim1.setDuration(300);
final RotateAnimation anim2 = new RotateAnimation(50f, 20f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim2.setInterpolator(new LinearInterpolator());
//anim2.setRepeatCount(Animation.INFINITE);
anim2.setDuration(300);
final ImageView splash = (ImageView) findViewById(R.id.mobileshake);
anim1.setAnimationListener(new AnimationListener(){
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
splash.startAnimation(anim2);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}});
anim2.setAnimationListener(new AnimationListener(){
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
splash.startAnimation(anim1);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}});
splash.startAnimation(anim1);