滑动Butoon不在布局内移动

时间:2016-03-02 13:02:43

标签: android

enter image description here

在上图中我创建了一个滑动按钮,我希望它只向右侧移动。当它向右侧时,我希望执行下一个活动。 问题是当我移动按钮时整个相对布局移动。例如,如果我向下移动按钮,则相对布局向下扩展。向左向右扩展。向右移动。右边是布局和活动。

<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);
        }
}

1 个答案:

答案 0 :(得分: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;
        }
    };
}

我希望它对你有所帮助。

相关问题