通过触摸移动按钮 [英] Moving buttons via Touch

查看:151
本文介绍了通过触摸移动按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的按钮,另一个订货量大的触摸。我的意思是,如果用户轻触一个按钮,并通过触摸,按键移动到另一个地方。我写这篇$ C $下的按钮中的一个,但是当我触摸一个按钮,三粒扣动作,以及和一个按钮不能移动到右侧或左侧,当我移动到屏幕的上方,图像按钮的一部分删除!

I want to move my buttons to another palce with touch. I mean if user touch one button and go to another place via touch, the button moves. I write this code for one of buttons, but when i touch one button, three button moves as well as and one button cannot move to right or left and when i move to top of screen, a part of image button deleted!

这是什么问题?我怎么能解决这个问题?我要运行这个程序的Andr​​oid +2.2

What is the problem? and How can i solve this? I want run this program in android +2.2

onButton.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            switch(event.getAction()){
                case MotionEvent.ACTION_MOVE:
                    RelativeLayout.LayoutParams relativeLayout=(RelativeLayout.LayoutParams) oneButton.getLayoutParams();
                    int x=(int)event.getRawX();
                    int y=(int)event.getRawY();

                    relativeLayout.leftMargin=x-50;
                    relativeLayout.rightMargin=x-50;
                    relativeLayout.topMargin=y-50;
                    relativeLayout.bottomMargin=y-50;
                    oneButton.setLayoutParams(relativeLayout);
                    break;
                    default:
                        break;
            }
            return true;
        }

    });

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView
    android:id="@+id/img"
    android:src="@drawable/root"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"/>
<ImageButton 
    android:id="@+id/btnOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="35dip"
    android:layout_alignParentTop="true"
    android:layout_marginTop="70dip"
    android:src="@drawable/oneButton"
    android:background="@null"
    android:visibility="invisible"/>
<ImageButton 
    android:id="@+id/btnTwo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="35dip"
    android:layout_alignParentTop="true"
    android:layout_marginTop="70dip"
    android:src="@drawable/twoButton"
    android:background="@null"
    android:visibility="invisible"/>
   <ImageButton 
    android:id="@+id/btnThree"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btnOne"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="15dip"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dip"
    android:src="@drawable/ThreeButton"
    android:background="@null"
    android:visibility="invisible"/>
   <ImageButton 
    android:id="@+id/btnFour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btnTwo"
    android:layout_alignParentRight="true"
    android:layout_marginRight="15dip"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dip"
    android:src="@drawable/fourButton"
    android:background="@null"
    android:visibility="invisible"/> 

</RelativeLayout>

谢谢....

Thanks....

推荐答案

创建一个新的类,它实现OnTouchListener

Create a new Class that implements OnTouchListener

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.RelativeLayout;

public class MultiTouchListener implements OnTouchListener
{

private float mPrevX;
private float mPrevY;

public MainActivity mainActivity;
public MultiTouchListener(MainActivity mainActivity1) {
    mainActivity = mainActivity1;
}

@Override
public boolean onTouch(View view, MotionEvent event) {
    float currX,currY;
    int action = event.getAction();
    switch (action ) {
        case MotionEvent.ACTION_DOWN: {

            mPrevX = event.getX();
            mPrevY = event.getY();
            break;
        }

        case MotionEvent.ACTION_MOVE:
        {

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


                MarginLayoutParams marginParams = new MarginLayoutParams(view.getLayoutParams());   
                marginParams.setMargins((int)(currX - mPrevX), (int)(currY - mPrevY),0, 0);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
                view.setLayoutParams(layoutParams); 


            break;
        }



        case MotionEvent.ACTION_CANCEL:
            break;

        case MotionEvent.ACTION_UP:

            break;
    }

    return true;
}

}

现在在你的主要业务,设置OnTouchListener对你的看法...这是你的ImageButton或ImageView的;

Now in your Main activity, set OnTouchListener on your view... that is your imageButton or imageView;

MultiTouchListener touchListener=new MultiTouchListener(this);
onButton.setOnTouchListener(touchListener);

这篇关于通过触摸移动按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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