2012-02-12 148 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