How to move and fade out any View with Animation - android

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

Related

Why view animation is not gone

I am new in Android. I created ripple animation to hide view, but view do not gone.
Animator anim = ViewAnimationUtils.createCircularReveal(view, 150, 150, 200f, 0f);
anim.start();
I expect that view gone after animation is end.
First of all, the right name for this animation is circular reveal.
You should add the listener to your animation and override method named as onAnimationEnd, then start an animation.
In the onAnimationEnd you should set visibility to the view.
Animator anim = ViewAnimationUtils.createCircularReveal(view, 150, 150, 200f, 0f);
anim.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.INVISIBLE);
}
});
anim.start();
You need to set the duration of the animation
Animator.setDuration(1000)
for example for 1000ms
You need to use setAnimationListener .
Follow this method:
Animator anim = ViewAnimationUtils.createCircularReveal(view, 150, 150, 200f, 0f);
anim.setDuration(1000);
anim.start();
anima.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) {
animation.cancel();
//hide
}
});

Android animation does not repeat

I wanna make an animation. It has to fade in and fade out.
Works, but, I have to make it inifinite. The animation works once time.
This is my code:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
final AnimationSet animation = new AnimationSet(false); //change to false
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.REVERSE);
linearLayout.setAnimation(animation);
I'm trying to solving like this:
Click here
But is not the same case. I'm doing everything programatically.
Somebody can help me?
Thanks!
Try this:
Animation fadeIn , zoomout;
private void setAnimationOnPlayButton(){
fadeIn = AnimationUtils.loadAnimation(getActivity(), R.anim.fadeIn );
fadeOut = AnimationUtils.loadAnimation(getActivity(), R.anim.fadeOut );
btnPlayVideoInPlayer.setAnimation(fadeIn);
btnPlayVideoInPlayer.setAnimation(fadeOut);
btnPlayVideoInPlayer.startAnimation(fadeIn);
fadeIn .setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation arg0) {
btnPlayVideoInPlayer.startAnimation(zoomout);
}
});
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation arg0) {
btnPlayVideoInPlayer.startAnimation(fadeIn );
}
});
}
Where R.anim.fadein and R.anim.fadeout is animation file in anim folder
I just tested this code and it works exactly how you want it to.
First of all make a new android resource directory under src>main>res called anim.
Then make a new animation resource file and call it anim_fade.xml.
Paste the following code in it.
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="1000" android:repeatMode="reverse"
android:repeatCount="infinite"/>
After you do that go to your java file and start the animation in your class. If you want it to start on app launch then paste it in your onCreate method, and if you want it to start onClick or anywhere else paste it in that class.
public void example{
//Major Animation code here
Animation fade = AnimationUtils.loadAnimation(this, R.anim.anim_fade);
linearlayout.startAnimation(fade);
}

Android: Rotate animation and move animation combination

I have an ImageView image. I need to rotate this image 90 degrease right and after that to move this image from left to right. I managed how to do that. I used AnnimationListener and after rotation finished I started moveAnimation(). But before moving image returns to it original look(before rotation).
xml code for rotation rotation.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:interpolator="#android:anim/linear_interpolator"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:startOffset="0"
/>
rotateAnimation()
private void rotateAnimation(){
Animation rotation = AnimationUtils.loadAnimation(getContext(), R.anim.rotate);
rotation.setRepeatCount(0);
rotation.setFillAfter(true);
rotation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
moveAnnimation();
}
});
moveAnnimation()
private void moveAnnimation(){
TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 2000, 0, 0);
moveLefttoRight.setDuration(1000);
moveLefttoRight.setFillAfter(true);
moveLefttoRight.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) {
// TODO Auto-generated method stub
}
});
image.startAnimation(moveLefttoRight);
}
You need to setFillAfter(true) for both the rotation and translation Animation object.
Animation.html#setFillAfter(boolean)
If fillAfter is true, the transformation that this animation performed will persist when it is finished. Defaults to false if not set. Note that this applies to individual animations and when using an AnimationSet to chain animations.
Following is my code, you need AnimationSet to achieve the chaining Animation Effect.
public class MainActivity extends Activity {
ImageView image = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.imageview);
animateImageView();
}
private void animateImageView() {
AnimationSet set = new AnimationSet(true);
set.setFillAfter(true);
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 200, 0, 0);
moveLefttoRight.setDuration(1000);
moveLefttoRight.setStartOffset(1000);
set.addAnimation(rotation);
set.addAnimation(moveLefttoRight);
image.startAnimation( set );
}
}

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

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