7

その非常に簡単なことは、私は前にそれを行っているが、私は予想通り、今のが実行されていません。 imageview.setOnTouchListener(this);onTouch与える奇妙なタッチポイント私がやっているのAndroid

:とにかく、私は私がやろうとしていると私は何を取得しています::

シナリオ:: を私は今、私はこのようtouchlistenerを設定し、ImageViewが置かれているRelativeLayoutを持っているものを簡単に説明しましょう

このように私は私のイメージを移動しています、それは ...私がやったonTouch(View v,MotionEvent event)機能を無効にするために私に尋ねたとAction_Moveに私はXYを取得し、余白を残してトップにそれらを追加します。

奇妙な問題: マイImageViewであることが見えるように非常に顕著な揺れで、私は正しい方向に向かっていた場合と同様に、ImageViewが右側に移動しますが、その途中でそれが左側に戻ってくる移動画像が安定していないように、その...私は私がlog中に取得XYを与えている..そのような何かを振動させる、これはあなたのアイデアを与える願っています。 RelativeLayoutのため

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] 

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> 

ので、これは最高です、私はあなたが何を必要とする場合、まだ、私に知らせて、私の質問を説明することができました。

更新::そのコンテナすなわちRelativeLayoutImageViewを移動するための コード:onTouch

case MotionEvent.ACTION_MOVE: 
      LayoutParams params = (LayoutParams) imageview.getLayoutParams(); 

      params.leftMargin = params.leftMargin + x; 
      params.topMargin = params.topMargin + y; 

      imageview.setLayoutParams(params); 
      break; 

答えて

5

XとY変数が添付onTouchListenerを持っているビューにXとYの相対的なものです。したがって、10,10は、RelativeLayoutではないImageViewの左上からのものです。

これらの値を使用してImageViewを移動すると、自然に不安定な動作が発生します。また、それぞれの移動アクションは、別の移動アクションを引き起こすわずかに異なるOnTouchEventを起動します。この再帰によって、「振動」効果がもたらされます。

+0

「各移動アクションは、別の移動アクションを起こし、わずかに異なるOnTouchEventを発生させます - この再帰はあなたに与えられる 『振動』効果を。」それを少し説明してください、リンク/ドキュメントは、それが詳細に書かれているところで非常に役に立つでしょう... – Farhan

0

event.getY()の代わりにevent.getRawY()のY座標を取得してビューを移動したときに、不安定な振る舞いが消えます。 getY()メソッドを使用していた場合は、変更してみてください。

関連する問題