2016-01-25 126 views
0

我正在一個項目上移動整個屏幕上的浮動按鈕。我寫了一個簡單的代碼,用於拖動浮動按鈕的圖像&。但它並沒有在設備的特定屏幕中移動。它在頭部隱藏,也從各個角落切下。我附上了一些截圖。我怎樣才能從各個方面添加一些填充。 drag in corners evenenter image description here拖放圖像在設備的屏幕尺寸使用android

這裏是我拖&跌落事件代碼:

mFloatingActionButton.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent event) { 
       if (gestureDetector.onTouchEvent(event)) { 
        Intent mIntent = new Intent(mActivity, Cart.class); 
        startActivity(mIntent); 
       } else { 

        int X = (int) event.getRawX(); 
        int Y = (int) event.getRawY(); 

        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 

        switch (event.getAction() & MotionEvent.ACTION_MASK) { 
         case MotionEvent.ACTION_DOWN: 
          view.getLayoutParams(); 

          _xDelta = X - lParams.leftMargin; 
          _yDelta = Y - lParams.topMargin; 
          break; 
         case MotionEvent.ACTION_UP: 
          break; 
         case MotionEvent.ACTION_POINTER_DOWN: 
          break; 
         case MotionEvent.ACTION_POINTER_UP: 
          break; 
         case MotionEvent.ACTION_MOVE: 

          if (X > windowwidth) { 
           X = windowwidth; 
          } 
          if (Y > windowheight) { 
           Y = windowheight; 
          } 
          lParams.leftMargin = X - _xDelta; 
          lParams.topMargin = Y - _yDelta; 
          lParams.rightMargin = 100; 
          lParams.bottomMargin = 100; 
          view.setLayoutParams(lParams); 
          break; 
        } 
        mRrootLayout.invalidate(); 
        return true; 
       } 
       return true; 
      } 
     }); 

XML文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:fab="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white_color"> 

    <in.srain.cube.views.GridViewWithHeaderAndFooter 
     android:id="@+id/gridProductList" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fadingEdge="none" 
     android:focusable="false" 
     android:listSelector="@android:color/transparent" 
     android:numColumns="2" 
     android:overScrollMode="never" 
     android:paddingBottom="4dp" 
     android:scrollbarStyle="outsideOverlay" 
     android:scrollbars="none" 
     android:stretchMode="columnWidth" /> 

    <RelativeLayout 
     android:id="@+id/rl_parent_floating" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:visibility="visible"> 

     <RelativeLayout xmlns:fab="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/rl_floating_button" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <com.melnykov.fab.FloatingActionButton 
       android:id="@+id/fab" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginTop="3dp" 
       android:src="@drawable/ic_floating_cart" 
       fab:fab_colorNormal="@color/header_color_red" 
       fab:fab_colorPressed="@color/colorPrimaryDark" 
       fab:fab_colorRipple="@color/gold_color" /> 

      <TextView 
       android:id="@+id/txtCartCount" 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:layout_alignRight="@+id/fab" 
       android:layout_alignTop="@+id/fab" 
       android:background="@drawable/cart_gold_circle" 
       android:gravity="center" 
       android:singleLine="true" 
       android:text="2" 
       android:textColor="@android:color/white" 
       android:textSize="8sp" /> 

     </RelativeLayout> 
    </RelativeLayout> 
</FrameLayout> 

回答