android - 使用 Transition Framework 恢复或重置场景

标签 android android-animation

我正在关注 tutorial并想用它做一些我自己的事情。该教程向我展示了如何制作动画。所以我这样做了,现在我想恢复以前的 View (场景)。首先是淡入新 View ,效果很好,但恢复/重置失败。它应该表现得像这样:

enter image description here

我将 onClickListener 设置为菜单条 (ImageView),并切换 boolean 值以检查 View 是否已满。我遇到的一个问题是我必须在转换结束时重新分配 onClickListener,因为新布局在转换过程中膨胀。但是当我在三个按钮可见后单击时,它不会注册我的单击(通过调试我检查过)。

这里是代码文件:

public class HorizotalViewActivity extends AppCompatActivity implements Transition.TransitionListener, View.OnClickListener
{


    boolean viewing = false;
    ImageView goButton;
    Scene scene, restoreScene;

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

        ViewGroup sceneView = (ViewGroup) findViewById( R.id.sceneView );


        scene = Scene.getSceneForLayout( sceneView,
                R.layout.transition_example2, this );

        restoreScene = Scene.getSceneForLayout( sceneView,
                R.layout.transition_example, this );


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


        goButton.setOnClickListener( this );


    }



    @Override public void onTransitionStart( Transition transition )
    {

    }

    @Override public void onTransitionEnd( Transition transition )
    {
        goButton = (ImageView) findViewById( R.id.goButton );


        goButton.setOnClickListener( this );
    }
//...

    @Override public void onClick( View view )
    {
        if ( !viewing )
        {
            TransitionManager.go( scene );
            viewing = true;
        }
        else
        {


            TransitionManager.go( restoreScene );

            //goToScene( restoreScene );


            viewing = false;
        }
    }
}

这里是 xml 文件:

Activity 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sceneView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.lgvalle.material_animations.HorizotalViewActivity">
<include layout="@layout/transition_example"/>

</LinearLayout>

默认布局(transition_example.xml):

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scene"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/goButton"
           android:src="@drawable/menu"
            android:layout_width="60dp"
            android:layout_height="60dp" />

    </LinearLayout>

过渡布局(transition_example2.xml):

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scene"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/goButton"
        android:src="@drawable/menu"

        android:layout_width="60dp"
        android:layout_height="60dp" />

    <ImageView
        android:id="@+id/play"
        android:layout_marginLeft="15dp"
        android:src="@drawable/play"
        android:layout_width="60dp"
        android:layout_height="60dp" />
    <ImageView
        android:id="@+id/repeat"
        android:layout_marginLeft="15dp"
        android:src="@drawable/repeat"
        android:layout_width="60dp"
        android:layout_height="60dp" />
    <ImageView
        android:id="@+id/share"
        android:layout_marginLeft="15dp"
        android:src="@drawable/share"
        android:layout_width="60dp"
        android:layout_height="60dp" />


</LinearLayout>

我看不出我的逻辑有任何问题,它是 self 解释的,

1 - 检查 boolean 转换 2 是否已经发生

1A - 进行转换,更新 boolean

2 - 如果转换发生

2A - 调用之前的场景,更新boolean

现在,我猜 onCickListener 存在这个问题,因为一旦 TransitionManager.go() 出现,我就无法在 onClickListener 中设置断点调用,如果它得到注册,它可能会按预期工作。没有 logcat 错误/警告。

最佳答案

我的项目也有同样的情况,这是我发现的:

当您使用场景转换时会发生什么,您有 2 个将设置动画的 View 集。让我们说 Set A(开始布局)和 Set B(结束布局)。您在代码中所做的是在集合 A 上设置点击监听器。但是在场景转换之后,集合 A 不再出现在屏幕上。

转换后再次设置您的点击监听器将解决您的问题。 (当您使用包含内容值的 CustomView 时,您也需要“复制”。

为此,请使用 Scene.setEnterAction(Runnable)方法,

scene.setEnterAction(new Runnable(){

@override
void run()
{

   goButton = (ImageView) scene.getSceneRoot.findViewById( R.id.goButton );


   goButton.setOnClickListener( this );

}

});

关于android - 使用 Transition Framework 恢复或重置场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40604341/

相关文章:

java - 如何使用 jFreeChart 做双域十字准线

android - 显示隐藏的工具栏动画不流畅

android - 在 Activity 生命周期中什么时候开始动画?

android - Android 中从左到右的无缝 Activity 过渡动画

android - Delphi XE5 Android 中的广播接收器

java - Android - Acra 不报告

android - ViewPager 中的 ListView 第一次不刷新 (Android)

java - 在加载 Activity 之前加载 Admob 广告,以便它们立即出现而不是在 3-5 秒后出现?

安卓 : Draw Circle With Text Inside

android - 将 View 动画转换到屏幕边缘