简体   繁体   English

拖动增加/减少值

[英]Increase/ decrease value with dragging

I'd like to have such a feauture in my app: when user drag finger on the screen up to down some parameter will be decreased, when drag down to up, parameter will be increased. 我想在我的应用程序中具有这样的功能:当用户在屏幕上向上或向下拖动手指时,某些参数将减少,当向下拖动时,参数将增加。 I will use is to dynamically change length of drawed element. 我将使用的是动态更改绘制元素的长度。 Could you give me some instrutions how to write such a thing? 你能给我一些指导如何写这样的东西吗?

EDIT: To be more precisely. 编辑:更准确地说。 I think I should use MotionEvent.ACTION_MOVE. 我想我应该使用MotionEvent.ACTION_MOVE。 But can't figure out how to do something like: depending on move Y length I add or subtract proportional value. 但是无法弄清楚如何做:根据移动Y的长度,我增加或减少比例值。 Could you help me? 你可以帮帮我吗?

I couldn't find my solution (), but here is what I have done today: 我找不到解决方案(),但是今天我做了以下操作:

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;
    }

}

Activity must have TextView01, TextView02, TextView03 and TextView04. 活动必须具有TextView01,TextView02,TextView03和TextView04。

01 and 02 display increased/decreased values with gesture onScroll method usage. 01和02使用手势onScroll方法用法显示增加/减少的值。 I used it today first time. 我今天第一次使用它。 There is only problem with GestureDetector(this) constructor. GestureDetector(this)构造函数只有一个问题。 It is deprecated and you should find how to replace it with modern version (shouldn't be difficult but it is long time since I done something with Android and I don't have time today to do it, my friend waits with whiskey :D ). 它已被弃用,您应该找到如何用现代版本替换它(应该不会很困难,但是由于我用Android做了一些事情而且我今天没有时间这样做,已经很长时间了,我的朋友等威士忌:D )。

I tried mentioned onScroll because I couldn't remember how I did it previously with simply touch events. 我试过提到onScroll因为我不记得以前通过简单的触摸事件是如何做到的。 Presented method which displays result on 03 and 04 TextViews is correct, but only when user don't make "return moves". 所显示的在03和04 TextViews上显示结果的方法是正确的,但仅当用户不进行“返回移动”时才适用。 Correct operation is only when finger touches down, moves in horizontal or vertical direction and do go back until stops touching the screen. 正确的操作只有在手指向下触摸,沿水平或垂直方向移动并返回时才停止,直到停止触摸屏幕为止。 It is because move value is always added into result without checking if it is getting bigger or lower. 这是因为始终将移动值添加到结果中,而无需检查它是变大还是变小。 I made it in February somehow by adding some more code, but who knows now how... ;P 我在二月份以某种方式通过添加更多代码来做到这一点,但现在谁知道呢...; P

Please consider that I'm total noob and there probably are other more efficient ways but hope it helps:) 请考虑我完全是菜鸟,可能还有其他更有效的方法,但希望对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM