2014-01-07 3 views
3

Может кто-нибудь, пожалуйста, помогите мне в следующем:
1. Он не должен касаться точки касания, он должен начинать перетаскивание из любой точки.
2. Вид DragShadowBuilder должен оживлять назад, пока его не отбросить на правильную цель.Android DragShadowBuilder придерживается центра точки касания

+0

Облицовка одной и той же проблемы. Сообщите нам, если у вас есть решение по этому вопросу. – AndroidHacker

ответ

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()); 
    } 
} 

Для преобразования вид на Bitmap взяты из here:

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.

Комментарии в коде самообслуживания, для получения дополнительной информации дайте мне знать. Надеюсь, это поможет.

Смежные вопросы