Android拖动控件改变其位置

mainActivity如下:

package cn.dragtest;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnTouchListener{
   private Button mButton;
   private ImageView mImageView;
   private DisplayMetrics displayMetrics;
   private float lastX=0;
   private float lastY=0;
   private int screenWidth=0;
   private int screenHeight=0;
   private int left;
   private int top;
   private int right;
   private int bottom;
   private boolean isFirst=true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
    }
	private void initView() {
         mButton=(Button) findViewById(R.id.button);
         mImageView=(ImageView) findViewById(R.id.imageView);
         mButton.setOnTouchListener(this);
         mImageView.setOnTouchListener(this);
	}
	public boolean onTouch(View view, MotionEvent event) {
		if (isFirst) {
			// 得到屏幕的宽
			displayMetrics = getResources().getDisplayMetrics();
			screenWidth = displayMetrics.widthPixels;
			// 得到标题栏和状态栏的高度
			Rect rect = new Rect();
			Window window = getWindow();
			mImageView.getWindowVisibleDisplayFrame(rect);
			int statusBarHeight = rect.top;
			int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
			int titleBarHeight = contentViewTop - statusBarHeight;
			// 得到屏幕的高
			screenHeight = displayMetrics.heightPixels- (statusBarHeight + titleBarHeight);
			isFirst=false;
		}
		int action=event.getAction();
		switch (action) {
		//按下
		case MotionEvent.ACTION_DOWN:
			//按下处坐标
			lastX=event.getRawX();
			lastY=event.getRawY();
			break;
		//移动
		case MotionEvent.ACTION_MOVE:
			//移动的距离
			float distanceX=event.getRawX()-lastX;
			float distanceY=event.getRawY()-lastY;
			//移动后控件的坐标
			left=(int)(view.getLeft()+distanceX);
			top=(int)(view.getTop()+distanceY);
			right=(int)(view.getRight()+distanceX);
			bottom=(int)(view.getBottom()+distanceY);
			//处理拖出屏幕的情况
			if (left<0) {
				left=0;
				right=view.getWidth();
			}
			if (right>screenWidth) {
				right=screenWidth;
				left=screenWidth-view.getWidth();
			}
			if (top<0) {
				top=0;
				bottom=view.getHeight();
			}
			if (bottom>screenHeight) {
				bottom=screenHeight;
				top=screenHeight-view.getHeight();
			}
			//显示图片
			view.layout(left, top, right, bottom);
			lastX=event.getRawX();
			lastY=event.getRawY();
			break;
		//抬起
		case MotionEvent.ACTION_UP:
			break;
		default:
			break;
		}
		return false;
	}
}

main.xml如下:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
     >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:clickable="true"
     />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="please drag"
        android:clickable="true"
    />
</LinearLayout>

 

时间: 2024-09-20 10:52:42

Android拖动控件改变其位置的相关文章

Android基础控件(EditView、SeekBar等)的使用方法_Android

 android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView的使用方法.在介绍各种控件之前,先简单介绍android UI控件最基本的几种属性: id: id是控件唯一标识符,可通过**findViewById(R.id.*)**操作控件. layout_width:控件宽度,可设置为match_parent(充满父布局,即让父布局决定当前控件的宽度).wrap_

Android重要控件SnackBar使用方法详解_Android

SnackBar是DesignSupportLibrary中的一个重要的控件,用于在界面下面提示一些关键信息,跟Toast不同的地方是SnackBar允许用户向右滑动消除它,同时,也允许在SnackBar中设定一个Action,当用户点击了SnackBar里面的按钮的时候,可以进行一些操作,所以,功能绝对是很强大的.  SnackBar的构造:  // 参数分别是父容器,提示信息,持续时间public static Snackbar make(@NonNull View view, @NonNu

Android基础控件(EditView、SeekBar等)的使用方法

android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView的使用方法.在介绍各种控件之前,先简单介绍android UI控件最基本的几种属性: id: id是控件唯一标识符,可通过**findViewById(R.id.*)**操作控件. layout_width:控件宽度,可设置为match_parent(充满父布局,即让父布局决定当前控件的宽度).wrap_c

笔记 - Android - 3.控件属性

Android:控件     <TextView/>:普通文本标签          <EditText/>:输入框         > android:hint:在输入框控件中起提示作用:         > android:lines:限定输入框的可视行数         > android:maxLength:限定输入框中的可输入字符长度:         > android:inputType:指定输入框的类型             >>

动态添加-如何添加控件到指定位置。

问题描述 如何添加控件到指定位置. 假如现在有一个button,点击这个button增加一个地址信息,并添加到这个button的上面.如何实现.求大神指教!!! 解决方案 可以先放一个TextView上去设置为隐藏 android:visibility="gone" ,点击后将此TextView设置为显示"visible"~~ 解决方案二: 点击监听里 直接setText("path"), 解决方案三: Button点击后打开对话框,对话框接受数

关于android videoView控件重绘的问题

问题描述 关于android videoView控件重绘的问题 我引用了vitamio的一个框架,使用其中的videoView写了一个视频播放器, 它的机制是在布局文件中声明一个videoView控件,然后在代码中使用findbyid得到控件之后 去操作这个videoview,具体代码我会发图. 现在遇到的问题是当我在打开另一个activity再关闭那个activity之后(带有videoview的activity执行了onpuse方法)会出现videoview重置的问题(播放时间清零,缓存清零

Android评分控件RatingBar使用实例解析_Android

无论游戏,应用,网站,都少不了评分控件.在Android SDK 中提供了 RatingBar控件来实现相应的工作. <RatingBar/>标签有几个常用评分相关属性 android:numStars,指定评分五角星数. android:rating,指定当前分数 android:stepSize, 指定分数增量 <RatingBar/>还有3种 常用的style属性 默认style 就是ratingBarStyle style ratingBarStyleIndicator 不

Android Shape控件美化实现代码_Android

如果你对Android系统自带的UI控件感觉不够满意,可以尝试下自定义控件,我们就以Button为例,很早以前Android123就写到过Android Button按钮控件美化方法里面提到了xml的selector构造.当然除了使用drawable这样的图片外今天Android开发网谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape.gradient.stroke.corners等.  我们就以目前系统的Button的selector为例说下: <s

Android GridView控件自定义

虽然Android已自带了GridView,但是,却不够灵活,同时也不能自由添加控件,因此,本人通过需要进一步封装,来实现Android自定义GridView控件,达到自己需要的效果. 我们看一下最终所需要的效果图: 说明: 上图:这里先省去分页,只讲:Android GridView 控件实现自定义. 按照上面的图例需求,大致上可以把Android GridView 画成如下的方式: 思路如下: 默认将我们的组合控件设置为Orientation 是VERTICAL. 首先一行五个,那么一行以一