使视图的可见性从动画变为可见 [英] Animate visibility of a view from gone to visible with animation

查看:148
本文介绍了使视图的可见性从动画变为可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图默认为不可见(只是第一次)。

I have a view that is invisible by default(Just for the first time).

现在需要使用以下动画将可见性切换为 VISIBLE

Now I need to switch the visibility to VISIBLE with this animation:

if (myView.getVisibility() == View.INVISIBLE) {
    myView.setVisibility(View.VISIBLE);
    myView.animate().translationY(0);
 }

(类似于SnackBar默认动画)

(Like the SnackBar default animation)

但这不起作用。它将通过默认动画变为可见

But this isn't working. It will turn visible with default animation

有没有简单的方法可以实现这一目标?

Is there any simple way that I could achieve this?

注意

我正在设定我的观点,以消除这种情况:

I'm animating my view to dismiss, like this:

myView.animate().translationY(myView.getHeight());


推荐答案

您可以使用 XML 动画。

创建幻灯片动画 XML code> set 和 alpha 并将此 XML 放入资源 anim 文件夹。

Create a slide-up animation XML using set and alpha and put this XML into your resource anim folder.

slide_up.xml

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

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

使用情况:

使用 AnimationUtils.loadAnimation() XML 加载动画,并使用<$ c $设置并启动动画c> .startAnimation()方法。

Use AnimationUtils.loadAnimation() to load animation from XML and set and start animation using .startAnimation() method.

以下是示例:

ImageView imageView = (ImageView) findViewById(R.id.imageView);

// slide-up animation
Animation slideUp = AnimationUtils.loadAnimation(this, R.anim.slide_up);

if (imageView.getVisibility() == View.INVISIBLE) {
    imageView.setVisibility(View.VISIBLE);
    imageView.startAnimation(slideUp);
}

希望这会有所帮助〜

这篇关于使视图的可见性从动画变为可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆