Can we use Android TranslateAnimation for 2 different location? - android

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.

Related

How to create a translate animation

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
}
});

Android animation when scroll

I want do a animate scroll like Jquery´s (in this web page works when you click in top menu´s options) in android. is it possible? I have seen that exists scrollTo but is possible to do with animation and not like jumps. Thank you very much.
Using ObjectAnimator, This is a sample for scrolling to top :
public void scroolToTop() {
int x = 0;
int y = 0;
ObjectAnimator xTranslate = ObjectAnimator.ofInt(mScrollView, "scrollX", x);
ObjectAnimator yTranslate = ObjectAnimator.ofInt(mScrollView, "scrollY", y);
AnimatorSet animators = new AnimatorSet();
animators.setDuration(1000L);
animators.playTogether(xTranslate, yTranslate);
animators.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub
}
});
animators.start();
}

Get new position of Layout after animation ends

Small question, in android after you use the TranslateAnimation how do you get the position it has taken.
In my app when the user touches an image it will move up and show a input screen.
But when the image reaches its final position i want to stick the image to its new location.
But when i use getHeight or getLayoutParams i always get the old position.
public void moveProducts() {
float addAmmountY = -250;
TranslateAnimation anim = new TranslateAnimation(0,0,0,addAmmountY); //(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
anim.setDuration(1000);
final LinearLayout product_buttons = (LinearLayout)findViewById(R.id.product_buttons);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//TODO set new location of the linearlayout
}
});
product_buttons.startAnimation(anim);
}
try this:
#Override
public void onAnimationEnd(Animation animation) {
product_buttons.requestLayout();
}
After the animation try to get the new position of the image.

Drag One Button To Other Button Position

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.

Looped animation in ImageView

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);

Categories

Resources