当前位置 : 主页 > 手机开发 > android >

android – 如何拖动旋转的DragShadow?

来源:互联网 收集:自由互联 发布时间:2021-06-11
我用ObjectAnimator旋转了TextView,如下所示: ObjectAnimator rotate = ObjectAnimator.ofFloat(aRotatedTextView, "rotation", 0f, someAngle); 现在,我尝试在长按上拖放旋转的(!)TextView,如下所示: public boolean onLon
我用ObjectAnimator旋转了TextView,如下所示:

ObjectAnimator rotate = ObjectAnimator.ofFloat(aRotatedTextView, "rotation", 0f, someAngle);

现在,我尝试在长按上拖放旋转的(!)TextView,如下所示:

public boolean onLongClick(View view) {
    ClipData data = ClipData.newPlainText("DragData", (String) view.getTag());
    DragShadowBuilder dragShadowBuilder = new View.DragShadowBuilder(view);
    mDragInProgress = view.startDrag(data, dragShadowBuilder, view, 0);
    view.setVisibility(View.INVISIBLE);
    return true;
}

会发生什么是在没有旋转的情况下创建DragShadow(角度== 0).我已经看到了很多位图(但没有旋转)的例子,我无法工作.
有没有办法创建旋转的DragShadow?

需要同样的事情.像这样实现它(包括缩放)

new OnTouchListener() {
    @Override
    public boolean onTouch(final View view, MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
    double rotationRad = Math.toRadians(view.getRotation());
    final int w = (int) (view.getWidth() * view.getScaleX());
    final int h = (int) (view.getHeight() * view.getScaleY());
    double s = Math.abs(Math.sin(rotationRad));
    double c = Math.abs(Math.cos(rotationRad));
    final int width = (int) (w * c + h * s);
    final int height = (int) (w * s + h * c);
    DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view) {
        @Override
        public void onDrawShadow(Canvas canvas) {
            canvas.scale(view.getScaleX(), view.getScaleY(), width / 2,
                    height / 2);
            canvas.rotate(view.getRotation(), width / 2, height / 2);
            canvas.translate((width - view.getWidth()) / 2,
                    (height - view.getHeight()) / 2);
            super.onDrawShadow(canvas);
        }

        @Override
        public void onProvideShadowMetrics(Point shadowSize,
                Point shadowTouchPoint) {
            shadowSize.set(width, height);
            shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
        }
    };

    view.startDrag(null, shadowBuilder, view, 0);
    ...
}
网友评论