2012-02-12 192 views
0

我想在我的應用程序中有這樣的特性:當用戶在屏幕上拖動手指時,某些參數會減少,當向下拖動時,參數會增加。我將使用的是動態更改繪製元素的長度。你能給我一些指導如何寫這樣的事情嗎?編輯: 更準確地說,我想我應該使用MotionEvent.ACTION_MOVE。但不知道如何做這樣的事情:根據移動Y長度我加或減去比例值。你可以幫幫我嗎?拖動增加/減少值

+0

我想實現相同的功能,最後做了什麼? – juned 2012-10-30 09:32:26

+0

嗨ju,你還需要這個嗎?對不起,我寫這麼晚了。我甚至不記得我在哪個項目中使用它,但如果你不能解決你的問題,我可以挖掘我的Android工作區並尋找它。不能保證我會找到它(我甚至卸載了Android SDK),但如果您仍然需要,我可以付出一些努力。 – bLAZ 2012-11-10 10:33:15

+0

Thanks @bLAZY如果你能爲我找到它,那將會很棒。如果你能找到那個,我會很幸運。非常感謝 – juned 2012-11-10 10:37:24

回答

0

我找不到我的解決方案(),但這裏是我今天做了:

package com.example.test1; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.GestureDetector; 
import android.view.GestureDetector.OnGestureListener; 
import android.view.Menu; 
import android.view.MotionEvent; 
import android.widget.TextView; 

public class MainActivity extends Activity implements OnGestureListener { 

    private TextView TextView01; 
    private TextView TextView02; 
    private TextView TextView03; 
    private TextView TextView04; 

    private GestureDetector gestureScanner; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView01 = (TextView)findViewById(R.id.TextView01); 
     TextView02 = (TextView)findViewById(R.id.TextView02); 
     TextView03 = (TextView)findViewById(R.id.TextView03); 
     TextView04 = (TextView)findViewById(R.id.TextView04); 



     gestureScanner = new GestureDetector(this); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    int whereIsFirstX = 0; 
    int whereIsFirstY = 0; 
    int diffrenceBetweenFirstAndCurrentTouchPositionX = 0; 
    int diffrenceBetweenFirstAndCurrentTouchPositionY = 0; 

    int changeInHorizontal = 0; 
    int changeInVertical = 0; 



    public boolean onTouchEvent(MotionEvent event) { 

     int eventaction = event.getAction(); 

     int X = (int)event.getX(); 
     int Y = (int)event.getY(); 

     if (eventaction == MotionEvent.ACTION_DOWN) { 
      whereIsFirstX = X; 
      whereIsFirstY = Y; 
     } 

     if (eventaction==MotionEvent.ACTION_MOVE){ 
      diffrenceBetweenFirstAndCurrentTouchPositionX = X - whereIsFirstX; 
      diffrenceBetweenFirstAndCurrentTouchPositionY = whereIsFirstY - Y; 


     } 

     // FINAL RESULT TO USE IN YOUR APP; I USED IT IN VERTICAL DIRECTION TO CHANGE SPEED IN GAME 
     //Here the difference is also divided by 10 to depress the influence of change into result value: 
     changeInHorizontal = changeInHorizontal + diffrenceBetweenFirstAndCurrentTouchPositionX/10; 
     changeInVertical = changeInVertical + diffrenceBetweenFirstAndCurrentTouchPositionY/10; 

     TextView03.setText("Simple Horizontal: " + Integer.toString(changeInHorizontal)); 
     TextView04.setText("SimpleVertical: " + Integer.toString(changeInVertical)); 

     try { // quiet screen 
      Thread.sleep(100); 
     } catch (Exception e) { 

     } 

     //Note that onTouchEvent function has to return it for onScroll method: 
     return gestureScanner.onTouchEvent(event); 


    } 

    public boolean onDown(MotionEvent arg0) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, 
      float arg3) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    public void onLongPress(MotionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    public void onShowPress(MotionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    public boolean onSingleTapUp(MotionEvent arg0) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    int changeInHorizontalScroll; 
    int changeInVerticalScroll; 

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
      float distanceY) { 
     changeInHorizontalScroll = (int)(changeInHorizontalScroll - distanceX); 
     changeInVerticalScroll = (int)(changeInVerticalScroll + distanceY); 
     TextView01.setText("onScroll Horizontal: " + Integer.toString(changeInHorizontalScroll)); 
     TextView02.setText("onScroll Vertical: " + Integer.toString(changeInVerticalScroll)); 
     return false; 
    } 

} 

活動必須有TextView01,TextView02,TextView03和TextView04。

01和02以手勢方式onScroll顯示增加/減少的值。我今天第一次使用它。 GestureDetector(this)構造函數只有問題。它已被棄用,你應該找到如何用現代版本取代它(應該不難,但是我用Android做了很長的一段時間,而今天我沒有時間去做,我的朋友用威士忌等着:D )。

我嘗試過提及onScroll,因爲我記不起以前只是通過觸摸事件做了些什麼。在03和04 TextViews上顯示結果的方法是正確的,但只有當用戶不進行「返回移動」時。正確的操作只有當手指觸摸下來,水平或垂直方向移動,並返回到停止觸摸屏幕。這是因爲移動值總是添加到結果中,而不檢查它是否變大或變小。我在二月份取得它在某種程度上通過增加更多的代碼,但誰知道現在怎麼樣......,P

請考慮,我是總的小白,有可能是其他更有效的方法,但希望它有助於:)

+0

非常感謝:) – juned 2012-11-14 08:05:21