【问题标题】:Translate a View from One Layout to Another with Translate Animation使用翻译动画将视图从一种布局转换为另一种布局
【发布时间】:2014-04-10 03:47:22
【问题描述】:

我是 Android 动画的新手,我的要求是在单击该视图时将视图从一个布局转换为单个 xml 文件中的布局。

场景: 假设我单击一个按钮,出现在 xml 文件的标题顶部,它应该向下移动/翻译(它应该会产生影响,它位于向下到标题的其他布局上),而且我希望当用户再次点击它,它现在应该移动到原来的位置。

这里我用我的 xml 文件解释:

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/app_bg"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/header"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >

        <Button
            android:id="@+id/btnSearchHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerInParent="true"
            android:background="@drawable/search_icon" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/bottom"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/app_transparent"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_marginTop="10dp"
        android:visibility="visible" >

        <Button
            android:id="@+id/btnMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:text="ABC" />

        <Button
            android:id="@+id/btnSearchSelected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/btnMenu"
            android:text="CDE" />
    </RelativeLayout>
</LinearLayout>

更精确的要求规范(请仔细阅读:)

这里我有两个子内部布局:-

顶部布局 - id-> top 底部布局- id -> 底部

现在一个视图 (Button -> btnSearchHeader) 位于我的顶部布局中,我想在点击时将其动画到底部布局(它应该会产生影响,它通过翻译动画被翻译到底部布局)那个按钮,当用户点击那个按钮时,它应该再次用翻译动画翻译回它的原始位置..即它应该显示在顶部布局中

我不知道如何使用翻译动画来产生这些影响,但是我只有基本的翻译动画知识,不足以满足我的要求。

任何类型的相关帮助都是可观的。

谢谢

【问题讨论】:

  • 你可以尝试的是,三个按钮A(在顶部),B,C(在底部),点击A,它变得不可见,B变得可见并转换为C,它变得不可见,并且 C 变得可见,反之亦然
  • 也许你还没有检查过这个:developer.android.com/training/animation/layout.html
  • 仍然不清楚你在问什么。也许图像会有所帮助。

标签: android android-layout translate-animation


【解决方案1】:

您是否尝试过类似以下的简单方法?

 final int topHeight = findViewById(R.id.top).getHeight();         
 final int bottomHeight = findViewById(R.id.bottom).getHeight();   
 final View button = findViewById(R.id.btnSearchHeader);                                                                              
 final ObjectAnimator moveDownAnim = ObjectAnimator.ofFloat(button, "translationY", 0.F, topHeight + bottomHeight / 2 - button.getHeight() / 2);
 final ObjectAnimator moveUpAnim = ObjectAnimator.ofFloat(button, "translationY", topHeight + bottomHeight / 2 - button.getHeight() / 2, 0.F);
 button.setOnClickListener(new View.OnClickListener() { 
     @Override                   
     public void onClick(View v) {     
         if (0.F == v.getTranslationY())
             moveDownAnim.start(); 
         else                    
             moveUpAnim.start();
     }
 });

如果你确实需要按钮视图来改变父母,你可以在每个动画结束时使用AnimatorListener来实现。比如:

moveDownAnim.addListener(new Animator.AnimatorListener() { 
    @Override                                          
    public void onAnimationEnd(Animator animation) {   
        ((ViewGroup)findViewById(R.id.top)).removeView(button);
        ((ViewGroup)findViewById(R.id.bottom)).addView(button);
        ((RelativeLayout)button.getLayoutParams()).addRule(RelativeLayout.CENTER_IN_PARENT);
        button.setTranslationY(0.F);        // Since it is now positioned in the new layout, no need for translation. 
    }       
    @Override
    public void onAnimationCancel(Animator animation)  { /* NOP */ }
    @Override
    public void onAnimationRepeat(Animator animation)  { /* NOP */ }
    @Override
    public void onAnimationStart(Animator animation)   { /* NOP */ }
});

(以及moveUpAnim 的类似监听器。)

但是,我怀疑您是否需要实际执行此操作才能达到您想要的视觉效果。但是如果你做了这部分,你可能还需要为top 视图设置一个固定的高度,而不是wrap_content。 (否则,如果在按钮移动到底部视图时发生布局传递,则顶部布局的高度可能会变为 0,如果其中没有其他内容。)最简单的方法是直接在 xml 布局文件中执行此操作。但是,如果您想“即时执行”,您可以在 onAnimationEnd() 方法中使用以下内容更改布局的高度:

@Override                                          
public void onAnimationEnd(Animator animation) { 
    final ViewGroup topLayout = findViewById(R.id.top);
    topLayout.getLayoutParams().height = topLayout.getHeight();    // Keep it the same height...
    topLayout.removeView(button);
    ((ViewGroup)findViewById(R.id.bottom)).addView(button);
    ((RelativeLayout)button.getLayoutParams()).addRule(RelativeLayout.CENTER_IN_PARENT);
    button.setTranslationY(0.F);        // Since it is now positioned in the new layout, no need for translation. 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多