Looped animation in ImageView - android

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

Related

Can we use Android TranslateAnimation for 2 different location?

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.

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 Animated Turns

I'm using the following code to make turns (left or right) in a series. So if i call it one after another ie: turn(90); turn(90); turn(-90); all it shows is the last one. I'd like to show them all and it wait until the first one is done before moving on to the next. Any idea's?
public void turn(int i)
{
RotateAnimation anim = new RotateAnimation( currentRotation, currentRotation + i,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
currentRotation = (currentRotation + i) % 360;
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(1000);
anim.setFillEnabled(true);
anim.setFillAfter(true);
token.startAnimation(anim);
}
So what i did was create a que of the animations and an iterator to run through it... after i defined all the animations that needed to be done, added them to the que, i ran the first one, then within the AnimationListener handled the rest.
Queue<RotateAnimation> que = new LinkedList<RotateAnimation>();
Iterator<RotateAnimation> queIt;
public void turnToken(int i){
RotateAnimation anim = new RotateAnimation( currentRotation, currentRotation + i,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
currentRotation = (currentRotation + i) % 360;
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(1000);
anim.setFillEnabled(true);
anim.setAnimationListener(this);
anim.setFillAfter(true);
que.add(anim);
}
AnimationListener:
#Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
if(queIt.hasNext()){
token.startAnimation((Animation) queIt.next());
}
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}

How to move and fade out any View with Animation

Ok, so I have a View where its being animated with the following code:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.productPage_parentLayout_RL);
ImageView iv = new ImageView( ProductPage.this );
imageLoader.DisplayImage(FULL_PRODUCT_INFO.getFirstImage(), iv);
Animation animation = new TranslateAnimation(0, helper.getDeviceWidth() - 100,0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
rl.addView(iv);
With this code, the view starts from the left side of the screen and moves to almost the end of the screen, but I also want to fade-out the view and hide/destroy it in the end. I tried to search anything related to this, but couldn't find any.
Any help is appreciated.
Try this
ObjectAnimator move=ObjectAnimator.ofFloat(iv, "translationY",100f);
move.setDuration(3000);
ObjectAnimator alpha1=ObjectAnimator.ofFloat(iv, "alpha",0.5f);
alpha1.setDuration(1000);
ObjectAnimator alpha2=ObjectAnimator.ofFloat(iv, "alpha",0);
alpha2.setDuration(2000);
AnimatorSet animset=new AnimatorSet();
animset.play(alpha2).before(alpha1).with(move);
animset.start();
Animation a = new AlphaAnimation(1.0f, 0.0f);
a.setDuration(1000);
a.setFillAfter(true);
iv.startAnimation(a);
AnimationSet set = new AnimationSet(true);
set.addAnimation(animation)
set.addAnimation(a)
set.setFillAfter(true);
iv.startAnimation(set);
set.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) {
iv.setVisibility(View.INVISIBLE);
}
});

Slide down view in android

I would like to have a button in my android application trigger a slide down view of a form. I want to have a view at the top of the screen, a list at the bottom of the screen, and I want to have the slide down form view appear between the two when a button is clicked.
I have no problem showing the view, but can't seem to animate it from hidden to shown on the screen.
Any ideas on how this could work?
public void doSlideDown(View view){
RelativeLayout myView = (RelativeLayout)findViewById(R.id.my_view);
addListingView.setVisibility(myView.VISIBLE);
Animation slideDown = setLayoutAnim_slidedown();
myView.startAnimation(slideDown);
}
public void doSlideUp(View view){
RelativeLayout myView = (RelativeLayout)findViewById(R.id.my_view);
Animation slideUp = setLayoutAnim_slideup();
myView.startAnimation(slideUp);
}
public Animation setLayoutAnim_slidedown() {
AnimationSet set = new AnimationSet(true);
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, -1.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(800);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
// MapContacts.this.mapviewgroup.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.d("LA","sliding down ended");
}
});
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.25f);
return animation;
}
public Animation setLayoutAnim_slideup() {
AnimationSet set = new AnimationSet(true);
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f);
animation.setDuration(800);
animation.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
RelativeLayout bodyView = (RelativeLayout)findViewById(R.id.bodyView);
RelativeLayout myView = (RelativeLayout)findViewById(R.id.my_view);
addListingView.clearAnimation();
bodyView.removeView(myView);
}
});
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.25f);
return animation;
}
This may help you:
ANDROID ANIMATION SLIDEDOWN SLIDEUP
Android Animation Framework

Categories

Resources