【问题标题】:Moving Android View click boundaries after animation动画后移动Android View点击边界
【发布时间】:2012-12-14 04:38:28
【问题描述】:

我在 RelativeLayout 中有两个视图,它们都填满了屏幕,因此视图 B 位于视图 A 的顶部。我还定义了一个动画,它可以将视图 B 部分移出屏幕以在下面显示视图 A。动画效果很好,但是我遇到了视图边界不随视图移动的经典问题,因此我用来触发动画的按钮(位于视图 B 上)只能从其原始位置单击,不无论视图 B 位于何处。我遇到的问题是动画结束后,当我设置布局参数时,它会导致视图 B 再次重绘,从动画结束的位置转换。

作为一个具体示例,视图 B 的左边缘最初位于 x = 0,按钮位于 x = 450。当按下按钮时,动画将视图移动到 x = -400。这可以正常工作 - 视图部分位于屏幕左侧,并且按钮现在位于 x = 50,因此它仍在屏幕上。虽然按钮的点击区域仍然在 x = 450。所以现在我在视图 B 上设置布局参数:

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) viewB.getLayoutParams();
lp.rightMargin = 400;
viewB.setLayoutParams(lp);

设置新参数后,视图在右侧获得 400 像素的填充,将整个视图移动到 x = -800。按钮的可点击区域现在正确地位于 x = 50,所以看起来我可以让它看起来正确或行为正确。知道我做错了什么吗?以下是动画的设置方式。

Animation anim = null;
anim = new TranslateAnimation(0, -400, 0, 0);
anim.setAnimationListener(this);
anim.setDuration(duration);
viewB.startAnimation(anim); 

【问题讨论】:

    标签: android user-interface


    【解决方案1】:

    我可以通过在动画之前或之后更改布局参数来使事情正常进行:

    private int marginOffsets;
    
    public void triggerAnimation(boolean show, offset)
    {
        int curX = 0;
        int newX = 0;
        Animation anim = null;
    
        this.showingPanel = show;
        if(show)
        {
            curX = 0 - offset;
    
            android.widget.RelativeLayout.LayoutParams lp = new android.widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,  ViewGroup.LayoutParams.FILL_PARENT);
            lp.rightMargin = 0;
            rootPanel.setLayoutParams(lp);
        }
        else
        {
            newX = 0 - offset;
        }
    
        marginOffsets = newX < 0 ? 0 - offset : offset;
    
        anim = new TranslateAnimation(curX, newX, 0, 0);
        anim.setAnimationListener(this);
        anim.setDuration(duration);
        startAnimation(anim);        
    }
    
    public void onAnimationEnd(Animation anim)
    {
        //This prevents flicker when the view is moving onscreen.
        clearAnimation();
    
        if(!showingPanel)
        {
            //Move the margin to move the actual bounds so click events still work.
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,  ViewGroup.LayoutParams.FILL_PARENT);
            lp.rightMargin = 0 - marginOffsets;
            rootPanel.setLayoutParams(lp);
        }    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多