【问题标题】:Move and scale custom view in android在android中移动和缩放自定义视图
【发布时间】:2014-06-26 12:37:58
【问题描述】:

我想为我的 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;
            }
        }

单击菜单按钮时右侧的视图(内容视图)调整为小尺寸。触摸调整后的视图时,它会调整为原始大小。另外,我想缩放并从右侧移动视图-离开触摸。上面的代码 sn -p 有一些视图位置问题。它没有正确更新视图布局/位置。我该如何解决?

【问题讨论】:

    标签: android animation scale move ontouchlistener


    【解决方案1】:

    使用此代码实现缩放和过渡animation

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

    用于反向animation

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

    【讨论】:

    • 我想在拖动时移动和调整大小(将缩放(小)视图设为原始大小)。参考 NDragListener。但 ist 对我不起作用。
    • 在这种情况下,您必须在 onDragListner 中更改 view.setscalex()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 2016-10-01
    • 2023-04-08
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多