Android - 动画后的幽灵视图

时间:2013-09-26 05:20:21

标签: java android android-layout animation android-animation

在我的短信应用中,当用户点击附加图片进行展开时,我会打开一个对话框。

在我决定添加动画之前,一切正常。此对话框有3个按钮(一个在右上角关闭ImageButton,在对话框底部栏中关闭两个按钮)。当用户点击图像时,按钮会激活/进入。这一切都很好,除非按钮仍然可以点击,即使它们被隐藏了。

动画之前: Before

动画制作后: After

下面对话框的代码:

// Declare these here to increase scope. (Listeners)
private static boolean ContainerChanged;
private static boolean ActionsHidden;
private static boolean AnimStarted;
private static boolean closeAnimFinished;
private static boolean barAnimFinished;

private void showExpandedImageDialog(Uri imgUri)
{
    // Initialize Dialog
    final Dialog dialog = new Dialog(ActivitySend.this, R.style.FullHeightDialog);
    dialog.setContentView(R.layout.dialog_attachment_image_send);

    // Initialize Views
    final RelativeLayout Container = (RelativeLayout) dialog.findViewById(R.id.Container);
    final LinearLayout actions = (LinearLayout) dialog.findViewById(R.id.Actions);
    final ImageButton btnClose = (ImageButton) dialog.findViewById(R.id.btnClose);
    Button btnReplace = (Button) dialog.findViewById(R.id.btnReplace);
    Button btnRemove = (Button) dialog.findViewById(R.id.btnRemove);
    ImageView image = (ImageView) dialog.findViewById(R.id.Image);

    // Load Image & Make Zoomable
    PhotoViewAttacher mAttacher = new PhotoViewAttacher(image);
    image.setImageURI(imgUri);
    mAttacher.update();

    // Get animations ready
    final Animation fiCloseAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.fade_in);
    fiCloseAnim.setFillEnabled(true);
    fiCloseAnim.setFillAfter(true);
    final Animation foCloseAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.fade_out);
    foCloseAnim.setFillEnabled(true);
    foCloseAnim.setFillAfter(true);
    final Animation dBarAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.slide_down);
    dBarAnim.setFillEnabled(true);
    dBarAnim.setFillAfter(true);
    final Animation uBarAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.slide_up);
    uBarAnim.setFillEnabled(true);
    uBarAnim.setFillAfter(true);

    // Reset static variables
    ActionsHidden = false;
    AnimStarted = false;
    closeAnimFinished = false;
    barAnimFinished = false;

    // Initialize listeners
    AnimationListener closeAnimListener = new AnimationListener()
    {
        @Override
        public void onAnimationEnd(Animation animation)
        {
            closeAnimFinished = true;
            if (closeAnimFinished && barAnimFinished)
            {
                AnimStarted = false;
                closeAnimFinished = false;
                barAnimFinished = false;

                if (ActionsHidden)
                {
                    actions.setVisibility(View.VISIBLE);
                    btnClose.setVisibility(View.VISIBLE);
                }
                else
                {
                    actions.setVisibility(View.GONE);
                    btnClose.setVisibility(View.GONE);
                }
                ActionsHidden = !ActionsHidden;
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation)
        {
            // Nothing
        }

        @Override
        public void onAnimationStart(Animation animation)
        {
            AnimStarted = true;
        }
    };
    AnimationListener barAnimListener = new AnimationListener()
    {
        @Override
        public void onAnimationEnd(Animation animation)
        {
            barAnimFinished = true;
            if (closeAnimFinished && barAnimFinished)
            {
                AnimStarted = false;
                closeAnimFinished = false;
                barAnimFinished = false;

                if (ActionsHidden)
                {
                    actions.setVisibility(View.VISIBLE);
                    btnClose.setVisibility(View.VISIBLE);
                }
                else
                {
                    actions.setVisibility(View.GONE);
                    btnClose.setVisibility(View.GONE);
                }
                ActionsHidden = !ActionsHidden;
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation)
        {
            // Nothing
        }

        @Override
        public void onAnimationStart(Animation animation)
        {
            AnimStarted = true;
        }
    };

    // Set listeners
    fiCloseAnim.setAnimationListener(closeAnimListener);
    foCloseAnim.setAnimationListener(closeAnimListener);
    dBarAnim.setAnimationListener(barAnimListener);
    uBarAnim.setAnimationListener(barAnimListener);

    // Actions Appear/Disappear onTap (Animate)
    mAttacher.setOnPhotoTapListener(new OnPhotoTapListener()
    {
        @Override
        public void onPhotoTap(View view, float x, float y)
        {
            if (!AnimStarted && ActionsHidden)
            {
                actions.startAnimation(uBarAnim);
                btnClose.startAnimation(fiCloseAnim);
            }
            else if (!AnimStarted)
            {
                actions.startAnimation(dBarAnim);
                btnClose.startAnimation(foCloseAnim);
            }
        }
    });

    // Make dialog square
    ContainerChanged = false;
    ViewTreeObserver vto = Container.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
    {
        public boolean onPreDraw()
        {
            // Set boolean to save processing power
            if (!ContainerChanged)
            {
                int width = Container.getMeasuredWidth();
                Container.getLayoutParams().height = width;
                ContainerChanged = true;
            }

            return true;
        }
    });

    // Set button listeners
    btnClose.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            dialog.dismiss();
        }
    });
    btnReplace.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
        }
    });
    btnRemove.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
        }
    });

    // Show dialog
    dialog.show();
}

如果我删除动画并且只使用view.setVisibility(View.GONE),那么一切都很完美但我真的很喜欢这些动画......

我可以采取哪些措施来解决这个问题? 我可以使用view.setClickable(false);,但我确信有更好的解决方案。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

让您的活动实现AnimationListener。然后会有一个重写的方法public void onAnimationEnd(Animation arg0),在此方法中为所有三个按钮编写setClickable(false)setEnabled(false),在您开始动画时再次启用它们....希望您得到了我..