java Android TranslateAnimation动态更新布局的大小
我正在尝试制作一个如下的动画:
上下文:
我有两个{
为了做到这一点,我显然需要一个TranslateAnimation
,以便从第一个EditText
后面移动第二个
我的方法:
我能想到的第一件事是在使用FrameLayout
时,把EditText
放在另一个上面,然后在第一个的onTouch事件中对第二个做TranslateAnimation
。问题是,如果我在wrap_content
中有FrameLayout
高度,那么用户将看不到动画。如果我在运行时改变FrameLayout
的高度,我会在第一个EditText
的下方留下一个空隙,如图所示:
我认为的第二个解决方案是向TranslateAnimation
添加一个AnimationListener
,并在onAnimationStart
方法中更改FrameLayout
的高度。但问题是FrameLayout
的高度变化太突然。我想保持动画平滑
我的问题:
如何从第一个EditText
后面获得第二个EditText
的平滑动画,并在第二个EditText
向下移动时更改FrameLayout
的高度
谢谢
更新:
我用我搜索的{AnimationScale
缩放包含两个EditText
的RelativeLayout
以平滑显示动画,但没有成功。这是我的密码:
protected void expanse() {
Log.d("Accordion", "expandAccordion");
//Translate the top of the second EditText to its bottom
TranslateAnimation translateSecond = new TranslateAnimation(second.getLeft(), second.getLeft(),
second.getTop(), second.getBottom());
translateSecond.setDuration(1000);
//Scale the relative layout to show the both of them
ScaleAnimation scaleContainer = new ScaleAnimation(container.getLeft(), container.getLeft(),
container.getTop(), second.getBottom());
scaleContainer.setDuration(1000);
//At the end of the scaling, change the height of the relative layout
scaleContainer.setAnimationListener(new AnimationListenerAdapter() {
@Override
public void onAnimationEnd(Animation animation) {
RelativeLayout.LayoutParams params = (LayoutParams) container.getLayoutParams();
params.height = second.getBottom();
container.setLayoutParams(params);
super.onAnimationEnd(animation);
}
});
container.startAnimation(scaleContainer);
second.startAnimation(translateSecond);
}
顺便说一句,我可以保证,如果我硬编码一些大的高度,如350dp,动画显示正确
更新:
我试着移动第二个EditText
和下面的布局。这是代码。顺便说一句,由于另一个原因,我通过自定义ListView
更改了ViewPager
,这不会改变任何东西
public class MainActivity extends FragmentActivity {
private EditText first;
private EditText second;
private HoboViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (HoboViewPager) findViewById(R.id.pager);
pager.setPageTransformer(true, new RotationPageTransformer());
pager.setAdapter(new SampleAdapter(this, getSupportFragmentManager()));
first = (EditText) findViewById(R.id.location_search_field);
second = (EditText) findViewById(R.id.term_search_field);
first.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
expanseSecondField();
return true;
}
});
}
protected void expanseSecondField() {
TranslateAnimation translateSecondField = new TranslateAnimation(second.getLeft(), second.getLeft(),
second.getTop(), second.getBottom());
translateSecondField.setFillAfter(true);
translateSecondField.setDuration(1000);
TranslateAnimation translateContainer = new TranslateAnimation(pager.getLeft(), pager.getLeft(),
pager.getTop(), pager.getTop() + second.getBottom());
translateContainer.setFillAfter(true);
translateContainer.setDuration(1000);
pager.startAnimation(translateContainer);
second.startAnimation(translateSecondField);
}
}
这不起作用,因为容器的TranslateAnimation会立即执行。结果与我在动画结束时尝试更改容器大小时的结果相同
# 1 楼答案
如何使用第二个选项&;尝试同时执行两个平移动画:1个在EditText上,然后一个在FrameLayout上?给他们两个相同的精确增量和持续时间。这样,FrameLayout将平滑向下移动,然后在动画结束时更改FrameLayout的高度
希望这有帮助:)