在android中移动并缩放自定义视图

时间:2014-06-26 12:37:58

标签: android animation scale move ontouchlistener

我想为我的Android应用程序创建自定义菜单,例如iPhone https://github.com/XavierDK/XDKAirMenu。我的应用程序在屏幕左侧显示菜单,在屏幕右侧显示其内容。

这是屏幕右侧的自定义布局(内容视图)

public boolean onTouchEvent(MotionEvent event) {
                gestureDetector.onTouchEvent(event);

                if (isTranformed) {
                    final int X = (int) event.getRawX();
                    final int Y = (int) event.getRawY();
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) this .getLayoutParams();
                        if (layoutParams.leftMargin == 0 && layoutParams.topMargin == 0) {
                            //requestLayout();
                            isTranformed = false;
                            isScrolling = false;
                            break;
                        }
                        isScrolling = true;
                        int xDiff = layoutParams.leftMargin - (X - _xDelta);
                        layoutParams.leftMargin = X - _xDelta;
                        int scaleFactor =  layoutParams.leftMargin > 0 ? layoutParams.leftMargin : 1;
                        layoutParams.topMargin = layoutParams.topMargin - ((layoutParams.topMargin / scaleFactor) * xDiff);

                        if (layoutParams.leftMargin < 0) {
                            layoutParams.leftMargin = 0;
                        }
                        if (layoutParams.topMargin < 0) {
                            layoutParams.topMargin = 0;
                        }
                        layoutParams.width = (displayMetrics.widthPixels - layoutParams.leftMargin);
                        layoutParams.height = (displayMetrics.heightPixels - (layoutParams.topMargin * 2));
                        this.setLayoutParams(layoutParams);
                        //invalidate();
                        //setLeft(layoutParams.leftMargin);
                        //setTop(layoutParams.topMargin);
        /*              this.setX(layoutParams.leftMargin);
                        this.setY(layoutParams.topMargin);*/
                        this.requestLayout();

                        break;

                    case MotionEvent.ACTION_UP:
                        if (isScrolling) {
                            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) CustomSlidingLayout.this
                                    .getLayoutParams();

                            if (params.leftMargin < displayMetrics.widthPixels / 2) {

                                if (!isAnimating) {
                                    isAnimating = true;
                                    ResizeMoveAnimation anim = new ResizeMoveAnimation(
                                            this, 0, 0, displayMetrics.widthPixels,
                                            displayMetrics.heightPixels);
                                    anim.setAnimationListener(animationListener);
                                    startAnimation(anim);
                                }
                            } else {
                                if (!isAnimating) {
                                    isAnimating = true;
                                    Rect rect = new Rect();
                                    getLocalVisibleRect(rect);
                                    ResizeMoveAnimation anim = new ResizeMoveAnimation(
                                            this, (int) (displayMetrics.widthPixels * 0.8),
                                            displayMetrics.heightPixels / 4,
                                            displayMetrics.widthPixels * 2,
                                            (int) (displayMetrics.heightPixels * 0.75));
                                    // anim.setAnimationListener(animationListener);
                                    startAnimation(anim);
                                    isAnimating = false;
                                }
                            }
                            isScrolling = false;
                        }

                        break;

                    case MotionEvent.ACTION_DOWN:
                        FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) this
                                .getLayoutParams();
                        _xDelta = X - lParams.leftMargin;
                        break;
                    }
                    return true;
                }
                return false;
            }
        }

右侧视图(内容视图)在单击菜单按钮时调整为小尺寸。当触摸调整大小的视图时,它会调整为原始大小。此外,我想缩放并从右侧移动视图 - 保持联系。上面的代码片段有一些视图位置问题。它没有正确更新视图布局/位置。我该如何修复它?

1 个答案:

答案 0 :(得分:0)

使用此代码实现缩放和转换animation

    float scalefactor = 0.5;
    float translateX = 500; // Translate amount
    view.animate().scaleX(scalefactor).translationXBy(translateX).start();

for reverse animation

    view.animate().scaleX(1).translationXBy(-translateX).start();