Show/Hide View using Slide Up and Down Animation

2019-03-16 16:02发布

问题:

Still showing and hiding view like this:

if(isChecked)
{           
    linearLayoutMap.setVisibility(View.VISIBLE);
}
else 
{
    linearLayoutMap.setVisibility(View.GONE);               
}

but what if I have to show and hide using Slide Up and Slide Down Animation

回答1:

Create below xml in anim folder

slid_down.xml :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="1000"
    android:fromYDelta="0"
    android:toYDelta="100%" />
</set>

slid_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="1000"
    android:fromYDelta="100%"
    android:toYDelta="0" />
</set>

Create Amim common util class :

public class MyUtils {

public void SlideUP(View view,Context context)
{
 view.startAnimation(AnimationUtils.loadAnimation(context,
            R.anim.slid_down));
}

public void SlideDown(View view,Context context)
{
 view.startAnimation(AnimationUtils.loadAnimation(context,
            R.anim.slid_up));
}


}