android – 使用翻译动画将视图从一个布局转换为另一个布局

我是 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->最佳
底部布局 – id – >底部

现在一个视图(Button – > btnSearchHeader)位于我的顶部布局中,我希望将相同的动画设置为底部布局(它应该会产生影响,它将翻译动画转换为底部布局)点击该布局按钮,当用户点击该按钮时,它应该再次转换回原始位置并使用平移动画..即它应该显示在顶部布局中

我不知道如何使用翻译动画来提供这些影响,但是我只有一个基本的翻译动画知识,这对我来说不足以满足我的要求.

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

谢谢

解决方法

你尝试过如下简单的东西吗?
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,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类似的监听器.)

但是,我怀疑你需要实际做到这一点,以达到你想要的视觉效果.但是如果你做这个部分,你可能还需要为你的顶视图设置一个固定的高度而不是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. 
}

相关文章

AdvserView.java package com.earen.viewflipper; import an...
ImageView的scaleType的属性有好几种,分别是matrix(默认)...
文章浏览阅读8.8k次,点赞9次,收藏20次。本文操作环境:win1...
文章浏览阅读1.2w次,点赞15次,收藏69次。实现目的:由main...
文章浏览阅读3.8w次。前言:最近在找Android上的全局代理软件...
文章浏览阅读2.5w次,点赞17次,收藏6次。创建项目后,运行项...