控件的拖动,使用到一个监听事件

setOnTouchListener:
XML代码:
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <ImageView
android:id="@+id/textview"
android:src="@drawable/ic_launcher_foreground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:text="www.baidu.com"/> </LinearLayout>

JAVA代码


 package com.example.a11658.textviewdrag;

 import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView; public class MainActivity extends AppCompatActivity {
private ImageView tv;
private int x;
private int y; @SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tv = findViewById(R.id.textview);
tv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//手指按下
//获取第一次接触屏幕
x = (int) event.getRawX();
y = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
//手指拖动
//获取距离差
int dx = (int) event.getRawX() - x;
int dy = (int) event.getRawY() - y;
//更改Imageview在窗体的位置
v.layout(v.getLeft() + dx, v.getTop() + dy,
v.getRight() + dx, v.getBottom() + dy);
//获取移动后的位置
x = (int) event.getRawX();
y = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
//手指弹起
x = (int) event.getRawX();
y = (int) event.getRawY();
break;
}
return true;
}
});
}
}

view.layout(left, top, right, bottom)实现拖动。

1.按下图搞懂几个坐标

视图宽度 view.getWidth();

视图高度 view.getHeight() 

橘色线:view.getLeft()

蓝色线:view.getRight()

红色线:view.getTop()

粉色线:view.getBottom()

上下左右的偏移都是相对于(0.0)来说

Demo地址:https://github.com/1165863642/WidgetDrag

Android 简单实现控件的拖动的更多相关文章

  1. android 简单的控件前端代码

    /Hello_word/res/layout/activity_main.xml Graphical  Layout/activity_fullsreen.xml(layout/) 代码与设置界面互换 ...

  2. Android Design Support控件之DrawerLayout简单使用

    DrawerLayout能够让我们在项目中非常方便地实现側滑菜单效果.如今主流的应用如QQ等都 採用的这样的效果. 这两天也是在学习Android Design Support的相关知识.网上有关这方 ...

  3. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  4. Android其它新控件 (转)

    原文出处:http://blog.csdn.net/lavor_zl/article/details/51312715 Android其它新控件是指非Android大版本更新时提出的新控件,也非谷歌I ...

  5. 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用

    Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...

  6. 【风马一族_Android】第4章Android常用基本控件

    第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...

  7. Android两个控件叠在一起,如何让被挡住的控件显示出来

    Android两个控件叠在一起,如何让被挡住的控件显示出来 问题 : 两个控件叠在一起,如何让被挡住的控件显示出来? 比如A,B两个控件,A被B挡住,目前A要显示出来,B不能被隐藏,A的高度只有那么一 ...

  8. Android中ExpandableListView控件基本使用

    本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...

  9. [开源]在iOS上实现Android风格的控件Toast

    [开源]在iOS上实现Android风格的控件Toast iOS的风格和Apple其他产品一样,简单而粗暴.没有给人其他选择的余地,让你又爱又恨.同样的,Apple对待iOS平台的开发人员和对待大众消 ...

随机推荐

  1. [Java]LeetCode133. 克隆图 | Clone Graph

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  2. [Swift]LeetCode563. 二叉树的坡度 | Binary Tree Tilt

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  3. [Swift]LeetCode888. 公平的糖果交换 | Fair Candy Swap

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...

  4. Java破解图片防盗链

    前言 今天想下载几张好看的壁纸,然后发现了一张是自己比较喜欢的额.然后点进去看看,WOW!好多好看的壁纸呀,于是我就起了贪念.哈哈!想把他们这一组图片打包下载,小白的方法就是一张一张下载,那样对于我们 ...

  5. dataframe去除null、NaN和空字符串

    去除null.NaN 去除 dataframe 中的 null . NaN 有方法 drop ,用 dataframe.na 找出带有 null. NaN 的行,用 drop 删除行: import ...

  6. Python内置函数(58)——slice

    英文文档: class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set ...

  7. http缓存与离线缓存

    一.http协议实现缓存 1. 缓存头部 通用缓存.条件缓存.缓存控制三大类 头部名称 说明 请求/响应 通用缓存头部 控制客户端是否向服务器发送请求或者是服务端响应请求   cache-contro ...

  8. [linux]为阿里云ECS(CentOS7)配置IPv6地址

    环境为:ECS"经典网络"类型 步骤: 1. 编辑 /etc/sysctl.conf 文件,将其中三条禁用IPv6的设置更改为: net.ipv6.conf.all.disable ...

  9. ADO.NET中COMMAND对象的ExecuteNonQuery、ExcuteReader和ExecuteScalar方法

    1.ExecuteNonQuery方法.该方法执行更新操作,即与UPDATE.INSERT.DELETE等语句有关的操作,在这种情况下,返回值是命令影响的行数.对其他语句,如SET或CREATE,则返 ...

  10. 给vs2015添加EF

    今天做EF的小例子时,发现需要添加实体数据模型,但是不管怎么找在新建项中都找不到这个选项,这是怎么回事,于是就开始百度吧,有的说可能是VS安装时没有全选,也有的人说可能是重装VS时,没有将注册表清除, ...