Android拖拽影像生成器固定在触控点中心。

3

请问有人可以帮我解决以下问题吗:
1. 它不应该粘在触摸点上,而应该从任意点开始拖动。
2. 当DragShadowBuilder视图没有放置在正确的目标上时,它应该自动返回并执行动画。


遇到了同样的问题。如果您有任何解决方案,请告诉我们。 - AndroidHacker
1个回答

3
我通过创建一个自定义的 View.DragShadowBuilder 类来实现这一点。
代码如下:
public class CustomDragShadowBuilder extends View.DragShadowBuilder {
View v;
public CustomDragShadowBuilder(View v) {
    super(v);
    this.v=v;
}
@Override
public void onDrawShadow(Canvas canvas) {
    super.onDrawShadow(canvas);

    /*Modify canvas if you want to show some custom view that you want
      to animate, that you can check by putting a condition passed over
      constructor. Here I'm taking the same view*/ 

    canvas.drawBitmap(getBitmapFromView(v), 0, 0, null);
}
@Override
public void onProvideShadowMetrics(Point shadowSize, Point touchPoint) {
/*Modify x,y of shadowSize to change the shadow view 
   according to your requirements. Here I'm taking the same view width and height*/ 
    shadowSize.set(v.getWidth(),v.getHeight());
/*Modify x,y of touchPoint to change the touch for the view 
 as per your needs. You may pass your x,y position of finger 
 to achieve your results. Here I'm taking the lower end point of view*/
    touchPoint.set(v.getWidth(), v.getHeight());
  }
}

将从这里获取的视图转换为位图

private Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    else 
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}

虽然这是一个老问题,但对于想要使用自定义DragShadowBuilder的其他人可能会有所帮助。
代码中的注释很容易理解,如果需要更多信息,请让我知道。 希望能帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接