拖动增加/减少值

时间:2012-02-12 19:28:41

标签: java android

我想在我的应用程序中有这样一个功能:当用户将手指在屏幕上向上拖动时,某些参数将会减少,当向下拖动到向上时,参数将会增加。我将使用是动态更改绘制元素的长度。你能给我一些指示如何写这样的东西吗?

编辑: 更准确地说。我想我应该使用MotionEvent.ACTION_MOVE。但无法弄清楚如何做一些事情:取决于移动Y长度我加或减比例值。你能帮帮我吗?

1 个答案:

答案 0 :(得分: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上显示结果的显示方法是正确的,但仅在用户不进行“返回移动”时才显示。正确操作仅在手指触摸时,在水平或垂直方向上移动并返回直到停止触摸屏幕。这是因为移动值总是被添加到结果中而不检查它是变大还是变小。我在2月以某种方式通过添加更多代码来实现它,但现在谁知道如何...; P

请考虑我是总菜鸟,可能还有其他更有效的方法,但希望有所帮助:)