關於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有半毛錢關係麼。。。


希望有大牛能夠進來指教一下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章