This post is related to show animation on any view in android.
1) Simple Android Animation on View GONE/VISIBLE
If you just want a better transition view on GONE / VISIBLE rather than the default Android show/hide, I came across some simple trick to make a simple animation using TranslateAnimation class.
*Note: For a better transitioning, you can use animationlistener , and setVisibility onAnimationStart() or onAnimationEnd().
2) In below code "rlSearchByLocationSub" is RelaiveLayout.
if (location) {
imgBtnLocation.setBackgroundResource(R.drawable.arrow_down);
rlSearchByNameSub.setVisibility(View.GONE);
rlSearchByLocationSub.setVisibility(View.VISIBLE);
// Animation
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(150);
set.addAnimation(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(500);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.5f);
rlSearchByLocationSub.setLayoutAnimation(controller);
location = false;
} else {
imgBtnLocation.setBackgroundResource(R.drawable.arrow_right);
// Animation
AlphaAnimation fade_out = new AlphaAnimation(1.0f, 0.0f);
fade_out.setDuration(500);
fade_out.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0) {
}
public void onAnimationRepeat(Animation arg0) {
}
public void onAnimationEnd(Animation arg0) {
rlSearchByLocationSub.setVisibility(View.GONE);
}
});
rlSearchByLocationSub.startAnimation(fade_out);
location = true;
}
1) Simple Android Animation on View GONE/VISIBLE
If you just want a better transition view on GONE / VISIBLE rather than the default Android show/hide, I came across some simple trick to make a simple animation using TranslateAnimation class.
*Note: For a better transitioning, you can use animationlistener , and setVisibility onAnimationStart() or onAnimationEnd().
// View to be animate (I use ImageView as example here)
ImageView imageView = (ImageView)findViewById(R.id.imageview);
Below are the methods to set view from VISIBLE to GONE
// To animate view slide out from
left
to
right
public void slideToRight(View view){
TranslateAnimation animate = new TranslateAnimation(
0
,view.getWidth(),
0
,
0
);
animate.setDuration(
500
);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
// To animate view slide out from
right
to
left
public void slideToLeft(View view){
TranslateAnimation animate = new TranslateAnimation(
0
,-view.getWidth(),
0
,
0
);
animate.setDuration(
500
);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
// To animate view slide out from
top
to
bottom
public void slideToBottom(View view){
TranslateAnimation animate = new TranslateAnimation(
0
,
0
,
0
,view.getHeight());
animate.setDuration(
500
);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
// To animate view slide out from
bottom
to
top
public void slideToTop(View view){
TranslateAnimation animate = new TranslateAnimation(
0
,
0
,
0
,-view.getHeight());
animate.setDuration(
500
);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
2) In below code "rlSearchByLocationSub" is RelaiveLayout.
if (location) {
imgBtnLocation.setBackgroundResource(R.drawable.arrow_down);
rlSearchByNameSub.setVisibility(View.GONE);
rlSearchByLocationSub.setVisibility(View.VISIBLE);
// Animation
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(150);
set.addAnimation(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(500);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.5f);
rlSearchByLocationSub.setLayoutAnimation(controller);
location = false;
} else {
imgBtnLocation.setBackgroundResource(R.drawable.arrow_right);
// Animation
AlphaAnimation fade_out = new AlphaAnimation(1.0f, 0.0f);
fade_out.setDuration(500);
fade_out.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0) {
}
public void onAnimationRepeat(Animation arg0) {
}
public void onAnimationEnd(Animation arg0) {
rlSearchByLocationSub.setVisibility(View.GONE);
}
});
rlSearchByLocationSub.startAnimation(fade_out);
location = true;
}
3) To show the button call this
AlphaAnimation fade_in = new AlphaAnimation(0.0f, 1.0f);
fade_in.setDuration(500);
fade_in.setAnimationListener(new AnimationListener()
{
public void onAnimationStart(Animation arg0)
{
}
public void onAnimationRepeat(Animation arg0)
{
}
public void onAnimationEnd(Animation arg0)
{
myButton.setVisibility(View.VISIBLE);
}
});
myButton.startAnimation(fade_in);
Then to hide the button:AlphaAnimation fade_out = new AlphaAnimation(1.0f, 0.0f);
fade_out.setDuration(upcoming_animation_time);
fade_out.setAnimationListener(new AnimationListener()
{
public void onAnimationStart(Animation arg0)
{
}
public void onAnimationRepeat(Animation arg0)
{
}
public void onAnimationEnd(Animation arg0)
{
myButton.setVisibility(View.GONE);
}
});
myButton.startAnimation(fade_out);
4)If
I want to make an
Animation
for when aView
gets it's visibility set
to GONE.
public class MainActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.q2634073); findViewById(R.id.item1).setOnClickListener(this); } @Override public void onClick(View view) { view.startAnimation(new MyScaler(1.0f, 1.0f, 1.0f, 0.0f,
500, view, true));
}
public class MyScaler extends ScaleAnimation {
private View mView;
private LayoutParams mLayoutParams;
private int mMarginBottomFromY, mMarginBottomToY;
private boolean mVanishAfter = false;
public MyScaler(float fromX, float toX, float fromY, float toY,
int duration, View view,boolean vanishAfter) {
super(fromX, toX, fromY, toY);
setDuration(duration);
mView = view;
mVanishAfter = vanishAfter;
mLayoutParams = (LayoutParams) view.getLayoutParams();
int height = mView.getHeight();
mMarginBottomFromY = (int) (height * fromY)
+ mLayoutParams.bottomMargin - height;
mMarginBottomToY = (int) (0 - ((height * toY)
+ mLayoutParams.bottomMargin)) - height;
}
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
int newMarginBottom = mMarginBottomFromY
+ (int) ((mMarginBottomToY - mMarginBottomFromY)
* interpolatedTime);
mLayoutParams.setMargins(mLayoutParams.leftMargin,
mLayoutParams.topMargin,mLayoutParams.rightMargin,
newMarginBottom);
mView.getParent().requestLayout();
} else if (mVanishAfter) {
mView.setVisibility(View.GONE);
}
}
}
}
Comments
Post a Comment