为了账号安全,请及时绑定邮箱和手机立即绑定

关于android实现拖动旋转角度,调整布局参数的思路

标签:
Android

我想做一个视图跟随我的手指和做一些旋转和缩放在多点触控用下面的代码

@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) {    final int action = motionEvent.getActionMasked();    int newPosX,newPosY;    switch (action) {        case MotionEvent.ACTION_DOWN: {            final int pointerIndex = motionEvent.getActionIndex();            final float x = motionEvent.getX( pointerIndex);            final float y = motionEvent.getY( pointerIndex);

            RelativeLayout.LayoutParams parms = (RelativeLayout.LayoutParams) view.getLayoutParams();            // Remember where we started (for dragging)
            mLastTouchX = (int) x;
            mLastTouchY = (int) y;            // Save the ID of this pointer (for dragging)
            mActivePointerId = motionEvent.getPointerId( 0);            break;
        }        case MotionEvent.ACTION_MOVE: {            if(motionEvent.getPointerCount()==2){                float newDist = spacing(motionEvent);                float scale = newDist / oldDist * view.getScaleX();
                view.setScaleY(scale);
                view.setScaleX(scale);                float newAngle = rotation(motionEvent);                float a = newAngle - oldAngle;
                view.animate().rotationBy(a).setDuration(0).setInterpolator(new LinearInterpolator()).start();
            }            // Find the index of the active pointer and fetch its position
            final int pointerIndex =
                    motionEvent.findPointerIndex( mActivePointerId);            final float x = motionEvent.getX( pointerIndex);            final float y = motionEvent.getY( pointerIndex);            // Calculate the distance moved
            final float dx = x - mLastTouchX;            final float dy = y - mLastTouchY;
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();

            layoutParams.leftMargin += dx;
            layoutParams.topMargin += dy;

            view.setLayoutParams(layoutParams);            break;
        }        case MotionEvent.ACTION_POINTER_DOWN:{
            oldDist = spacing(motionEvent);
            oldAngle = rotation(motionEvent);            break;
        }        case MotionEvent.ACTION_UP: {
            mActivePointerId = INVALID_POINTER_ID;            break;
        }        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;            break;
        }        case MotionEvent.ACTION_POINTER_UP: {            final int pointerIndex = motionEvent.getActionIndex();            final int pointerId = motionEvent.getPointerId( pointerIndex);            if (pointerId == mActivePointerId) {                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mLastTouchX = (int) motionEvent.getX( newPointerIndex);
                mLastTouchY = (int) motionEvent.getY( newPointerIndex);
                mActivePointerId = motionEvent.getPointerId( newPointerIndex);
            }            break;
        }
    }    return true;

}    private float spacing(MotionEvent event) {    float x = event.getX(0) - event.getX(1);    float y = event.getY(0) - event.getY(1);    return (float) Math.sqrt(x * x + y * y);
}private float rotation(MotionEvent event) {    double delta_x = (event.getX(0) - event.getX(1));    double delta_y = (event.getY(0) - event.getY(1));    double radians = Math.atan2(delta_y, delta_x);    return (float) Math.toDegrees(radians);
}

一切都顺利直到测试到视图旋转。当它旋转90度以上的时候,我们试着将它拖它跳跃的触摸点。我认为这件事与

LayoutParams。左边界+=DXLayoutParams。左边空白+=Dysetlayoutparams(LayoutParams)
这段代码有关,我测试了两天都没有成功!

注:我想实现的是使视图拖动旋转和2指量表(拖单指也) 我代码来自谷歌的跟随拖动文件使它不跳时,开关的手指。 我使用这个旋转 视图。animate() rotationby(一)。。setDuration(0)。setinterpolator(新linearinterpolator())

因为当我使用视图。setrotate(),视图是振动。

方法1:我删除了

lay

outParams.leftMargin += dx;
layoutParams.topMargin += dy;

setLayoutParams(layoutParams);

这段代码……

然后取而代之的是:

layoutParams.leftMargin += dx;
layoutParams.topMargin += dy;

这段代码

现在即使旋转视图不走动。但当我切换活动手指跳跃从实际位置。

在action_pointer_up我做这个转变的活动手指

//To get the moved distance of the finger(X and Y)float diffX = motionEvent.getX(pointerIndex) - mLastTouchX;                    float diffY = motionEvent.getY(pointerIndex) - mLastTouchY;//to get the distance from touch point and the top or left of the view
                    final float dx = motionEvent.getRawX() - (motionEvent.getRawX()-motionEvent.getX());                    final float dy = motionEvent.getRawY() - (motionEvent.getRawY()-motionEvent.getY());
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();//Settings appropriate value for the margin top and margin left.

                    layoutParams.leftMargin = (int) ((( motionEvent.getRawX() )-dx )+ diffX );
                    layoutParams.topMargin = (int) ((( motionEvent.getRawY() )-dy )+ diffY );
case MotionEvent.ACTION_POINTER_UP: {                final int pointerIndex = motionEvent.getActionIndex();                final int pointerId = motionEvent.getPointerId( pointerIndex);                if (pointerId == mActivePointerId) {                    // This was our active pointer going up. Choose a new
                    // active pointer and adjust accordingly.
                    final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                    Log.d(TAG,+newPointerIndex);
                    mLastTouchX = (int) motionEvent.getX( newPointerIndex);
                    mLastTouchY = (int) motionEvent.getY( newPointerIndex);
                    mActivePointerId = motionEvent.getPointerId( newPointerIndex);
                }                break;
            }

原文链接:http://www.apkbus.com/blog-919651-76517.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消