2016-02-29 87 views
0

Im使用拖放操作將視圖拖動到目標上。 我很難搞清楚如何讓原始視圖在被拖動時被拖動到不可見狀態,並且如果拖動結束時再次可見,但它不會被拖放到目標上。Android拖放可見/不可見

final TextView dragText1 = (TextView) v.findViewById(R.id.text_user_drags_1); 
dragText1.setOnTouchListener(new MyTouchListener()); 
final TextView target1 = (TextView) v.findViewById(R.id.text_target_1); 
target1.setOnDragListener(new MyDragListener()); 


private final class MyTouchListener implements View.OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      ClipData data = ClipData.newPlainText("", ""); 
      DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
      view.startDrag(data, shadowBuilder, view, 0); 
      // view.setVisibility(View.INVISIBLE); 
      return true; 
     } else { 
      //view.setVisibility(View.VISIBLE); 
      return true; 
     } 
    } 
} 
class MyDragListener implements OnDragListener { 

    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     int action = event.getAction(); 
     switch (event.getAction()) { 
      case DragEvent.ACTION_DRAG_STARTED: 

       TextView draggedView2 = (TextView) event.getLocalState(); 
       ViewGroup owner1 = (ViewGroup) draggedView2.getParent(); 
       owner1.setVisibility(View.INVISIBLE); 
       Log.d(TAG, "DRAG STARTED"); 
       break; 
      case DragEvent.ACTION_DRAG_ENTERED: 

       Log.d(TAG, "DRAG ENTERED"); 
       break; 
      case DragEvent.ACTION_DRAG_EXITED: 

       Log.d(TAG, "DRAG EXITED"); 
       break; 
      case DragEvent.ACTION_DROP: 
       // Dropped, reassign View to ViewGroup 
       TextView draggedView = (TextView) event.getLocalState(); 
       ViewGroup owner = (ViewGroup) draggedView.getParent(); 
       owner.setVisibility(View.INVISIBLE); 
       Log.d(TAG, "DRAG DROPPED"); 
       break; 

      case DragEvent.ACTION_DRAG_ENDED: 
       if(event.getAction() == DragEvent.ACTION_DROP){ 
        TextView draggedView3 = (TextView) event.getLocalState(); 
        ViewGroup owner3 = (ViewGroup) draggedView3.getParent(); 
        owner3.setVisibility(View.INVISIBLE); 
       } 
       TextView draggedView4 = (TextView) event.getLocalState(); 
       ViewGroup owner4 = (ViewGroup) draggedView4.getParent(); 
       owner4.setVisibility(View.VISIBLE); 
       Log.d(TAG, "DRAG ENDED"); 
      default: 
       break; 
     } 
     return true; 
    } 
} 

回答

0

您必須使用事件ACTION_DRAG_LOCATION。在這個事件中,你得到你的視圖的拖拽X,並將一個alpha值應用到與拖拽百分比成比例的視圖中。因此,如果您的視圖已被拖動50%,則應用0.5的alpha;拖動75%,應用.25的alpha等等(alpha =(100f - drag_percent)/ 100f