2014-04-01 64 views
1

我有一個相對佈局,其中我有一個TextView有一些文本「嗨!這是約翰」。我想將文本移動到任何想要在屏幕上觸摸文本的位置。我在Text View上使用onTouchListener()。任何人都可以幫助我。如何將TextView移動到屏幕上的任何位置?

+0

一套動畫上的TextView –

回答

0

onTouch聽者,並根據它的位置,它移動到你想要的另一個位置上MotionEvent.ACTION_UP事件..

private final static int START_DRAGGING = 0; 
    private final static int STOP_DRAGGING = 1; 
    private int status; 
    int flag=0; 
    float xAxis = 0f; 
    float yAxis = 0f; 
    float lastXAxis = 0f; 
    float lastYAxis = 0f; 
...  
@Override 
public boolean onTouch(View v, MotionEvent me){ 
      if(me.getAction()==MotionEvent.ACTION_DOWN){ 
      status = START_DRAGGING; 
      final float x = me.getX(); 
      final float y = me.getY(); 
      lastXAxis = x; 
      lastYAxis = y; 
      v.setVisibility(View.INVISIBLE); 
      }else if(me.getAction()==MotionEvent.ACTION_UP){ 
      status = STOP_DRAGGING; 
      flag=0; 
      v.setVisibility(View.VISIBLE); 
      }else if(me.getAction()==MotionEvent.ACTION_MOVE){ 
      if (status == START_DRAGGING){ 
      flag=1; 
      v.setVisibility(View.VISIBLE); 
      final float x = me.getX(); 
      final float y = me.getY(); 
      final float dx = x - lastXAxis; 
      final float dy = y - lastYAxis; 
      xAxis += dx; 
      yAxis += dy; 
      v.setX((int)xAxis); 
      v.setY((int)yAxis); 
      v.invalidate(); 
      } 
     } 
     return false; 
     } 
+0

請告訴我如何移動文本 – Lakshmipathi

+0

檢查我的代碼.. – Akshay

+0

您能否提供我的OnTouchListener總代碼() – Lakshmipathi

相關問題