有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试制作一个如下的动画:

上下文:

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

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

我的方法:

我能想到的第一件事是在使用FrameLayout时,把EditText放在另一个上面,然后在第一个的onTouch事件中对第二个做TranslateAnimation。问题是,如果我在wrap_content中有FrameLayout高度,那么用户将看不到动画。如果我在运行时改变FrameLayout的高度,我会在第一个EditText的下方留下一个空隙,如图所示:

enter image description here

我认为的第二个解决方案是向TranslateAnimation添加一个AnimationListener,并在onAnimationStart方法中更改FrameLayout的高度。但问题是FrameLayout的高度变化太突然。我想保持动画平滑

我的问题:

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

谢谢

更新:

我用我搜索的{}改变了{},这个案例没有区别。我尝试用AnimationScale缩放包含两个EditTextRelativeLayout以平滑显示动画,但没有成功。这是我的密码:

    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. # 1 楼答案

    如何使用第二个选项&;尝试同时执行两个平移动画:1个在EditText上,然后一个在FrameLayout上?给他们两个相同的精确增量和持续时间。这样,FrameLayout将平滑向下移动,然后在动画结束时更改FrameLayout的高度

    希望这有帮助:)