Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

how to swipe a button onTouch event in android

How to swipe a button on touch event android with directions left,right, up,down..?...I am new to android and want to develop a callback application.Please help by providing some links

callbackButton.setOnTouchListener(new OnTouchListener() {

    private float currX;
    private float currY;

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        float x1 = 0, x2, y1 = 0, y2, dx, dy , oldx =0,oldy=0;
        String direction;

        switch(event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            oldx = event.getX();
            oldy = event.getY();
            break;

        case MotionEvent.ACTION_MOVE:

            x2 = event.getX();
            y2 = event.getY();
            dx = x2-x1;
            dy = y2-y1;

            // Use dx and dy to determine the direction
            if(Math.abs(dx) > Math.abs(dy)) {
                if(dx>0) {
                    direction = "right";
                    Log.e("right...","moving..");
                }else{
                    direction = "left";
                    Log.e("left...","moving..");
                }
            } else {
                if(dy>0) {
                    direction = "down";
                    Log.e("down...","moving..");

                    currX = event.getRawX();
                    currY = event.getRawY();

                    Log.e("x=", ""+(currX-oldx));
                    Log.e("y=", ""+(currY-oldy));

                    MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
                    marginParams.setMargins((int)(currX-oldx), (int)(currY-oldy),0, 0);

                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
                    v.setLayoutParams(layoutParams);
                } else {
                    direction = "up";
                    Log.e("up...","moving..");
                }
            }
        }
        return true;
    }
});

This is my code for imageButton moving on touch event. But this code is not working as I want

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here you will find an explaination on how to handle touch events:

http://developer.android.com/guide/topics/ui/ui-events.html

Then, in the on touch method, you need to

  • detect when the user put his finger down
  • store the finger's position
  • detect when the user moves
  • compare with previous position
  • detect when the user stops touching the screen

Example (using code from http://developer.android.com/training/graphics/opengl/touch.html)

@Override
public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    float x = e.getX();
    float y = e.getY();

        switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mIsDown = true;
            break;
        case MotionEvent.ACTION_MOVE:

            float dx = x - mPreviousX;
            float dy = y - mPreviousY;

            // Here you can try to detect the swipe. It will be necessary to
            // store more than the previous value to check that the user move constantly in the same direction
            detectSwipe(dx, dy);

        case MotionEvent.ACTION_UP:
            mIsDown = false;
            break;
    }

    mPreviousX = x;
    mPreviousY = y;
    return true;
}

With this simple case, you wouldn't even need to stor when the user puts his finger down or up (since moving implies a finger down) but it can be useful to have the boolean value stored)

Good luck with your app

EDIT:

It seems you edited your post with some code. You say that you don't get the explected results but you don't say what you get. This might be useful for us to help you.

You should try to find if a library that detects swipe movements already exists. I'm pretty sure there is many out there

EDIT 2: I assume you button is a simple android.Button. One solution could be to create a class that extends Button (ex: MySwipableButton). in your xml, you create a layout that contains your MySwipableButton, and give it enought place to be moved (for example, it has width=fill_parent, since you want it to swipe on the while screen). MySwipableButton implements onTouch to store the position in which the button should be (using the method you already have) MySwipableButton would also overwrite onDraw(Graphics g). In onDraw, you would paint the button (super.draw()) at the place it must be (regarding the current swipe) and leave the rest of the view empty


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...