为什么dragShadowBuilder不显示imageview?

时间:2012-08-01 10:14:54

标签: android drag-and-drop imageview

我正在尝试使用dragShadowBuilder(View视图)构造函数发送给它ImageView没有与我开始拖延的那个相关联。 ImageView img = new ImageView(this); img.setbackgroundresource(R.drawable.icon); 为什么dragShadowBuilder在拖动过程中不会显示它?

1 个答案:

答案 0 :(得分:0)

我认为问题是ImageView在添加到布局之前不会被绘制。 我通过使用扩展的ShadowBuilder绘制一个Bitmap解决了这个问题:

public class LJDragShadowBuilder extends View.DragShadowBuilder {

   private Bitmap image; // image to draw as a drag shadow

   public LJDragShadowBuilder(Bitmap _image) {
      super();
      image = _image;
   }

   public void onDrawShadow(Canvas canvas)
   {
      canvas.drawBitmap(image, 0, 0, null);
   }

   public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint)
   {
      shadowSize.x = image.getWidth();
      shadowSize.y = image.getHeight();
      shadowTouchPoint.x = shadowSize.x / 2;
      shadowTouchPoint.y = shadowSize.y / 2;
   }
}