简体   繁体   中英

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. But can't figure out how to do something like: depending on move Y length I add or subtract proportional value. 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.

01 and 02 display increased/decreased values with gesture onScroll method usage. I used it today first time. There is only problem with GestureDetector(this) constructor. 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 ).

I tried mentioned onScroll because I couldn't remember how I did it previously with simply touch events. Presented method which displays result on 03 and 04 TextViews is correct, but only when user don't make "return moves". 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

Please consider that I'm total noob and there probably are other more efficient ways but hope it helps:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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