简体   繁体   English

淡入淡出在 MotionLayout 中不起作用

[英]Fade in, fade out not working in MotionLayout

I'm trying to make an effect of fade in, fade out using alpha and motionLayout, but it seems that it's not working.我正在尝试使用 alpha 和 motionLayout 制作淡入淡出的效果,但似乎不起作用。

This is the imageView that I want to fade.这是我想要淡出的 imageView。

    <ImageView
    android:id="@+id/tijeras"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:contentDescription="@string/tijeras"
    android:src="@drawable/ic_tijeras" />
    </android.support.constraint.motion.MotionLayout>

And this is the motion file这是运动文件

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Transition
        app:duration="2000"
        app:constraintSetStart="@+id/start"
        app:constraintSetEnd="@+id/end">
        <KeyFrameSet>
            <KeyAttribute
                app:framePosition="0"
                app:motionTarget="@id/tijeras"
                android:alpha="1.0"/>
            <KeyAttribute
                app:framePosition="50"
                app:motionTarget="@id/tijeras"
                android:alpha="0.0"/>
        </KeyFrameSet>
    </Transition>
    <ConstraintSet android:id="@+id/start">
        <Constraint android:id="@+id/tijeras"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="1.0"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/logo"/>
    </ConstraintSet>
    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@+id/tijeras"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="1.0"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/logo"/>
    </ConstraintSet>
</MotionScene>

I can see the image, but it's not doing the alpha in and out.我可以看到图像,但它没有进行 alpha 输入和输出。 Any idea?任何的想法? Do I need to trigger it for starting?我需要触发它才能启动吗?

I think that i have found the problem and the solution.我想我已经找到了问题和解决方案。 So you are defining two ConstraintSet (start, end) and in both alpha = 1, it means that your view is visible in both of them right ?所以你定义了两个 ConstraintSet (start, end) 并且在两个 alpha = 1 中,这意味着你的视图在它们两个中都是可见的,对吗? Now let's see your KeyFrameSet现在让我们看看你的 KeyFrameSet

<KeyFrameSet>
        <KeyAttribute
            app:framePosition="0"
            app:motionTarget="@id/tijeras"
            android:alpha="1.0"/>
        <KeyAttribute
            app:framePosition="50"
            app:motionTarget="@id/tijeras"
            android:alpha="0.0"/>
    </KeyFrameSet>

You're saying that at framePosition = 0 your view is visible (alpha=1), then in the middle of your transition (framePosition = 50 ) you are hiding your view (alpha=0).你是说在 framePosition = 0 你的视图是可见的 (alpha=1),然后在你的过渡中间 (framePosition = 50) 你隐藏你的视图 (alpha=0)。 It means that when you are at framePosition = 100 (end of your transition) the alpha will be 1 again because in your ConstraintSet (end) is equal to 1.这意味着当您处于 framePosition = 100(过渡结束)时,alpha 将再次为 1,因为在您的 ConstraintSet(结束)中等于 1。

So try to change it like this, instead of 50, framePosition = 100所以尽量改成这样,而不是50,framePosition = 100

<KeyFrameSet>
        <KeyAttribute
            app:framePosition="0"
            app:motionTarget="@id/tijeras"
            android:alpha="1.0"/>
        <KeyAttribute
            app:framePosition="100"
            app:motionTarget="@id/tijeras"
            android:alpha="0.0"/>
    </KeyFrameSet>

It looks like you have set the alpha as 1.0 android:alpha="1.0" on both the start and end ConstraintSet .看起来您已经在开始和结束ConstraintSet上将android:alpha="1.0"设置为 1.0 android:alpha="1.0"

You can more easily set the alpha to update with a CustomAttribute by removing alpha from the KeyAttribute and placing the following below your Transition element通过从KeyAttribute删除 alpha 并将以下内容放置在Transition元素下方,您可以更轻松地将 alpha 设置为使用CustomAttribute进行更新

<ConstraintSet android:id="@+id/start">
    <Constraint android:id="@+id/tijeras"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/logo">
        <CustomAttribute
            motion:attributeName="alpha"
            motion:customFloatValue="0.0" />
    </Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
    <Constraint android:id="@+id/tijeras"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alpha="1.0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/logo">
        <CustomAttribute
            motion:attributeName="alpha"
            motion:customFloatValue="1.0" />
    </Constraint>
</ConstraintSet>

There is no need setting a CustomAttribute , this solution should work:无需设置CustomAttribute ,此解决方案应该有效:

BaseView基本视图

<!-- Other views -->

<ImageView
    android:id="@+id/tijeras"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:contentDescription="@string/tijeras"
    android:src="@drawable/ic_tijeras" />

Content Scene内容场景

<MotionScene xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
     <Transition
        app:duration="2000"
        app:constraintSetStart="@+id/start"
        app:constraintSetEnd="@+id/end">

        <KeyFrameSet>
            <KeyAttribute
                app:framePosition="50"
                app:motionTarget="@id/tijeras"
                android:alpha="0.0"/> <!-- Set alpha to zero -->
        </KeyFrameSet>
    </Transition>

    <ConstraintSet android:id="@+id/start">
    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@id/tijeras" <!-- without + -->
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="0.0" <!-- set alpha to zero -->
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/logo"/>
    </ConstraintSet>

</MotionScene>

I have not worked with MotionLayout but I was able to achieve a fade in effect for ImageView using the Animation Java class.我没有使用 MotionLayout,但我能够使用 Animation Java 类为 ImageView 实现淡入淡出效果。

First, the Java code.首先是Java代码。 mContext is simply the Context of the Activity/Fragment and can be retrieved by calling getApplicationContext() if you don't already have it saved as a global variable. mContext 只是 Activity/Fragment 的上下文,如果您还没有将其保存为全局变量,则可以通过调用 getApplicationContext() 来检索它。

ImageView mIV = mRootView.findViewById(R.id.bank_prof_iv);
Animation mAnim = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
mIV.startAnimation(mAnim);

Next, the fade in effect XML code in fade_in.xml.接下来,fade_in.xml 中的淡入效果 XML 代码。 You can of course change the duration and offset.您当然可以更改持续时间和偏移量。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:startOffset="600"
android:interpolator="@android:anim/accelerate_interpolator">

<scale
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromXScale="0.9"
    android:toXScale="1.0"
    android:fromYScale="0.9"
    android:toYScale="1.0"/>

<alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0"/>

</set>

I was able to achieve a fade out effect using the same format of Java code, just create a new Animation object and call startAnimation() whenever you want the fade out effect to take place.我能够使用相同格式的 Java 代码实现淡出效果,只需创建一个新的 Animation 对象并在您想要淡出效果发生时调用 startAnimation()。 The XML is achieved by simply flipping the fromAlpha and toAlpha values. XML 是通过简单地翻转 fromAlpha 和 toAlpha 值来实现的。

<alpha
    android:fromAlpha="1.0"
    android:toAlpha="0.0"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM