簡體   English   中英

Android:滾動列表時如何防止按下按鈕的動畫?

[英]Android: How to Prevent Down Press animation of a button when scrolling a list?

所以我有一個ImageView我想用作按鈕。 當我按下此按鈕時,它會縮小一點,而當我松開它時,該按鈕會縮小到原始大小。

為此,我將onTouchListener附加到圖像:

@Override
public boolean onTouch(View v, MotionEvent event) {
    Animation anim;
    int action = event.getAction();

    switch (action){
        case MotionEvent.ACTION_DOWN:
            // Scale down ImageView when finger is down
            anim = AnimationUtils.loadAnimation(getContext(), R.anim.poster_scale_down);
            v.startAnimation(anim);
            return true;

        case MotionEvent.ACTION_UP:
            // Scale up ImageView when finger is up
            anim = AnimationUtils.loadAnimation(getContext(), R.anim.poster_scale_normal);
            v.startAnimation(anim);
            break;

        case MotionEvent.ACTION_CANCEL:
             // Scale up ImageView when action is canceled
            anim = AnimationUtils.loadAnimation(getContext(), R.anim.poster_scale_normal);
            v.startAnimation(anim);
            break;
    }
    return false;
}

我在RecyclerView有一系列這些ImageViews 因此,當我滾動時,它將觸發我手指所處位置的ACTION_DOWN按下動畫。 真煩人!

我該如何預防? 是否有更好的方法將動畫添加為ImageView的選擇器?

用替代方法進行編輯:

case MotionEvent.ACTION_CANCEL:
            v.clearAnimation();
            break;

滾動時,它將取消您在列表項上的當前觸摸,因此只需清除動畫即可。 這不是完美的,因為如果您滑動的速度不夠快,它仍然會導致動畫僅稍微開始

只需使用變量自己維護狀態。 例如,第一次按下時,將變量設置為true,如果變量為true,則不執行動畫。 釋放后,將變量設置為false。

暫無
暫無

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

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