我正在做一个项目.我需要在其中拖动布局的地方设法移动(拖动)布局,但是当我松开触摸时,它并没有到达其原始位置.
请任何人告诉我我应该在MotionEvent.ACTION_UP中做什么,以便我可以将布局恢复到原始位置.
以下是我的代码.
import androID.app.Activity;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OntouchListener;import androID.vIEw.VIEwGroup;import androID.Widget.relativeLayout;public class HomeActivity extends Activity implements OntouchListener { relativeLayout _vIEw; relativeLayout _vIEw1; // TextVIEw _vIEw; VIEwGroup _root; private int _xDelta; relativeLayout.LayoutParams lParams; relativeLayout.LayoutParams mainlParams; private int _yDelta; // private int X1, Y1, wIDth; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); _root = (VIEwGroup) findVIEwByID(R.ID.root); _vIEw = (relativeLayout) findVIEwByID(R.ID.relativeLayout); _vIEw.setontouchListener(this); lParams = (relativeLayout.LayoutParams) _vIEw.getLayoutParams(); mainlParams = (relativeLayout.LayoutParams) _vIEw.getLayoutParams(); // _root.addVIEw(_vIEw1); } public boolean ontouch(VIEw vIEw, MotionEvent event) { int X = (int) event.getRawX(); int Y = (int) event.getRawY(); relativeLayout.LayoutParams layoutParams; lParams = (relativeLayout.LayoutParams) vIEw.getLayoutParams(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: Log.e("", "insIDe ACTION_DOWN"); lParams = (relativeLayout.LayoutParams) vIEw.getLayoutParams(); _xDelta = X - lParams.leftmargin; _yDelta = Y - lParams.topmargin; break; case MotionEvent.ACTION_UP: Log.e("", "insIDe ACTION_UP"); if ((X - _xDelta) < -150) { _root.removeVIEw(_vIEw); } else { vIEw.setLayoutParams(mainlParams); } break; case MotionEvent.ACTION_MOVE: Log.e("", "insIDe ACTION_MOVE"); layoutParams = (relativeLayout.LayoutParams) vIEw.getLayoutParams(); layoutParams.leftmargin = X - _xDelta; layoutParams.topmargin = Y - _yDelta; layoutParams.rightmargin = -250; layoutParams.bottommargin = -250; vIEw.setLayoutParams(layoutParams); break; } _root.invalIDate(); return true; }}
解决方法:
新编辑
在这里,我向您展示了一种非常简单的方法(并且代码不是很干净),该如何做您想做的事情.我有一个带有绿色linearLayout的relativeLayout:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:ID="@+ID/relative_layout"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"tools:context=".MainActivity" ><linearLayout androID:ID="@+ID/moving_layout" androID:layout_wIDth="100dp" androID:layout_height="100dp" androID:background="#00ff00" androID:gravity="center" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/hello_world" /></linearLayout> </relativeLayout>
然后在您的MainActivity中,将要移动的视图位于其中的topLayout设置为ontouchListener.在ACTION_DOWN上设置originalX和originalY,在ACTION_MOVE上设置moveX和moveY.当用户松开手指时,请返回原始位置:
public class MainActivity extends Activity implements OntouchListener {private linearLayout mlinearLayout;private relativeLayout mrelativeLayout;private relativeLayout.LayoutParams params;private float originalX = 0;private float originalY = 0;private float moveX = 0;private float moveY = 0;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mlinearLayout = (linearLayout) findVIEwByID(R.ID.moving_layout); mrelativeLayout = (relativeLayout) findVIEwByID(R.ID.relative_layout); mrelativeLayout.setontouchListener(this); int wIDth = getMetrics(100); int height = getMetrics(100); params = new relativeLayout.LayoutParams(wIDth, height);}/** * converts dp to px * * @param dp * @return */private int getMetrics(int dp) { displayMetrics displayMetrics = this.getResources().getdisplayMetrics(); return (int) ((dp * displayMetrics.density) + 0.5);}public boolean ontouch(VIEw v, MotionEvent event) { // Todo auto-generated method stub switch (event.getAction()) { case (MotionEvent.ACTION_DOWN): originalX = event.getX(); //set X start position originalY = event.getY();//set Y start position moveX = event.getX();//first move x moveY = event.getY();//first move y break; case (MotionEvent.ACTION_MOVE): moveX = event.getX();//set move x moveY = event.getY();//set move y //set LayoutParams to mlinearLayout params.leftmargin = (int) moveX; params.topmargin = (int) moveY; mlinearLayout.setLayoutParams(params); break; case (MotionEvent.ACTION_UP): //set mlinearLayout back to original position params.leftmargin = (int) originalX; params.topmargin = (int) originalY; mlinearLayout.setLayoutParams(params); break; } return true;} }
不要怀疑getMetrics()方法,这是因为将与XML定义相同的宽度和高度设置为mlinearLayout.在xml中,将此值设置为dp,在LayoutParams处为px.但这与您的问题无关.
我在这里所做的是,将原始X和Y设置为ACTION_DOWN,然后回到ACTION_UP.因此,每次用户再次按下时,这些值将被“更新”,如果手指松开,则布局将返回到ACTION_DOWN的最后一点.这应该给您一个解决问题的思路.
以上是内存溢出为你收集整理的拖动后如何将布局恢复到android中的原始位置全部内容,希望文章能够帮你解决拖动后如何将布局恢复到android中的原始位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)