Far*_*han 7 android relativelayout ontouchlistener android-imageview
我正在做的很简单的事情,我以前做过,但现在它没有像我预期的那样运行.无论如何让我简要解释一下我想要做什么和我得到什么::
方案::
我有一个RelativeLayout在其中ImageView放置,现在我设定touchlistener如下:imageview.setOnTouchListener(this);
它要求我覆盖onTouch(View v,MotionEvent event)我做的功能......并且在Action_Move我得到x和y并将它们添加到左边和上边距,这样我移动我的图像.
奇怪的问题:
我ImageView正在移动,但是有一个非常明显的震动,就像我正朝着正确的方向移动一样,ImageView会向右侧移动但是在它回来的路上它会回到左侧,这样它看起来就像是图像不稳定,它的振动. .类似的东西..我给X和Ÿ我在得到log......希望这给你的想法.
ACTION_DOWN[#0(pid 0)=160,233] Going right..
ACTION_MOVE[#0(pid 0)=160,233] ////
ACTION_MOVE[#0(pid 0)=160,233] //
ACTION_MOVE[#0(pid 0)=174,231] //
ACTION_MOVE[#0(pid 0)=176,233] //
ACTION_MOVE[#0(pid 0)=196,232] //
ACTION_MOVE[#0(pid 0)=152,232] // suddenly i got 152 as location for x... image comes back
ACTION_MOVE[#0(pid 0)=167,232] // again started to go right
ACTION_MOVE[#0(pid 0)=180,233] // going right
ACTION_MOVE[#0(pid 0)=173,233] // again comes a little back
ACTION_MOVE[#0(pid 0)=187,232] // same thing goes till end..
ACTION_MOVE[#0(pid 0)=159,232]
ACTION_MOVE[#0(pid 0)=174,231]
ACTION_MOVE[#0(pid 0)=177,233]
ACTION_MOVE[#0(pid 0)=189,231]
ACTION_MOVE[#0(pid 0)=155,232]
ACTION_MOVE[#0(pid 0)=171,231]
ACTION_MOVE[#0(pid 0)=183,230]
ACTION_MOVE[#0(pid 0)=161,234]
ACTION_MOVE[#0(pid 0)=171,233]
ACTION_MOVE[#0(pid 0)=174,230]
ACTION_MOVE[#0(pid 0)=183,230]
ACTION_MOVE[#0(pid 0)=162,234]
ACTION_MOVE[#0(pid 0)=170,233]
ACTION_MOVE[#0(pid 0)=176,233]
ACTION_MOVE[#0(pid 0)=165,233]
ACTION_MOVE[#0(pid 0)=175,232]
ACTION_MOVE[#0(pid 0)=163,233]
ACTION_MOVE[#0(pid 0)=171,233]
ACTION_MOVE[#0(pid 0)=167,233]
ACTION_MOVE[#0(pid 0)=172,232]
ACTION_MOVE[#0(pid 0)=178,232]
ACTION_MOVE[#0(pid 0)=158,234]
ACTION_MOVE[#0(pid 0)=170,234]
ACTION_MOVE[#0(pid 0)=170,232]
ACTION_MOVE[#0(pid 0)=177,231]
ACTION_MOVE[#0(pid 0)=157,234]
ACTION_MOVE[#0(pid 0)=160,234]
ACTION_MOVE[#0(pid 0)=169,232]
ACTION_MOVE[#0(pid 0)=165,233]
ACTION_MOVE[#0(pid 0)=167,233]
ACTION_MOVE[#0(pid 0)=159,233]
ACTION_UP [#0(pid 0)=161,233]
Run Code Online (Sandbox Code Playgroud)
RelativeLayout的XML代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#99000000">
<ImageView
android:id="@+id/imageview"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="100dp"
android:scaleType="matrix" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
所以,这是最大的,我可以解释我的问题,如果你需要什么,请告诉我.
更新::
用于移动ImageView其容器的代码,即RelativeLayout:
case MotionEvent.ACTION_MOVE:
LayoutParams params = (LayoutParams) imageview.getLayoutParams();
params.leftMargin = params.leftMargin + x;
params.topMargin = params.topMargin + y;
imageview.setLayoutParams(params);
break;
Run Code Online (Sandbox Code Playgroud)
X和Y变量onTouch是相对于具有onTouchListener附加的View的X和Y. 所以10,10会从左上角的你ImageView不会的RelativeLayout.
如果你使用这些值来移动你ImageView那么你自然会得到不稳定的行为,每个移动动作也会触发一个稍微不同的OnTouchEvent导致另一个移动动作 - 这个递归会给你一个"振动"的效果.
| 归档时间: |
|
| 查看次数: |
901 次 |
| 最近记录: |