2

这是我的布局。它有两个视图,我通过另一个移动一个。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/time_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/slide_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="20dp"
        android:layout_alignParentBottom="true" >

        <View
            android:id="@+id/slide_to_pause_backgound"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="#0000FF" />

        <View
            android:id="@+id/slide_to_pause"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignLeft="@+id/slide_to_pause_backgound"
            android:layout_alignParentBottom="true"
            android:background="#00FFFF" />
    </RelativeLayout>

这就是我在活动类中设置要移动的视图的方式:

slideView = ((View) findViewById(R.id.slide_to_pause));
slideView.setOnTouchListener(this);   

这就是我移动视图的方式:

@Override
public boolean onTouch(View view, MotionEvent event) {
    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();

    switch (event.getAction() & MotionEvent.ACTION_MASK) {

    case MotionEvent.ACTION_DOWN:
        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        viewLeftMargin = X - lParams.leftMargin;
        viewTopMargin = lParams.topMargin;  
        viewBottomMargin = lParams.bottomMargin;
        viewRightMargin = X - lParams.rightMargin;
        break;

    case MotionEvent.ACTION_MOVE:
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        layoutParams.rightMargin = X - viewRightMargin;
        layoutParams.leftMargin = X - viewLeftMargin;           
        layoutParams.topMargin = viewTopMargin;
        layoutParams.bottomMargin = viewBottomMargin;
        view.setLayoutParams(layoutParams);         

        break;
    }
    return true;
    }
}

我想在它向右时做一个动作,在它向左时做另一个动作。

我怎样才能做到这一点?谢谢!

4

2 回答 2

4

尝试这个:

package com.example.motionevent;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnTouchListener {

    private int viewLeftMargin;
    private int viewTopMargin;
    private int viewBottomMargin;
    private int viewRightMargin;
    private int lastMarginLeft;
    private int lastMarginRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View slideView = (View) findViewById(R.id.slide_to_pause);
        slideView.setOnTouchListener(this);
        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) slideView
                .getLayoutParams();
        lastMarginLeft = lParams.leftMargin;
        lastMarginRight = lParams.rightMargin;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        // TODO Auto-generated method stub
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_DOWN:
            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view
                    .getLayoutParams();
            viewLeftMargin = X - lParams.leftMargin;
            viewTopMargin = lParams.topMargin;
            viewBottomMargin = lParams.bottomMargin;
            viewRightMargin = X - lParams.rightMargin;
            break;

        case MotionEvent.ACTION_MOVE:
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                    .getLayoutParams();
            layoutParams.rightMargin = X - viewRightMargin;
            layoutParams.leftMargin = X - viewLeftMargin;
            layoutParams.topMargin = viewTopMargin;
            layoutParams.bottomMargin = viewBottomMargin;
            view.setLayoutParams(layoutParams);
            if (layoutParams.leftMargin > lastMarginLeft) {
                Log.d("ON_TOUCH", "Moving to right!");
                lastMarginLeft = layoutParams.leftMargin;
                                lastMarginRight = layoutParams.rightMargin; // Here
            } else {
                Log.d("ON_TOUCH", "Moving to left!");
                lastMarginRight = layoutParams.rightMargin;
                lastMarginLeft = layoutParams.leftMargin; // and here
            }
            break;
        }
        return true;
    }

}
于 2012-12-03T18:01:40.557 回答
1

尝试记住视图上第一次触摸的 X 位置,并在移动操作中通过检查该点与当前位置之间的差异来确定用户是向左还是向右移动(可能考虑到向左/向右滑动默认值距离):

public static final int MOVE_SIZE = 30;
public int mStartingX = -1000;

@Override
public boolean onTouch(View view, MotionEvent event) {
    final int X = (int) event.getX();
    final int Y = (int) event.getY();

    switch (event.getAction() & MotionEvent.ACTION_MASK) {

    case MotionEvent.ACTION_DOWN:
        mStartingX = X;
        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        viewLeftMargin = X - lParams.leftMargin;
        viewTopMargin = lParams.topMargin;  
        viewBottomMargin = lParams.bottomMargin;
        viewRightMargin = X - lParams.rightMargin;
        break;

    case MotionEvent.ACTION_MOVE:
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        layoutParams.rightMargin = X - viewRightMargin;
        layoutParams.leftMargin = X - viewLeftMargin;           
        layoutParams.topMargin = viewTopMargin;
        layoutParams.bottomMargin = viewBottomMargin;
        view.setLayoutParams(layoutParams);         
        if (mStartingX != -1000 && X >= mCurrentX + MOVE_SIZE) {
            doActionRight(); 
        } else if (mStartingtX != -1000 && X < mStartingX - MOVE_SIZE) {
            doActionLeft();
        }
        break;
    case MotionEvent.ACTION_UP:
        mStartingX = -1000;
        break;
于 2012-12-03T18:02:29.323 回答