如何定位视图以使点击在动画后起作用

时间:2012-10-23 01:33:57

标签: android android-animation onclicklistener

我目前正在使用Nine Old Androids来设置RelativeLayout的动画,就像滑块菜单一样,它从顶部向下滑动。我试过很多方面,有人可以提出建议吗?

这是动画代码:

  private OnClickListener slideClick = new OnClickListener(){
    @Override
    public void onClick(View v) {
        slideGroups.bringToFront();

        if(!mPulledDown) {
            ObjectAnimator oa = ObjectAnimator.ofFloat(slideGroups, "translationY", slideGroups.getHeight()-80);
            oa.addListener(new AnimatorListener() {
                @Override
                public void onAnimationStart(Animator arg0) {}

                @Override
                public void onAnimationRepeat(Animator arg0) {}

                @Override
                public void onAnimationEnd(Animator arg0) {
                    arrow.setImageResource(R.drawable.arrowup);
                    if(Application.getAndroidAPILevel() < 11) {
                        // only if pre 3.0 (api level 11)
                        Toast.makeText(FriendsActivity.this, String.format("api level %s", 
                                Application.getAndroidAPILevel()), Toast.LENGTH_SHORT).show();
                        //RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) slideGroups.getLayoutParams();

                        // clear animation to prevent flicker
                        slideGroups.clearAnimation();

                        // set new "real" position of wrapper
                        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, slideGroups.getWidth());
                        //lp.addRule(RelativeLayout.BELOW, R.id.branchFinderIncludeHeader);
                        slideGroups.setLayoutParams(lp);

                    }
                }

                @Override
                public void onAnimationCancel(Animator arg0) {}
            });
            oa.start();
        }
        else 
        {
            ObjectAnimator oa = ObjectAnimator.ofFloat(slideGroups, "translationY", 0);
            oa.addListener(new AnimatorListener() {
                @Override
                public void onAnimationStart(Animator arg0) {}

                @Override
                public void onAnimationRepeat(Animator arg0) {}

                @Override
                public void onAnimationEnd(Animator arg0) {
                    arrow.setImageResource(R.drawable.arrowdown);
                    if(Application.getAndroidAPILevel() < 11) {
                        // only if pre 3.0 (api level 11)
                        Toast.makeText(FriendsActivity.this, String.format("api level %s", 
                                Application.getAndroidAPILevel()), Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onAnimationCancel(Animator arg0) {}
            });
            oa.start();
        }

        mPulledDown = !mPulledDown;         
    }
};

我只需要使用sdk 4级和更高版本的东西。目前,触摸/点击会在展开时将该事件传递到滑块下方的列表。如果需要任何其他信息,请告诉我。 slideGroups是RelativeLayout。箭头是一个imageview。 groupsButton是一个带文本和图像按钮的LinearLayout。 groupsButton应该是可点击的;这就是扩展并且应该崩溃的东西。

提前致谢。

1 个答案:

答案 0 :(得分:2)

有些人通过使用解决方案编辑我的问题做出了贡献。我想给予他们信任,但不能赞成编辑。对后人来说,这是适用于我的解决方案。

int originalMarginLeft = 0;
int originalMarginTop = 0;
int originalMarginRight = 0;
int originalMarginBottom = 0;
int originalLeft = 0;
int originalTop = 0;
int originalRight = 0;
int originalBottom = 0;
int originalX = 0;
int originalY = 0;
int originalHeight = 0;

private OnClickListener slideClick = new OnClickListener(){
    @Override
    public void onClick(View v) {
        slideGroups.bringToFront();

        if(originalMarginLeft == 0) {
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) slideGroups.getLayoutParams();
            originalMarginLeft = params.leftMargin;
            originalMarginTop = params.topMargin;
            originalMarginRight = params.rightMargin;
            originalMarginBottom = params.bottomMargin;
            originalLeft = slideGroups.getLeft();
            originalTop = slideGroups.getTop();
            originalRight = slideGroups.getRight();
            originalBottom = slideGroups.getBottom();
            originalHeight = params.height;
        }

        if(!mPulledDown) {
            ObjectAnimator oa = ObjectAnimator.ofFloat(slideGroups, "translationY", originalHeight);
            oa.addListener(new AnimatorListener() {
                @Override
                public void onAnimationStart(Animator arg0) {}

                @Override
                public void onAnimationRepeat(Animator arg0) {}

                @Override
                public void onAnimationEnd(Animator arg0) {
                    arrow.setImageResource(R.drawable.arrowup);
                    if(Application.getAndroidAPILevel() < 11) {

                        slideGroups.clearAnimation();
                        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(slideGroups.getWidth(), slideGroups.getHeight());
                        lp.leftMargin = originalMarginLeft;
                        lp.rightMargin = originalMarginRight;
                        slideGroups.setLayoutParams(lp);

                    }
                }

                @Override
                public void onAnimationCancel(Animator arg0) {}
            });
            oa.start();
        }
        else 
        {
            ObjectAnimator oa = ObjectAnimator.ofFloat(slideGroups, "translationY", -originalHeight+60);
            oa.addListener(new AnimatorListener() {
                @Override
                public void onAnimationStart(Animator arg0) {}

                @Override
                public void onAnimationRepeat(Animator arg0) {}

                @Override
                public void onAnimationEnd(Animator arg0) {
                    arrow.setImageResource(R.drawable.arrowdown);
                    if(Application.getAndroidAPILevel() < 11) {

                        slideGroups.clearAnimation();
                        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(slideGroups.getWidth(), slideGroups.getHeight());
                        lp.leftMargin = originalMarginLeft;
                        lp.rightMargin = originalMarginRight;
                        lp.topMargin = originalMarginTop;
                        lp.bottomMargin = originalMarginBottom;
                        slideGroups.setLayoutParams(lp);
                    }
                }

                @Override
                public void onAnimationCancel(Animator arg0) {}
            });
            oa.start();
        }

        mPulledDown = !mPulledDown;         
    }
};