簡體   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