2014-01-13 30 views
5

如問題所述。這裏是我的dragshadowbuilder供參考。如您所知,拖動時創建的陰影是半透明的。無論如何要讓它變得不透明嗎? (完全固體)是否有可能使陰影不透明?

public class MyDragShadowBuilder extends View.DragShadowBuilder { 
    View view; 
    int width; 
    int height; 
    Bitmap bitmap; 
    public MyDragShadowBuilder(View v) { 
     super(v); 
     view = v; 
     ImageView b = (ImageView)view; 
     int x = Character.getNumericValue(b.getContentDescription().charAt(0)); 
     int y = Character.getNumericValue(b.getContentDescription().charAt(1)); 
     bitmap = InGameActivity.currentBoardState[x][y].getPicture(); 
     width = bitmap.getWidth(); 
     height = bitmap.getHeight(); 
    } 

    @Override 
    public void onDrawShadow(Canvas canvas) { 
     Rect source = new Rect(0, 0, InGameActivity.chessSquareHeightWidth, InGameActivity.chessSquareHeightWidth); 
     canvas.drawBitmap(bitmap, null, source, null); 
    } 

    @Override 
    public void onProvideShadowMetrics(Point shadowSize, Point touchPoint) { 
     shadowSize.set(InGameActivity.chessSquareHeightWidth, InGameActivity.chessSquareHeightWidth); 
     touchPoint.set(InGameActivity.chessSquareHeightWidth/2, InGameActivity.chessSquareHeightWidth/2); 
    } 
} 

編輯︰這是我目前的onTouch代碼,我也會提供應用程序的背景。

OnTouchListener myOnTouchListener = new OnTouchListener() { 

       @Override 
       public boolean onTouch(View view, MotionEvent event) { 
        System.out.println(event.toString()); 
        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
         ImageView b = (ImageView)view; 
         int x = Character.getNumericValue(b.getContentDescription().charAt(0)); 
         int y = Character.getNumericValue(b.getContentDescription().charAt(1)); 
         if (!currentBoardState[x][y].isEmpty()) { 
          ((ImageView)view).setImageBitmap(null); 
          DragShadowBuilder shadowBuilder = new MyDragShadowBuilder(view); 
          view.startDrag(null, shadowBuilder, view, 0); 
         } 

         moveChessPiece; 
         return true; 
        } 

        return false; 
       } 
      }); 

好的,這是你需要知道的。我正在開發一個國際象棋應用程序。我有一個名爲currentBoardState的8x8 GridLayout。所以目前我希望能夠拖動該作品,但是我希望被拖動的作品是不透明的,這樣用戶就可以感覺到他/她實際上是MOVING作品,而不僅僅是點擊然後點擊目的地正如我以前所做的那樣。

+0

工作,你的意思是你的看法是半透明的,但你需要它拖影是不透明的? – DouO

回答

9

由於@Ernir說,有沒有API獲得DragShadow。但是我們知道拖動位置和拖動視圖,所以我們可以自己繪製。

我寫了一個簡單和功能有限的演示,你可以下載它here

它實現了一個名爲DragContainer的自定義ViewGroup,使用它將您的原始根視圖包裹在佈局xml中。

<info.dourok.android.demo.DragContainer xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <!-- original root view --> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <!-- ... --> 
    </RelativeLayout> 

</info.dourok.android.demo.DragContainer> 

呼叫DragContainer#startDragChild開始像拖(演示有更多細節):

的活動:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mDragContainer = (DragContainer) findViewById(R.id.root); 
     View drag = mDragContainer.findViewById(R.id.drag); 
     View drop = mDragContainer.findViewById(R.id.drop); 
     drag.setTag(IMAGEVIEW_TAG); 
     drag.setOnLongClickListener(new View.OnLongClickListener() { 
      public boolean onLongClick(View v) { 
       ClipData.Item item = new ClipData.Item((CharSequence) v 
         .getTag()); 
       ClipData dragData = new ClipData((CharSequence) v.getTag(), 
         new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN }, 
         item); 
       return mDragContainer.startDragChild(v, dragData, // the data to 
                    // be 
                    // dragged 
         null, // no need to use local data 
         0 // flags (not currently used, set to 0) 
         ); 

      } 
     }); 


    } 

您還可以擴展DragContainer繪製定製的拖影,這是您的要求的一個例子。

public class MyDragContainer extends DragContainer { 
    public MyDragContainer(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public MyDragContainer(Context context) { 
     super(context, null); 
    } 

    public MyDragContainer(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    int width; 
    int height; 
    Bitmap bitmap; 

    @Override 
    protected void onSetDragTarget(View v) { 
     ImageView b = (ImageView) v; 
     int x = Character.getNumericValue(b.getContentDescription().charAt(0)); 
     int y = Character.getNumericValue(b.getContentDescription().charAt(1)); 
     bitmap = InGameActivity.currentBoardState[x][y].getPicture(); 
     width = bitmap.getWidth(); 
     height = bitmap.getHeight(); 
    } 

    @Override 
    protected void drawDragShadow(Canvas canvas) { 
     Rect source = new Rect(0, 0, InGameActivity.chessSquareHeightWidth, InGameActivity.chessSquareHeightWidth); 

     int w = InGameActivity.chessSquareHeightWidth; 
     int h = InGameActivity.chessSquareHeightWidth; 

     canvas.translate(getDragX() - w/2, getDragY() - h/2); 

     canvas.drawBitmap(bitmap, null, source, null); 
    } 
} 

我希望這會有所幫助。

+0

嗨,@Clay你有演示嗎? – DouO

+0

Hi @Clay,我已更新演示,您需要再次下載它。 – DouO

+0

您必須在您的活動佈局xml中聲明'DragContainer'作爲根視圖,並在'setContentView'之後獲取引用。例如:'mDragContainer =(DragContainer)findViewById(R.id.root);' – DouO

2

不,不幸的是不可能的,據我所知。

影子Bitmap繪製在您無法觸及的表面上。唯一的解決方案就是實現Drag &就像在API 11之前完成的那樣。它實際上並沒有你想象的那麼難。

一些鏈接,讓你開始:

Link 1

Link 2 *更新

+0

你提供的第二個鏈接,是否有不透明的陰影?我可以試試,但也許你已經知道 – Ogen

+0

對不起,第二個鏈接是錯誤的,現在我只是有你的希望了:/。編輯我的迴應。 –

+0

如果我給你我的onTouch代碼,你能幫我把它和你的第二個鏈接集成嗎? – Ogen

3

我認爲實現這一目標的正確方法是:

/* Create the shadow from a view */ 
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
/* Access the view created and set the alpha of it to 1 (totally opaque)*/ 
shadowBuilder.getView().setAlpha(1); 
/* Start dragging the object */ 
view.startDrag(data, shadowBuilder, view, 0); 

編輯 它似乎並沒有對4.4及以後:(