[英]How to slide animate and make a view visible from right to left in Android
I want to make a view visible with slide animation effect from right to left and vise versa. 我想使用从右到左的幻灯片动画效果使视图可见,反之亦然。 I've successfully been able to hide a view with left to right slide animation but unable to achieve the other one. 我已经成功地能够以从左到右的幻灯片动画隐藏视图,但是无法实现另一个视图。 Following is the code snippet that I'm using : 以下是我正在使用的代码段:
private void showLayout(){
if(mContainerLayout.getVisibility() == View.VISIBLE){
mContainerLayout.animate()
.translationX(mContainerLayout.getWidth())
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mContainerLayout.setVisibility(View.GONE);
}
}).start();
}
else{
mContainerLayout.animate()
.translationX(0)
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mContainerLayout.setVisibility(View.VISIBLE);
}
}).start();
}
}
You can add a fade animation instead: 您可以添加渐变动画:
private void showLayout(boolean show){
if (show){
mContainerLayout.animate()
.translationX(0)
.setDuration(300)
.alpha(1)
.start();
} else {
mContainerLayout.animate()
.translationX(mContainerLayout.getWidth())
.alpha(0)
.setDuration(300)
.start();
}
}
Create a file slide_in_anim.xml
inside anim
folder under res
folder and add this code 在res
文件夹下的anim
文件夹内创建文件slide_in_anim.xml
并添加此代码
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fillAfter="true"
android:fromXDelta="-100%p"
android:toXDelta="0%p" />
</set>
Then use this animation as 然后将此动画用作
Animation slideAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_in_up);
mContainerLayout.startAnimation(slideAnimation);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.