关于DragShadowBuilder无法显示阴影问题探究

DragShadowBuilder是新版安卓拖放时的一个辅助类,是在手势拖动的时候动态显示拖动的阴影,这个阴影可以放置图片,也可以自己用画布画


构造方法

View.DragShadowBuilder(View):

这个构造器接收任意的应用程序的View对象。这个构造器把View对象保存在View.DragShadowBuilder对象中,以便在回调期间访问这个View对象,来构造拖拽影子。它(View对象参数)不必跟用户选择的开始拖拽操作的View对象相关联。

如果使用这个构造器,就不必扩展View.DragShadowBuilder类或覆写它的方法。默认情况,你会获得一个跟传递给构造器的View对象外观相同的拖拽影子。在用户的触屏位置下方,以出点为中心显示。

View.DragShadowBuilder():

如果使用这个构造器,在ViewDragShadowBuilder对象中没有有效的View对象。默认情况下,如果使用这个构造器,并且没有扩展View.DragShadowBuilder类或覆写它的方法,那么就会获得一个不可见的拖拽影子,系统不会给出错误。


相关的翻译已经有很多,但是不知道为什么没有去实验一下,反正我的实验结果是单纯的用View.DragShadowBuilder(View)是无法显示阴影的,当然我传入的是imageview,然后我就去stackoverflow上搜索,果然也有人有相同问题

链接http://stackoverflow.com/questions/11757115/why-dragshadowbuilder-doesnt-show-imageview


下面的有一种猜测:ImageView is not being drawn until it is added to a layout

所以解决方案为重写DragShadowBuilder传入一个位图,自己画

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;
   }
}
这个方法我证实过可行,同时研究android给的例子可以看到,它也是继承一个自己画的,跟传进来的view没半毛钱关系,只是super给父类了。。。

private static class MyDragShadowBuilder extends View.DragShadowBuilder {

    // The drag shadow image, defined as a drawable thing
    private static Drawable shadow;

        // Defines the constructor for myDragShadowBuilder
        public MyDragShadowBuilder(View v) {

            // Stores the View parameter passed to myDragShadowBuilder.
            super(v);

            // Creates a draggable image that will fill the Canvas provided by the system.
            shadow = new ColorDrawable(Color.LTGRAY);
        }

        // Defines a callback that sends the drag shadow dimensions and touch point back to the
        // system.
        @Override
        public void onProvideShadowMetrics (Point size, Point touch)
            // Defines local variables
            private int width, height;

            // Sets the width of the shadow to half the width of the original View
            width = getView().getWidth() / 2;

            // Sets the height of the shadow to half the height of the original View
            height = getView().getHeight() / 2;

            // The drag shadow is a ColorDrawable. This sets its dimensions to be the same as the
            // Canvas that the system will provide. As a result, the drag shadow will fill the
            // Canvas.
            shadow.setBounds(0, 0, width, height);

            // Sets the size parameter's width and height values. These get back to the system
            // through the size parameter.
            size.set(width, height);

            // Sets the touch point's position to be in the middle of the drag shadow
            touch.set(width / 2, height / 2);
        }

        // Defines a callback that draws the drag shadow in a Canvas that the system constructs
        // from the dimensions passed in onProvideShadowMetrics().
        @Override
        public void onDrawShadow(Canvas canvas) {

            // Draws the ColorDrawable in the Canvas passed in from the system.
            shadow.draw(canvas);
}
}
你看它画的是drawable,跟传进来的view有半毛钱关系么。。。


希望有大牛能够进来指教一下

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值