gpt4 book ai didi

java - Android - TranslateAnimation 动态更新布局的大小

转载 作者:行者123 更新时间:2023-11-30 11:27:22 28 4
gpt4 key购买 nike

我正在尝试制作这样的动画:

上下文:

我有两个 EditText,我需要当你点击一个时,另一个从第一个后面出现。在这里你有一些图片来获得我想要的图像。 Application in standby Application after you click the first EditText

显然,要做到这一点,我需要一个 TranslateAnimation 以便将第二个 EditText 从第一个后面移动。

我的方法:

我想到的第一件事是使用 FrameLayoutEditText 一个放在另一个上,然后在第一个的 onTouch 事件中执行 TranslateAnimation 在第二个。这个问题是,如果我在 wrap_content 中有 FrameLayout 高度,那么用户将看不到动画。如果我在运行时更改 FrameLayout 的高度,我将在第一个 EditText 下方留下空白,如您在这张图片中所见:

enter image description here

我认为的第二个解决方案是向 TranslateAnimation 添加一个 AnimationListener 并在 onAnimationStart 方法中更改 的高度框架布局。但这样做的问题是 FrameLayout 的高度变化太突然了。我想让动画保持流畅。

我的问题:

如何从第一个 EditText 后面获得第二个 EditText 的平滑动画,将 FrameLayout 的高度更改为第二个 EditText 向下移动?

谢谢!

更新:

我用 RelativeLayout 更改了 FrameLayout 我搜索过,这种情况没有区别。我尝试缩放此包含 EditTextAnimationScaleRelativeLayout 以顺利显示动画,但没有成功。这是我的代码:

    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 和下面的布局。这是代码。顺便说一句,由于其他原因,我通过自定义 ViewPager 更改了 ListView,这不会改变任何内容。

  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 个在 EditText 上,然后一个在 FrameLayout 上?给他们两个相同的精确增量和持续时间。这样,FrameLayout 会平滑的向下移动,然后在动画结束时,改变 FrameLayout 的高度。

希望这有帮助:)

关于java - Android - TranslateAnimation 动态更新布局的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19341499/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com