繁体   English   中英

Android:在多个linearLayouts之间重新排列子项

[英]Android: Rearrange children between multiple linearLayouts

我正致力于在不同布局中设置几个图像视图,以模拟足球形成。

例如。 这是配置1:

===============================


 1        2        3       4

===============================


      5        6        7

===============================


      8        9        10

===============================

上面的数字是各个水平LinearLayouts内的图像视图。 我想要切换到配置2(在最顶层的LinearLayout中包含5个数字,在中间布局中包含2个。下限不变。)

===============================


 1      2      3     4     5

===============================


           6        7

===============================


      8        9        10

===============================

现在,到目前为止,我能够在线性布局中动态添加和删除imageViews。 然而,过渡似乎非常突然。 所以我想知道动画是否可用于显示:

  1. 上部布局元素由于新到达而调整其位置。
  2. 5号从中间移动到顶部。
  3. 中间行元素调整它们的位置以便考虑删除。

需要注意的是,由于Number 5不再是mid布局的成员,因此可以显示它的转换。

编辑:上面的配置更改只是一个代表性的例子。 实际上,可以存在多种形式,并且可以从任何一种形成过渡到任何其他形式。 该解决方案也必须是“流动的”并且不依赖于硬编码的像素值。 到目前为止,由于删除或添加,我能够处理重新排列部分(当然没有动画)。

为了解决你的问题,我将使用翻译动画。

以下是如何执行此操作的示例。

RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(relativeLayout);

final ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_your_icon);
relativeLayout.addView(imageView);

final float amountToMoveRight = imageView.getX() + 100;
final float amountToMoveDown = imageView.getY() + 100;

TranslateAnimation anim = new TranslateAnimation(imageView.getX(), imageView.getX() + 100, imageView.getY(), imageView.getY() + 100);
anim.setDuration(2000);

anim.setAnimationListener(new TranslateAnimation.AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        params.topMargin += amountToMoveDown;
        params.leftMargin += amountToMoveRight;
        imageView.setLayoutParams(params);
    }
});

imageView.startAnimation(anim);

您可以使用imageView.getX();获取每个视图的位置imageView.getX(); imageView.getY();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM