Animation: fade in, fade out
package app.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class Test extends Activity implements View.OnClickListener {
View viewToAnimate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.toggleButton);
button.setOnClickListener(this);
viewToAnimate = findViewById(R.id.theView);
}
@Override
public void onClick(View v) {
if(viewToAnimate.getVisibility() == View.VISIBLE) {
Animation out = AnimationUtils.makeOutAnimation(this, true);
viewToAnimate.startAnimation(out);
viewToAnimate.setVisibility(View.INVISIBLE);
} else {
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
viewToAnimate.startAnimation(in);
viewToAnimate.setVisibility(View.VISIBLE);
}
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/toggleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to Toggle"
/>
<View
android:id="@+id/theView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AAA"
/>
</LinearLayout>
Related examples in the same category
1. | Frame based animation | | |
2. | extends Animation to create your own animation | | |
3. | Slide out down animation, bounce in down animation | | |
4. | XML for Fade in animation | | |
5. | Xml for Fade out animation | | |
6. | XML for slide out animation | | |
7. | Animation Cloning | | |
8. | Animation Loading | | |
9. | Multi Property Animation | | |
10. | Animation: push up in,push up out,push left in,push left out,fade in,fade out,hyperspace in,hyperspace out | | |
11. | Using AlphaAnimation class to do animation in code | | |
12. | Animation Interpolator | | |
13. | Using TranslateAnimation class | | |
14. | Demonstrates the seeking capability of ValueAnimator. | | |
15. | Animating by calling invalidate() from draw(),loading and drawing resources, handling onPause() in an animation | | |