1

我目前正在使用九个旧 Android 来为 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 是相对布局。箭头是一个图像视图。groupsButton 是一个带有文本和图像按钮的 LinearLayout。groupsButton 应该是可点击的;这就是扩展并且应该折叠它。

提前致谢。

4

1 回答 1

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;         
    }
};
于 2012-10-23T17:53:41.687 回答