java 布局之间的淡入淡出效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4533731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:48:25  来源:igfitidea点击:

Fade effect between layouts

javaandroidlayoutfade

提问by CeccoCQ

As by object, I would reproduce fade effect between two layout. Now I've this situation:

作为对象,我会在两个布局之间重现淡入淡出效果。现在我有这种情况:

LinearLayout l;
LinearLayout l2;

To switch between them I've used

要在它们之间切换,我用过

l.setVisibility(View.GONE);
l2.setVisibility(View.VISIBLE);

I want add fade effect between this transiction, how I can do it?

我想在此转换之间添加淡入淡出效果,我该怎么做?

采纳答案by CeccoCQ

Using R.anim.fade_out & .R.anim.fade_in you can create an animation which does this. I don't know much about this myself but heres a tutorial regarding animations in android: Animation Tutorial

使用 R.anim.fade_out 和 .R.anim.fade_in 你可以创建一个动画。我自己对此不太了解,但这里有一个关于 android动画的教程动画教程

P.S. This tutorial is not mine thus credit does not go out to me.

PS 本教程不是我的,因此信用不属于我。

Edit:

编辑:

AnimationSet set = new AnimationSet(true);

Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
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(100);
set.addAnimation(animation);

LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
l.setLayoutAnimation(controller);

Fade out Animation

淡出动画

public static Animation runFadeOutAnimationOn(Activity ctx, View target) {
  Animation animation = AnimationUtils.loadAnimation(ctx,android.R.anim.fade_out);
  target.startAnimation(animation);
  return animation;
}

I'm guessing you can try something like this, I copy pasted the animation from the tutorial I don't know what it does exactly as I have no experience with Android development. Another example could be Example 2

我猜你可以尝试这样的事情,我从教程中复制粘贴了动画我不知道它到底是做什么的,因为我没有 Android 开发经验。另一个例子可能是例 2

回答by Ixx

Here is a working solution to cross fade between 2 layouts:

这是在 2 个布局之间淡入淡出的有效解决方案:

public class CrossFadeActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.crossfade);

    final View l1 = findViewById(R.id.l1);
    final View l2 = findViewById(R.id.l2);

    final Animation fadeOut = AnimationUtils.loadAnimation(CrossFadeActivity.this, R.anim.fade_out);
    final Animation fadeIn = AnimationUtils.loadAnimation(CrossFadeActivity.this, R.anim.fade_in);

    findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            fadeOut.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    l1.setVisibility(View.GONE);
                }
            });
            l1.startAnimation(fadeOut);
            l2.setVisibility(View.VISIBLE);
            l2.startAnimation(fadeIn);
        }
    });
    findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            fadeOut.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    l2.setVisibility(View.GONE);
                }
            });
            l2.startAnimation(fadeOut);
            l1.setVisibility(View.VISIBLE);
            l1.startAnimation(fadeIn);
        }
    });
}
}

crossfade.xml:

淡入淡出.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/l1"
    android:layout_width="fill_parent"
    android:layout_height="300dip"
    android:orientation="vertical" 
    android:background="@drawable/someimage"
    >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

<RelativeLayout
    android:id="@+id/l2"
    android:layout_width="fill_parent"
    android:layout_height="300dip"
    android:orientation="vertical" 
    android:background="@drawable/someimage2"
    android:visibility="gone"
    >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" 
        android:layout_centerInParent="true"
        />

</RelativeLayout>

</RelativeLayout>

Where l1 and l2 are 2 random example layouts. The trick is to put them in XML such that they overlap each other (e.g. in a RelativeLayout) with visible / gone, add listeners to the animations to toggle the visibility on finish, and set the view which has to fade in to visible before the animation starts, otherwise the animation will not be visible.

其中 l1 和 l2 是 2 个随机示例布局。诀窍是将它们放在 XML 中,使它们彼此重叠(例如在相对布局中)与可见/消失,向动画添加侦听器以切换完成时的可见性,并将必须淡入的视图设置为可见动画开始,否则动画将不可见。

I put the buttons with the listeners to toggle the animation in the layouts itself, because I need to implement it that way but the click listener can be of course somewhere else (if it's only one it should be used in combination with some flag or check to know how to toggle).

我将按钮与侦听器放在一起以在布局本身中切换动画,因为我需要以这种方式实现它,但单击侦听器当然可以在其他地方(如果它只是一个,它应该与某些标志或检查结合使用)知道如何切换)。

These are the animation files. They have to be stored in folder res/anim:

这些是动画文件。它们必须存储在文件夹 res/anim 中:

fade_in.xml

淡入淡出.xml

<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromAlpha="0.0"
android:toAlpha="1.0" />

fade_out.xml

淡出.xml

<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromAlpha="1.0"
android:toAlpha="0" />

UPDATE:

更新:

Instead of using R.anim.fade_in, you can use the default fade_in from Android API (android.R.fade_in):

您可以使用 Android API ( android.R.fade_in) 中的默认淡入淡出,而不是使用 R.anim.fade_in :

final Animation fadeIn = AnimationUtils.loadAnimation(CrossFadeActivity.this, android.R.anim.fade_in);

Using android.R.anim.fade_in, you will not need to create the file res/anim/fade_in.xml.

使用 android.R.anim.fade_in,您将不需要创建文件 res/anim/fade_in.xml。

Android has a package with some useful animations on android.R.anim: http://developer.android.com/reference/android/R.anim.html

Android 在 android.R.anim 上有一个包含一些有用动画的包:http: //developer.android.com/reference/android/R.anim.html

回答by binW

Since android 3.0 you can use

从 android 3.0 开始,您可以使用

android:animateLayoutChanges="true"

in a layout in xml and any changes to this specific layouts content at run time will be animated. For example, in your case, you will need to set this attribute for the parent for the parent layout of l and l2 e.g

在 xml 布局中,运行时对此特定布局内容的任何更改都将被动画化。例如,在您的情况下,您需要为 l 和 l2 的父布局的父级设置此属性,例如

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="me.kalem.android.RegistrationActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:animateLayoutChanges="true"
    android:padding="20dp">

    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="visible">

    ........

    </LinearLayout>

    <LinearLayout
        android:id="@+id/l2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone">

    ........

    </LinearLayout>

</LinearLayout>

Now hiding l1 and showing l2 at runtime will be animated.

现在隐藏 l1 并在运行时显示 l2 将被动画化。