2016-03-02 93 views
0

enter image description here滑動Butoon不是佈局內移動

在上述圖像I已經創建了一個滑動按鈕,我希望向側only.And權當其移動到右側我希望執行下一個活動。 問題是,當我移動按鈕的整個相對佈局moves.example,如果我移動按鈕向下相對佈局向下擴展.same左n右..下面是佈局和活動。

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/rect" 
     android:layout_marginTop="20dp" 
     > 
     <ImageView 
      android:id="@+id/img_slide" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/next" 
      android:background="#00000000" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="10dp" 
      /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#ffffff" 
      android:text="SLIDE TO NEXT ACTIVITY" 
      android:layout_centerInParent="true" 
      android:textStyle="bold"/> 
    </RelativeLayout> 

public class MultiTouchListener implements OnTouchListener 
    { 

     private float mPrevX; 
     private float mPrevY; 

     public MainActivity mainActivity; 
     public MultiTouchListener(MainActivity mainActivity1) 
     { 
      mainActivity = mainActivity1; 
     } 

     @Override 
     public boolean onTouch(View view, MotionEvent event) { 
      float currX,currY; 
      int action = event.getAction(); 
      switch (action) { 
       case MotionEvent.ACTION_DOWN: { 

        mPrevX = event.getX(); 
        mPrevY = event.getY(); 
        break; 
       } 
       case MotionEvent.ACTION_MOVE: 
       { 
        currX = event.getRawX(); 
        currY = event.getRawY(); 

        MarginLayoutParams params = new MarginLayoutParams(view.getLayoutParams()); 
        params.setMargins((int)(currX - mPrevX), (int)(currY - mPrevY),0,0); 

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(params); 
        view.setLayoutParams(layoutParams); 

        break; 
       } 
       case MotionEvent.ACTION_CANCEL: 
        break; 
       case MotionEvent.ACTION_UP: 
        break; 
      } 
      return true; 
     } 
} 

public class MainActivity extends AppCompatActivity 
    { 
     ImageView imgSlide; 

     MultiTouchListener touchListener=new MultiTouchListener(this); 
     @Override 
     protected void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.slidingbtn); 

      imgSlide=(ImageView)findViewById(R.id.img_slide); 
      imgSlide.setOnTouchListener(touchListener); 
     } 
} 
+0

您是否將按鈕固定在相對佈局上,即您是否給出了任何屬性,如layout_align_parentleft等 –

+0

No.I還沒有完成。 – Harshad

回答

1

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" 
    tools:context="com.example.slidetoopen.MainActivity"> 

    <RelativeLayout 
     android:id="@+id/rlBack" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/bg_slide"> 

     <ImageView 
      android:id="@+id/img_slide" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp" 
      android:src="@drawable/bg_circle" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="SLIDE TO NEXT ACTIVITY" 
      android:textColor="#ffffff" 
      android:textStyle="bold" /> 
    </RelativeLayout> 
</RelativeLayout> 

bg_c​​ircle.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners android:radius="100pt" /> 
    <solid android:color="#054b9b" /> 
    <stroke 
     android:width="1dp" 
     android:color="#a2c1ff" /> 

    <size 
     android:width="50dp" 
     android:height="50dp" /> 
</shape> 

bg_slide.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners android:radius="100pt" /> 
    <solid android:color="#000000" /> 
    <stroke 
     android:width="2dp" 
     android:color="#0000FF" /> 

</shape> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private static final String TAG = MainActivity.class.getSimpleName(); 
    private ImageView img_slide; 
    private RelativeLayout rlBack; 
    private float x = 0, posX = 0, preX = 0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     rlBack = (RelativeLayout) findViewById(R.id.rlBack); 
     img_slide = (ImageView) findViewById(R.id.img_slide); 
     img_slide.setOnTouchListener(touchListener); 
    } 

    private View.OnTouchListener touchListener = new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        v.bringToFront(); // set current view at front of other view 
        x = event.getX(); // get x pos 
        preX = v.getX(); // get view x pos 
        break; 
       case MotionEvent.ACTION_MOVE: 
        float curX = event.getX() - x; // subtract previous x from 
        // current x 
        posX = preX + curX; 
        // check boundary of parent view, so view is move inside 
        // only parent view 
        if (posX > rlBack.getX() + 10 && (posX + v.getWidth()) < rlBack.getWidth() - 10) { 
         v.setX(posX); 
         preX = posX; 
        } 
        break; 
       case MotionEvent.ACTION_UP: 
        // check circle is left of center of right of center 
        int width = rlBack.getWidth() - 10 - v.getWidth();  // 10 is for margin 
        if (v.getX() > (width/2)) { 
         v.setX(width); 
         Toast.makeText(MainActivity.this, "Open", Toast.LENGTH_SHORT).show(); 
         // code for activity open here 
        } else { 
         v.setX(rlBack.getLeft()); 
         Toast.makeText(MainActivity.this, "Not Open", Toast.LENGTH_SHORT).show(); 
        } 
        break; 
      } 
      v.invalidate(); 
      return true; 
     } 
    }; 
} 

我希望這對您有所幫助。

+0

非常感謝。這真的很有用。你用什麼方法? – Harshad