如何刷卡按钮onTouch事件的android [英] how to swipe a button onTouch event in android

查看:163
本文介绍了如何刷卡按钮onTouch事件的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何刷卡触摸事件按钮左方向机器人,右,上,下​​..?......我相信新的Andr​​oid和希望通过提供一些链接来开发一个回调application.Please帮助

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

这是我的$ C $下的ImageButton移动的触摸事件。但是,这code不工作,因为我想

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

推荐答案

在这里你会找到如何处理触摸事件的交代:

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

  • 检测,当用户把他的手指下来
  • 商店手指的位置
  • 检测,当用户移动时
  • 与previous位置比较
  • 检测当用户停止触摸屏幕

为例(使用code从 http://developer.android。 COM /培训/显卡/ OpenGL的/ touch.html

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

通过这个简单的情况下,你甚至不会需要STOR,当用户将手指向下或向上(因为移动意味着一个手指向下),但它可以让存储布尔值)有用

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

编辑:

看来你一些code编辑您的文章。你说你没有得到explected结果,但你不说什么你。这可能是有用的我们来帮助你。

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.

您应该找如果检测刷卡动作库已经存在。我是pretty的肯定有很多了

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

编辑2: 我想你的按钮是一个简单的android.Button。一个解决办法是创建一个类,扩展按钮(如:MySwipableButton)。在XML中,您创建一个包含您的MySwipableButton的布局,并给它足够多的地方移动(例如,它的宽度= FILL_PARENT,因为你希望它轻扫,同时屏幕上)。 MySwipableButton实现onTouch存储在其中的按钮应该是位置(使用你已经拥有的方法) MySwipableButton也将覆盖的OnDraw(图形G)。在OnDraw中,你能画出按钮(super.draw()),它必须是地方(关于当前刷卡),并留下视图的休息空

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

这篇关于如何刷卡按钮onTouch事件的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆