动画移动后,Android视图中单击边界 [英] Moving Android View click boundaries after animation

查看:117
本文介绍了动画移动后,Android视图中单击边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RelativeLayout的两种观点,两者都填满屏幕,所以认为B是对视图A.顶我也有定义的动画可以移动视图B部分离屏显示视图的下方。动画工作正常,但我有没有与视图移动视图界限的经典问题,所以,我用它来触发动画(这是位于视图B)按钮是从原来的位置只是点击,没有无论身在何处视图B位于。说我遇到的问题是,动画结束后,当我设置的布局PARAMS它造成视图B再次被重新绘制,从动画的结束位置转换。

作为具体例,图B的左边缘最初是在x = 0时,在x = 450的一个按钮时该按钮是pressed,动画移动视图为x = -400。这个工作正常 - 视图是部分关闭屏幕的左手侧,而现在的按钮是在x = 50,所以它仍然是在屏幕上。按钮的点击区域,虽然仍然是在x = 450。所以,现在我设置视图B布局PARAMS:

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

一旦新PARAMS设置,视图得到填充400像素右侧,移动到X = -800整个视图。按钮的点击区域现已正常在x = 50了,所以好像我可以把它看的权利或采取行动的权利。任何想法,我做错了什么?这里的动画是如何设置的。

 动画动画= NULL;
阿尼姆=新TranslateAnimation(0,-400,0,0);
anim.setAnimationListener(本);
anim.setDuration(持续时间);
viewB.startAnimation(阿尼姆);


解决方案

我能够把事情前或动画后更改布局PARAMS工作,酌情:

 私人诠释marginOffsets;公共无效triggerAnimation(布尔显示,偏移)
{
    INT curX = 0;
    INT下一页末= 0;
    动画动画= NULL;    this.showingPanel =显示;
    如果(显示)
    {
        curX = 0 - 偏移;        android.widget.RelativeLayout.LayoutParams LP =新android.widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
        lp.rightMargin = 0;
        rootPanel.setLayoutParams(LP);
    }
    其他
    {
        下一页末= 0 - 偏移;
    }    marginOffsets =下一页末< 0? 0 - 偏移:偏移;    阿尼姆=新TranslateAnimation(curX,下一页末,0,0);
    anim.setAnimationListener(本);
    anim.setDuration(持续时间);
    startAnimation(阿尼姆);
}公共无效onAnimationEnd(动画阿尼姆)
{
    //这个$ P视图时屏幕上移动$ pvents闪烁。
    clearAnimation();    如果(!showingPanel)
    {
        //移动保证金移动的实际边界所以单击事件仍然工作。
        RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
        lp.rightMargin = 0 - marginOffsets;
        rootPanel.setLayoutParams(LP);
    }
}

I have two views in a RelativeLayout, both of which fill the screen, so view B is on top of view A. I also have an animation defined which can move view B partially offscreen to show view A underneath. The animation works fine, but I'm having the classic issue of the view bounds not moving with the view, so the button that I use to trigger the animation (which is located on view B) is only clickable from its original position, no matter where view B is located. The issue that I'm having is that after the animation ends, when I set the layout params it's causing view B to be redrawn again, translated from the location of the end of the animation.

As a concrete example, the left edge of view B is initially at x = 0, with a button at x = 450. When the button is pressed, an animation moves the view to x = -400. This works properly - the view is partially off the left hand side of the screen, and the button is now at x = 50, so it is still on screen. The click area for the button though is still at x = 450. So now I set the layout params on view B:

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

Once the new params are set, the view gets 400px of padding on the right, moving the entire view to x = -800. The clickable area for the button is now properly at x = 50 though, so it seems like I can have it look right or act right. Any idea what I'm doing wrong? Here's how the animation is set up.

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

解决方案

I was able to get things working by changing the layout params before or after the animation, as appropriate:

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);
    }    
}

这篇关于动画移动后,Android视图中单击边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆