Android Fade In/Out animation from bottom to top - android

I am creating a animation in which I want to fade out my imageview but the fading animation should start from bottom to top unlike fading animation in which whole imageview fades at once.
public void animateFadeIn() {
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
motherboard.startAnimation(in);
motherboard.setVisibility(View.VISIBLE);
in.setDuration(1500);
in.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
animateFadeOut();
}
});
}
How to achieve this.
<?xml version="1.0" encoding="UTF-8"?>
<scale
android:fromYScale="-450"
android:toYScale="450" />
<alpha
android:duration="1500"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:repeatCount="infinite"
android:toAlpha="1.0" />

create a slide_up.xml in your
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="5000"
android:fillAfter="true"
android:fromYDelta="75%p"
android:repeatCount="0"
android:toYDelta="0%p" />
your fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
than load animation
ImageView imageview;
Animation animFadein, animslideup;
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
animslideup = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_up);
final AnimationSet s = new AnimationSet(true);
s.setInterpolator(new AccelerateInterpolator());
s.addAnimation(animslideup);
s.addAnimation(animFadein);
imageview.startAnimation(s);

Related

Android Animation repeat -> image animation set

i am trying to scale an image, rotate it, rotate it backwards and then scale it to its origninal size.
This is allready working, but i cant figure out how to repeat this animation set infinitly ( android:repeatCount="infinite" isnt working for me ).
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true"
android:repeatCount="infinite"
>
<scale
android:fromXScale="1.0"
android:toXScale="4.0"
android:fromYScale="1.0"
android:toYScale="4.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="700"
/>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="2000"
/>
<rotate
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="2700"
android:duration="2000"
/>
<scale
android:fromXScale="1.0"
android:toXScale="0.25"
android:fromYScale="1.0"
android:toYScale="0.25"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="4700"
android:duration="700"
/>
</set>
and in the Activity:
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
Animation rotateAndScale = AnimationUtils.loadAnimation(this, R.anim.rotate_z);
imageView.startAnimation(rotateAndScale);
<set> marker in xml is bad implemented and not work propperly. Full explenation here: Android animation does not repeat
what you should do is use listener and method to use recurency to roll the anim for ever.
public void startAnimation() {
View component= findViewById(R.id.imageView2);
component.setVisibility(View.VISIBLE);
Animation anim = AnimationUtils.loadAnimation(this, R.anim.rotate_z);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.rotate_z);
anim.setAnimationListener(this);
component.startAnimation(anim);
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
component.startAnimation(anim);
}

Ping App Animation Android

I am just curious. How do I implement animation in ping app when I start/replace one activity/fragment from other. In IOS we can add a mask layer in oval shape and modify the alpha values. How do i implement this in android?
This is the solution:
Create the shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#android:color/black"/>
</shape>
Set it to your ImageView:
<ImageView
android:layout_width="60dp" //or another
android:layout_height="60dp" //or another
android:id="#+id/ping"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#drawable/shape" />
Then create two animations scale_up.xml and scale_down.xml:
scale_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.5"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:fillBefore="true" />
</set>
scale_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.5"
android:fromYScale="1.5"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:fillBefore="true" />
</set>
And finally startAnimation() in code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView ping = (ImageView) findViewById(R.id.ping);
final Animation scaleDown = AnimationUtils.loadAnimation(this, R.anim.scale_down);
final Animation scaleUp = AnimationUtils.loadAnimation(this, R.anim.scale_up);
scaleDown.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
ping.startAnimation(scaleUp);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
scaleUp.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
ping.startAnimation(scaleDown);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
ping.startAnimation(scaleUp);
}

Rotation animation, slow it down

I need to slow down an rotate animation over an imageview: it begin faster, then it should "decelerate" until end of animation (then rotation stop). I've write this:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="100"
android:fromDegrees="0"
android:interpolator="#android:anim/cycle_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="30"
android:repeatMode="restart"
android:toDegrees="360" />
</set>
then add listener to animation:
Animation rotate = AnimationUtils
.loadAnimation(activity, R.anim.rotate);
ImageView logo = (ImageView) SplashScreen.activity
.findViewById(R.id.logo);
rotate.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent(SplashScreen.this,
LoginActivity.class);
SplashScreen.this.startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
if(animation.getRepeatCount() == 5) {
animation.setDuration(200);
} else if (animation.getRepeatCount() == 10) {
Log.i("ANIM", "10");
animation.setDuration(5000);
} else if (animation.getRepeatCount() == 15) {
animation.setDuration(800);
} else if (animation.getRepeatCount() == 20) {
animation.setDuration(1600);
} else if (animation.getRepeatCount() == 25) {
animation.setDuration(2000);
}
}
});
logo.setAnimation(rotate);
logo.startAnimation(rotate);
but animation has always the same velocity (code never go into onAnimationRepeat). What's wrong?
Simply use
android:interpolator="#android:anim/decelerate_interpolator"
in your animation xml file.
check this link for other interpolators http://developer.android.com/reference/android/view/animation/package-summary.html
also add
android:repeatCount="1"
because the default is 0.
Try this :
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="100"
android:fromDegrees="0"
android:interpolator="#android:anim/decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="30"
android:repeatMode="restart"
android:toDegrees="360" />
</set>
Change interpolator cycle_interpolator to decelerate_interpolator so that you get an effect which is faster at beginning and gradually slows down.

Change ImageView image during animation

I have an ImageView. I want to:
1 - shrink the view
2 - change the image resource
3 - scale back the view
I tried to this in the following section of code, but this is not working. It behaves like the 3rd row doesn't exist. Can someone tell me what I'm doing wrong?
Animation anim = AnimationUtils.loadAnimation(this, R.anim.scale);
Animation animo = AnimationUtils.loadAnimation(this, R.anim.scale_out);
i.startAnimation(anim);
i.setImageResource(R.drawable.logoc);
i.startAnimation(animo);
scale.xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="0.125"
android:fromYScale="1.0"
android:toYScale="0.125"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:duration="400"
android:fillBefore="true" />
scale_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="4.0"
android:fromYScale="1"
android:toYScale="4.0"
android:startOffset="400"
android:pivotX="50%"
android:pivotY="50%"
android:duration="800"
/>
I think you should try doing this way:
i.startAnimation(anim);
if (anim.hasEnded())
{
i.setImageResource(R.drawable.logoc);
i.startAnimation(animo);
}
Edit:
Ok, I was wrong: This works:
public class MyAnimationListener implements AnimationListener {
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
i.setImageResource(R.drawable.bg);
i.startAnimation(animo);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
MyAnimationListener mListener = new MyAnimationListener();
anim.setAnimationListener(mListener);
i.startAnimation(anim);

Rotating Image View Continuously in Android

I am using following code to rotate image view
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:duration="500"
android:repeatCount="infinite"
android:pivotX="50%"
android:pivotY="50%"
>
</rotate>
</set>
Animation rotate1 = AnimationUtils.loadAnimation(this, R.anim.rotate_picture);
rotate.startAnimation(rotate1);
The Layout which I am using is:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/loader"
android:layout_centerInParent="true"
android:id="#+id/rotate"
/>
</RelativeLayout>
But it is stop 500ms and restarting again. But I need to rotate image continuously. Without stopping it in middle. How can I do this?
//custom_anim.xml
<?xml version="1.0" encoding="utf-8" ?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000" />
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000">
</alpha>
<scale
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale=".1"
android:fromYScale=".1"
android:toXScale="1.0"
android:toYScale="1.0"
android:duration="2000" />
</set>
//Oncreate
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
imgageview.startAnimation(rotateimage);
rotateimage.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
AnimateandSlideShow();
}
});
//function
private void AnimateandSlideShow() {
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
imgageview.startAnimation(rotateimage);
rotateimage.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
AnimateandSlideShow();
}
});
}
I just tried it. For some odd reason it uses the AccelerateDecelerateInterpolator as default. Add android:interpolator="#android:anim/linear_interpolator" and you will get something more useful.
However there is still a tiny stop after every 500ms, I think it reloads the animation or it has one frame too much (0 = 360)
You can increase the values for a longer duration to get a better effect. Try android:duration="50000" and android:toDegrees="36000".
Add this property in rotate attribute
android:repeatMode="restart"
Or change your rotate1.xml file, remove set tag. Like this:
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="500"
android:repeatCount="infinite"
android:pivotX="50%"
android:pivotY="50%">
</rotate>

Categories

Resources