gpt4 book ai didi

android - 当我向右或向左移动 View 时如何执行操作?像滑动锁定按钮

转载 作者:行者123 更新时间:2023-11-29 15:25:32 24 4
gpt4 key购买 nike

这是我的布局。它有两个 View ,我将一个 View 移动到另一个 View 。

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

这是我在 Activity 类中设置的要移动的 View :

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

这就是我移动 View 的方式:

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

我想在它向右移动时执行一个操作,在向左移动时执行另一个操作。

我该怎么做?谢谢!

最佳答案

试试这个:

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

}

关于android - 当我向右或向左移动 View 时如何执行操作?像滑动锁定按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13688363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com