簡體   English   中英

帶有翻譯動畫的Android View-定位無法正常工作

[英]Android View with a translate animation - positioning is not working as expected

我有一個具有OnClickListener的視圖。 單擊后,視圖將平移到頁面上的特定位置。 這是沒有問題的,視圖可以到達應有的位置。 當再次單擊該視圖時,我想將其放置在其他位置,但事實並非如此。 經過一番排查后,我發現我的View的getTop()方法返回了相同的值-即使在平移動畫將視圖移動到屏幕的不同部分之后也是如此。 對於第二個動畫,它不使用當前位置(正如我期望的那樣),而是使用初始位置。

我正在做的一些事情:我使用的是ObjectAnimation類而不是TranslationAnimation類,因為我想保持OnClickListener功能。 使用TranslationAnimation類,我發現視圖已正確移動,但是OnClickListener僅在View所在的區域中工作。 使用ObjectAnimation類,我可以輕松地使翻譯正常工作,並且OnClickListener功能正確-在屏幕上當前視圖的位置觸發它。

這是我到目前為止的內容:

final LinearLayout child = layouts.get(i); //ArrayList containing some dynamic layouts
final int offset = target - child.getTop(); 
ObjectAnimator anim = ObjectAnimator.ofFloat(child,"translationY",offset);
anim.setDuration(250);
anim.start();

這是第一次單擊視圖時發生的情況。 它沿Y軸向上平移,其中的偏移量確定了View需要從其當前位置移動多遠。

現在,這是第二次點擊發生的情況。 這里的目標是使視圖與父母的基礎對齊。

target = parent.getBottom();
offset = target - child.getTop();
anim = ObjectAnimator.ofFloat(child, "translationY",offset);
anim.setDuration(250);
anim.start();
prev = child;

這就是一切的地方child.getTop()返回視圖原始位置的Y坐標。 不是當前位置。 因此,在動畫之后,將視圖放置在父級底部的下方。 我讀了一個不同的問題,該問題表明我應該改用child.getY() ,這應該使我獲得translationY位置加上top位置,但這並沒有帶來更好的結果。 我似乎無法正常工作。 我只是想將視圖從當前位置移動到屏幕底部,但這似乎很難完成。 任何幫助,將不勝感激。

編輯

我添加了一個動畫監聽器:

ObjectAnimator anim = ObjectAnimator.ofFloat(child,"translationY",offset);
anim.setDuration(250);
anim.addListener(new ObjectAnimator.AnimatorListener(){
    @Override
    public void onAnimationStart(Animator animation) {
        System.out.println("start: " + child.getTop() + " " + child.getY());
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        System.out.println("end: " + child.getTop() + " " + child.getY() + " " + child.getTranslationY());
        child.setTop((int)child.getY());
        System.out.println(child.getTop());
    }

    @Override
    public void onAnimationCancel(Animator animation) {}

    @Override
    public void onAnimationRepeat(Animator animation) {}
});
anim.start();

在這里,我設置偵聽器以嘗試更改視圖頂部的位置。 行為再次無法正常工作。 當我這樣做時,視圖實際上是在屏幕上方發送的。 System.out的輸出如下所示:

start: 2008 2008.0
end: 2008 478.0 -1530.0
478

因此,在動畫完成並設置新位置后調用child.getTop()返回一個正整數,但是視圖實際上並沒有完全顯示在屏幕上。 它在屏幕上方,部分可見。 視圖本身的高度約為700像素。 我仍然很困惑為什么這很難完成。

編輯2

我也嘗試過在onAnimationEnd方法中設置layoutparams:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)child.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.topMargin = (int)child.getY();
child.setLayoutParams(params);

結果: child.getTop()仍返回2008的原始位置。

您可以像這樣獲得屏幕底部的坐標:

float bottomOfScreen = getResources().getDisplayMetrics().heightPixels;

但您可能希望它減去LinearLayout的高度,否則LinearLayout會被底部截斷:

float bottomOfScreen = getResources().getDisplayMetrics().heightPixels
                    - child.getHeight();

 // if you want a little more space to the bottom
    // try something like - child.getHeight()*2; 

然后使用ViewPropertyAnimator對LL進行動畫處理,如下所示:

child.animate()
    .translationY(bottomOfScreen)
    .setInterpolator(new AccelerateDecelerateInterpolator())
    .setDuration(250);

插值器只是為了使動畫更加逼真。

如果child.getHeight()返回0,則系統尚未完成線性布局的設置,在這種情況下,您可能需要執行以下操作:

child.post(new Runnable() {
        @Override
        public void run() {
            float bottomOfScreen = getResources().getDisplayMetrics().heightPixels
                    - child.getHeight()*2;
            child.animate()
                    .translationY(bottomOfScreen)
                    .setInterpolator(new AccelerateDecelerateInterpolator())
                    .setDuration(250);
        }
    });

請記住,250毫秒的持續時間非常快,通常不會在屏幕上平移翻譯內容,因此您可能需要將其設置得更高一些,但這僅是口味問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM