2012-12-03 44 views
1

這是我的佈局。它有兩種觀點,我通過另一種觀點。如何將視圖向右或向左移動時執行操作?就像一個幻燈片鎖定按鈕

<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

試試這個:

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

} 
+0

謝謝。我會嘗試像這樣的東西。 – androidevil

+0

謝謝,解決了左右兩邊的問題 現在我正在嘗試診斷權和診斷權,如果你知道那也請發佈snipper –

+0

@SagarDevanga對不起,我不知道在這一刻! – Ascension

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; 
+0

謝謝。我會嘗試 – androidevil

相關問題