场景:
假设我单击一个按钮,它出现在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>
更精确的要求规格(请仔细阅读:)
这里我有两个子内部布局: –
现在一个视图(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. }
原文地址:https://www.jb51.cc/android/314994.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。