0

在我的 SMS 应用程序中,当用户单击附加图像以展开它时,我会打开一个对话框。

一切正常,直到我决定添加动画。此对话框有 3 个按钮(右上角的一个关闭 ImageButton 和对话框底部栏中的两个按钮)。当用户点击图像时,按钮动画出/入。这一切都很好,除了按钮即使在隐藏后仍然可以点击。

动画之前: 前

动画后: 后

我的以下对话框代码:

// 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);,但我确信有比这更好的解决方案。

4

1 回答 1

0

你可以这样做:

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

于 2013-09-26T05:41:50.190 回答