gpt4 book ai didi

android - 如何拖动旋转的 DragShadow?

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:38 24 4
gpt4 key购买 nike

我像这样用 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 (angle==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);
...
}

关于android - 如何拖动旋转的 DragShadow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17049938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com