【问题标题】:how to swipe a button onTouch event in android如何在android中滑动按钮onTouch事件
【发布时间】:2013-12-16 08:59:32
【问题描述】:

如何在触摸事件 android 上滑动按钮向左、向右、向上、向下...?...我是 android 新手,想开发一个回调应用程序。请提供一些链接帮助

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

这是我的 imageButton 在触摸事件上移动的代码。但是这段代码没有按我的意愿工作

【问题讨论】:

  • 请澄清您的问题。 “滑动”是人们用手指在触摸屏上做出的一种手势。
  • 您的意思是滑动时移动按钮吗?
  • 是的,我想将滑动按钮向左然后向右移动
  • @XylemRaj 了解 GestureDetector

标签: android touch-event ontouchlistener


【解决方案1】:

您将在此处找到有关如何处理触摸事件的说明:

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

那么,在on touch方法中,你需要

  • 检测用户何时放下手指
  • 存储手指的位置
  • 检测用户何时移动
  • 与之前的位置比较
  • 检测用户何时停止触摸屏幕

示例(使用来自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;
}

在这个简单的案例中,您甚至不需要存储用户何时放下或放下手指(因为移动意味着手指向下),但存储布尔值会很有用)

祝你的应用好运

编辑:

您似乎用一些代码编辑了您的帖子。你说你没有得到预期的结果,但你没有说你得到了什么。这可能有助于我们为您提供帮助。

您应该尝试查找检测滑动动作的库是否已经存在。我很确定那里有很多

编辑 2: 我假设你的按钮是一个简单的 android.Button。一种解决方案可能是创建一个扩展 Button 的类(例如:MySwipableButton)。在您的 xml 中,您创建一个包含 MySwipableButton 的布局,并为其提供足够的移动位置(例如,它具有 width=fill_parent,因为您希望它在 while 屏幕上滑动)。 MySwipableButton 实现 onTouch 来存储按钮应该在的位置(使用你已经拥有的方法) MySwipableButton 也会覆盖onDraw(Graphics g)。在 onDraw 中,您将在它必须位于的位置(关于当前滑动)绘制按钮 (super.draw()),并将视图的其余部分留空

【讨论】:

  • 我想在左右两个方向上移动一个简单的按钮,但我无法以我想要的方式移动它
  • 好吧,你还是别告诉我你现在得到了什么。您的按钮是否移动,但不是以您想要的方式移动,或者什么也没有发生?无论如何,我编辑了我的帖子,建议实现一个可滑动的按钮
【解决方案2】:

检查我有shown the drag drop image onTouch event 的博客。

Edit:这里是完整代码:

首先在你的eclipse中创建android项目并创建一个类和布局文件如下:

MainActivity.java

public class MainActivity extends Activity
{
private ImageView m_ivImage, m_ivImage1;
private int m_counter = 0;
float m_lastTouchX, m_lastTouchY, m_posX, m_posY, m_prevX, m_prevY, m_imgXB, m_imgYB, m_imgXC, m_imgYC, m_dx, m_dy;
private LinearLayout m_llTop;
private AbsoluteLayout m_alTop;
private Button m_btnAddView, m_btnRemove;
private Context m_context;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);

m_context = this;

m_prevX = 0;
m_prevY = 0;
m_imgXB = 50;
m_imgYB = 100;
m_imgXC = 150;
m_imgYC = 100;

m_ivImage = (ImageView) findViewById(R.id.ivImage);
m_ivImage1 = (ImageView) findViewById(R.id.ivImage1);
m_llTop = (LinearLayout) findViewById(R.id.llTop);
m_alTop = (AbsoluteLayout) findViewById(R.id.alTop);
m_btnAddView = (Button) findViewById(R.id.btnAdd);
m_btnRemove = (Button) findViewById(R.id.btnRemove);

m_ivImage.setOnTouchListener(m_onTouchListener);
m_ivImage1.setOnTouchListener(m_onTouchListener);
m_btnAddView.setOnClickListener(m_onClickListener);
m_btnRemove.setOnClickListener(m_onClickListener);

}

OnClickListener m_onClickListener = new OnClickListener(){

@Override
public void onClick(View p_v)
{
switch (p_v.getId())
{
case R.id.btnAdd:
addView();
break;
case R.id.btnRemove:
removeView();
break;
default:
break;
}
}
};

OnTouchListener m_onTouchListener = new OnTouchListener(){

@Override
public boolean onTouch(View p_v, MotionEvent p_event)
{
switch (p_event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
m_lastTouchX = p_event.getX();
m_lastTouchY = p_event.getY();
break;
}
case MotionEvent.ACTION_UP:
{
break;
}

case MotionEvent.ACTION_MOVE:
{
m_dx = p_event.getX() - m_lastTouchX;
m_dy = p_event.getY() - m_lastTouchY;

m_posX = m_prevX + m_dx;
m_posY = m_prevY + m_dy;

if (m_posX > 0 && m_posY > 0 && (m_posX + p_v.getWidth()) < m_alTop.getWidth() && (m_posY + p_v.getHeight()) < m_alTop.getHeight())
{
p_v.setLayoutParams(new AbsoluteLayout.LayoutParams(p_v.getMeasuredWidth(), p_v.getMeasuredHeight(), (int) m_posX, (int) m_posY));

m_prevX = m_posX;
m_prevY = m_posY;

}

break;
}
}
return true;
}
};

/**
* Add view dynamically for drag and drop
*/
private void addView()
{
ImageView m_img = new ImageView(m_context);
TextView m_tv=new TextView(m_context);
if (m_counter < 5)
{
if (m_counter % 2 == 0)
{
m_img.setBackgroundResource(R.drawable.bol_green);
m_tv.setText("Hello! Drag Me! ");
m_alTop.addView(m_tv, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXB), ((int) m_imgYB)));
m_alTop.addView(m_img, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXB), ((int) m_imgYB)));
}
else
{
m_img.setBackgroundResource(R.drawable.bol_paars);
m_alTop.addView(m_img, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXC), ((int) m_imgYC)));
}
m_counter++;
if (m_counter == 5)
m_btnAddView.setEnabled(false);
}

m_img.setOnTouchListener(m_onTouchListener);
m_tv.setOnTouchListener(m_onTouchListener);
}

public void removeView()
{
m_counter = 0;
m_alTop.removeAllViews();
m_alTop.invalidate();
m_btnAddView.setEnabled(true);
}
}

main_layout.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/llTop" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" > <AbsoluteLayout 
    android:id="@+id/alTop" android:layout_width="fill_parent" 
    android:layout_height="0dp" android:layout_margin="10dp" 
    android:layout_weight=".70" > <ImageView android:id="@+id/ivImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:layout_marginTop="10dp" 
    android:src="@drawable/ic_menu_share" android:visibility="gone" /> 
<ImageView android:id="@+id/ivImage1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp" android:layout_x="193dp" 
    android:layout_y="29dp" android:src="@drawable/ic_launcher" /> 
</AbsoluteLayout> <LinearLayout android:layout_width="wrap_content" 
    android:layout_height="0dp" android:layout_gravity="center" 
    android:layout_weight=".30" > <Button android:id="@+id/btnAdd" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:layout_marginTop="10dp" 
    android:text="Add View" /> <Button android:id="@+id/btnRemove" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:layout_marginTop="10dp" 
    android:text="Remove View" android:visibility="visible" /> 
</LinearLayout> </LinearLayout>

这是边界文件 xml 代码,它显示了对象移动的布局的边界。

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
        <stroke android:width="1dp" android:color="#000000"/>
        <corners 
            android:topLeftRadius="8dp" android:topRightRadius="8dp" 
            android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp"/> 
        <padding android:left="0dp" android:top="0dp" android:right="0dp" 
            android:bottom="0dp"/> <gradient android:angle="270" 
            android:endColor="#FFFFFF" android:startColor="#FFFFFF" 
            android:type="linear" android:centerColor="#FFFFFF"/>
</shape>
    </item> 
</selector>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多