关于DragShadowBuilder无法显示阴影问题探究
2014-10-20 15:18
357 查看
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传入一个位图,自己画
希望有大牛能够进来指教一下
构造方法
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有半毛钱关系么。。。
希望有大牛能够进来指教一下
相关文章推荐
- 关于CardView无法显示阴影的问题
- 关于最近出现的病毒导致桌面无法显示的问题
- eclipse3.2 关于VSS状态图标无法显示问题
- 解决关于vs2010中"w"无法 显示的问题
- 关于css图片按钮在IE6下无法显示的问题
- 关于在Hyper-V上安装了windows server 2003、2003SP1 R2后无法显示网卡的问题
- 关于博客无法正确显示后台信息的问题
- 关于无法显示该网页,弹出对话框显示"Internet Explorer无法打开Internet站点xxx,已终止操作"问题的解决方法
- 关于图片在窗体显示后,无法更新或删除的问题
- 关于window2008计划任务前台显示和批处理无法正常执行问题的解决办法
- 关于CSDN登录时,图片检验码无法显示的问题处理
- 关于SWT中 composite 嵌套 composite 内容无法显示的问题解决办法
- 关于系统托盘图标(systemtray)无法显示问题的解决
- 关于使用Adoeb SVG Viewer在IE中无法正常显示SVG文档的问题
- 关于win32绘图出现无法显示彩色问题的解决
- 关于TreeView控件发布后无法显示展开图标的问题
- 关于从GridView表中导出数据到Excel表中,身份证或者长度过长的数据在Excel中无法正常显示的问题
- 关于CListCtrl控件中无法显示新插入列的问题
- 【JAVA】使用jacob生成的html,关于文字乱码处理,图片无法显示等问题。
- 关于使用了QtWebKit的PyQt程序打包后无法显示网页图片的问题(Mac0S和windows)