ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 通过Touch移动按钮

2019-07-25 12:27:21  阅读:226  来源: 互联网

标签:android touch android-button


我想把我的按钮移动到另一个触摸的位置.我的意思是,如果用户触摸一个按钮并通过触摸转到另一个地方,则按钮会移动.我为其中一个按钮编写了这个代码,但是当我触摸一个按钮时,三个按钮移动以及一个按钮无法向右或向左移动,当我移动到屏幕顶部时,图像按钮的一部分被删除!

问题是什么?我该如何解决这个问题?我想在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>

谢谢….

解决方法:

创建一个实现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;
}

}

现在在您的Main活动中,在您的视图上设置OnTouchListener …这是您的imageButton或imageView;

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

标签:android,touch,android-button
来源: https://codeday.me/bug/20190725/1532857.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有