第一种

  

第二种

  

第一种实现方法

xml布局

<RelativeLayout 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" > <Button
android:id="@+id/startBtn"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerInParent="true"
android:background="@drawable/addstock" /> </RelativeLayout>

Activity调用

        DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels; // Toast.makeText(getActivity(), screenWidth + "==" + screenHeight + "="
// + vHeight, 0).show(); // 拖动的按钮
btn = (Button) view.findViewById(R.id.startBtn);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (bool) {
LatestNetDataPackage lnPackage = new LatestNetDataPackage(R.string.COMMAND_LATESTNET, fundKind, sortType,
requestType, curPage, FUND_COUNT, );
dataPackage = lnPackage;
NetWorkTaskManager.addRequestToRequestCache(lnPackage, HListViewFragment.this, HListViewFragment.this,
getActivity());
btn.setBackgroundResource(R.drawable.deletestock);
bool = false;
} else {
LatestNetDataPackage lnPackage = new LatestNetDataPackage(R.string.COMMAND_LATESTNET, fundKind, sortType,
requestType, curPage, FUND_COUNT, );
dataPackage = lnPackage;
NetWorkTaskManager.addRequestToRequestCache(lnPackage, HListViewFragment.this, HListViewFragment.this,
getActivity());
btn.setBackgroundResource(R.drawable.addstock);
bool = true;
}
}
}); btn.setOnTouchListener(new OnTouchListener() {
int lastX, lastY; // 记录移动的最后的位置
private int btnHeight; public boolean onTouch(View v, MotionEvent event) {
// 获取Action
int ea = event.getAction();
switch (ea) {
case MotionEvent.ACTION_DOWN: // 按下
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
screenWidth = view.getWidth();
screenHeight = view.getHeight();
btnHeight = btn.getHeight();
// Toast.makeText(getActivity(), "ACTION_DOWN:" + lastX + ",
// " + lastY, 0).show();
break;
case MotionEvent.ACTION_MOVE: // 移动
// 移动中动态设置位置
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
int left = v.getLeft() + dx;
int top = v.getTop() + dy;
int right = v.getRight() + dx;
int bottom = v.getBottom() + dy;
if (left < ) {
left = ;
right = left + v.getWidth();
}
if (right > screenWidth) {
right = screenWidth;
left = right - v.getWidth();
}
if (top < ) {
top = ;
bottom = top + v.getHeight();
}
if (bottom > screenHeight) {
bottom = screenHeight;
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom);
// Toast.makeText(getActivity(), "position:" + left + ", " +
// top + ", " + right + ", " + bottom, 0)
// .show();
// 将当前的位置再次设置
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP: // 抬起
// 向四周吸附
// int dx1 = (int) event.getRawX() - lastX;
// int dy1 = (int) event.getRawY() - lastY;
// int left1 = v.getLeft() + dx1;
// int top1 = v.getTop() + dy1;
// int right1 = v.getRight() + dx1;
// int bottom1 = v.getBottom() + dy1;
// if (left1 < (screenWidth / 2)) {
// if (top1 < 100) {
// v.layout(left1, 0, right1, btnHeight);
// } else if (bottom1 > (screenHeight - 200)) {
// v.layout(left1, (screenHeight - btnHeight), right1, screenHeight);
// } else {
// v.layout(0, top1, btnHeight, bottom1);
// }
// } else {
// if (top1 < 100) {
// v.layout(left1, 0, right1, btnHeight);
// } else if (bottom1 > (screenHeight - 200)) {
// v.layout(left1, (screenHeight - btnHeight), right1, screenHeight);
// } else {
// v.layout((screenWidth - btnHeight), top1, screenWidth, bottom1);
// }
// }
// break;
}
return false;
}
});

view指的是布局

第二种实现方法

xml布局

<com.ui.view.DragFrameLayout
android:id="@+id/becausefloat"
android:layout_width="fill_parent"
android:layout_height="0.0dp"
android:layout_gravity="center_vertical"
android:layout_weight="" > <FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="" /> <ImageView
android:id="@+id/dragImg"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/deletestock" />
</com.ui.view.DragFrameLayout>

自定义控件 com.ui.view.DragFrameLayout

package com.ui.view;

import com.ui.R;

import android.content.Context;
import android.graphics.Rect;
import android.location.Location;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.FrameLayout; public class DragFrameLayout extends FrameLayout { private View view; private int width, heigh; private int screenWid, screenHei; private boolean isClickDrag = false; private boolean isTouchDrag = false; private float startX, startY; private CheckClick checkClick = new CheckClick(); private DragImageClickListener dragImageListener; public DragImageClickListener getDragImageListener() {
return dragImageListener;
} public void setDragImageListener(DragImageClickListener dragImageListener) {
this.dragImageListener = dragImageListener;
} public interface DragImageClickListener {
public abstract void onClick();
} private class CheckClick implements Runnable { @Override
public void run() {
// TODO Auto-generated method stub
isClickDrag = false;
Log.i("drag", "=====checkTap====");
} } public DragFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
} public DragFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
} public void dragInit(View view) {
screenWid = getWidth();
screenHei = getHeight();
width = view.getWidth();
heigh = view.getHeight();
} @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
switch (ev.getAction()) { case MotionEvent.ACTION_DOWN:
float x = ev.getX();
float y = ev.getY();
Rect frame = new Rect();
if (view == null) {
view = findViewById(R.id.dragImg);
dragInit(view);
}
view.getHitRect(frame);
if (frame.contains((int) (x), (int) (y))) { isTouchDrag = true;
startX = x;
startY = y;
return true;
}
break; }
return false;
} @Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) { super.onLayout(changed, left, top, right, bottom); } @Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float x = event.getX();
float y = event.getY();
Rect frame = new Rect(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: view.getHitRect(frame);
if (frame.contains((int) (x), (int) (y))) {
startX = x;
startY = y;
isTouchDrag = true;
isClickDrag = true;
postDelayed(checkClick, ViewConfiguration.getTapTimeout());
}
break;
case MotionEvent.ACTION_MOVE: float distanX = Math.abs(x - startX);
float distanY = Math.abs(y - startY); if (Math.sqrt(distanY * distanY + distanX * distanX) > ) {
isClickDrag = false;
}
move(x, y);
break; case MotionEvent.ACTION_CANCEL:
isClickDrag = false;
isTouchDrag = false;
break;
case MotionEvent.ACTION_UP:
if (isClickDrag == true) {
dragImageListener.onClick();
removeCallbacks(checkClick);
}
isClickDrag = false;
isTouchDrag = false; // 这段是把控件吸附四周
// if (x > width && x < screenWid - width && y > heigh
// && y < screenHei - heigh) {
// int minType = minDIstance(x, y);
// Log.i("tags", screenHei + "==mintype=" + minType);
// switch (minType) {
// case LEFT:
// x = width;
// break;
// case RIGHT:
// x = screenWid - width;
// break;
// case TOP:
// y = heigh;
// break;
// case BOTTOM:
// y = screenHei - heigh;
// break;
// default:
// break;
// }
// move(x, y);
// }
break;
case MotionEvent.ACTION_OUTSIDE:
isClickDrag = false;
isTouchDrag = false;
break;
}
return true;
} private final static int LEFT = ;
private final static int RIGHT = ;
private final static int TOP = ;
private final static int BOTTOM = ; private int minDIstance(float x, float y) {
Log.i("tags", "x=" + x + "==y=" + y);
boolean left, top; if (x <= (screenWid - x)) {
left = true;
} else {
left = false;
}
if (y <= (screenHei - y)) {
top = true;
} else {
top = false;
} if(left&&top){
if(x<=y){
return LEFT;
}else{
return TOP;
}
}
if(left&&(!top)){
if(x<=(screenHei-y)){
return LEFT;
}else{
return BOTTOM;
}
} if((!left)&top){
if((screenWid-x)<= y){
return RIGHT;
}else{
return TOP;
}
} if((!left)&(!top)){
if((screenWid-x)<= (screenHei-y)){
return RIGHT;
}else{
return BOTTOM;
}
}
return ; } private void move(float x, float y) {
int left = (int) (x - width / );
int top = (int) (y - heigh / );
if (left <= )
left = ;
if (top <= )
top = ; if (left > screenWid - width)
left = screenWid - width;
if (top > screenHei - heigh)
top = screenHei - heigh; FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view
.getLayoutParams(); params.setMargins(left, top, (screenWid - left - width), (screenHei
- top - heigh)); view.setLayoutParams(params);
requestLayout();
} public double getDistance(double lat1, double lon1, double lat2, double lon2) {
float[] results = new float[];
Location.distanceBetween(lat1, lon1, lat2, lon2, results);
return results[];
} }

Activity调用方法

        dragImg = (ImageView)findViewById(R.id.dragImg);
frameLayout = (DragFrameLayout)findViewById(R.id.becausefloat);
frameLayout.setDragImageListener(new DragImageClickListener() { private boolean isDaix; @Override
public void onClick() {
// TODO Auto-generated method stub
if (isDaix) {
dragImg.setBackgroundResource(R.drawable.deletestock);
isDaix = false;
} else {
dragImg.setBackgroundResource(R.drawable.addstock);
isDaix = true;
}
Toast.makeText(MainActivity.this, "点击",
Toast.LENGTH_LONG).show();
}
});

Android 超简单的拖动按钮 悬浮按钮 吸附按钮的更多相关文章

  1. android 超简单的拖动按钮 悬浮按钮 吸附按钮 浮动按钮

    第一种    第二种    第一种实现方法 xml布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/re ...

  2. vue中超简单的方法实现点击一个按钮出现弹框,点击弹框外关闭弹框

    效果图展示: View层 <template> <div> <div class="mask" v-if="showModal" ...

  3. Android超简单气泡效果

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680最近有用到水下气泡上升效果,因此在网上查了一下资料,结果还真找到了 ...

  4. Android | 超简单集成HMS ML Kit实现最大脸微笑抓拍

    前言   如果大家对HMS ML Kit 人脸检测功能有所了解,相信已经动手调用我们提供的接口编写自己的APP啦.目前就有小伙伴在调用接口的过程中反馈,不太清楚HMS ML Kit 文档中的MLMax ...

  5. android 拖拽图片&拖动浮动按钮到处跑

    来自老外: 拖拽图片效果 方法一: 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  6. 如何为 Drupal 7 网站添加悬浮的反馈按钮?

    最近有客户咨询我们要怎么为 Drupal 网站添加悬浮按钮,方便访客能够链接到反馈表单页面.很幸运,使用 Feedback Simple 模块可以很容易实现. 在这篇短教程中,我将和大家分享如何添加链 ...

  7. 打造支持apk下载和html5缓存的 IIS(配合一个超简单的android APP使用)具体解释

    为什么要做这个看起来不靠谱的东西呢? 由于刚学android开发,还不能非常好的熟练控制android界面的编辑和操作,所以我的一个急着要的运用就改为html5版本号了,反正这个运用也是须要从serv ...

  8. 【Android自己定义View实战】之自己定义超简单SearchView搜索框

    [Android自己定义View实战]之自己定义超简单SearchView搜索框 这篇文章是对之前文章的翻新,至于为什么我要又一次改动这篇文章?原因例如以下 1.有人举报我抄袭,原文链接:http:/ ...

  9. 使用 JavaScript 中的 document 对象查找 HTML 元素,实现“登录”按钮的高亮特效 鼠标悬浮于“登录”按钮时,按钮高亮显示;

    查看本章节 查看作业目录 需求说明: 使用 JavaScript 中的 document 对象查找 HTML 元素,实现"登录"按钮的高亮特效 鼠标悬浮于"登录" ...

随机推荐

  1. 截止JDK1.8版本,java并发框架支持锁包括?

    读写锁 自旋锁 乐观锁

  2. Spring入门第一课

    今天先不讲Spring是什么. Spring网址:http://projects.spring.io/spring-framework/ Eclipse 安装开发IDE 在Eclipse Market ...

  3. C语言函数调用的底层机制

    由这个文章引入吧(百度文库:http://wenku.baidu.com/link?url=aAm1tBg4okqIyFAmfgrJBHdPDrri5LUEKrJjn-dNITds5lwSm550DT ...

  4. Unity中限制轴向移动范围Mathf.Clamp

    Mathf.Clamp   在游戏中,为了限制玩家的某一轴向的移动不超过一定的范围,可以用Mathf.Clamp来解决 Mathf.Clamp(float value,float min,float ...

  5. cinder 服务启动与请求流程源码分析

    文章以ocata版本进行分析 cinder   api 的创建和启动,和 nova   api 类似,都是通过在 api-paste.ini 中定义 app ,然后将 app 加载之后,启动 wsgi ...

  6. 51nod 1154【DP】

    区间DP大暴力吧?GG. dp[ i ] 为字符至 i 的最少数量. 如果[Left , Right]是回文串, dp[Right] = min(dp[ Right ] , dp[Left-1] + ...

  7. 小R的棋子

    小R的棋子(dp) 数轴上有 n 个位置可以摆放棋子,标号为1,2,3...n.小 R 现在要在一些位置摆放棋子,每个位置最多摆放一个棋子,摆放棋子的总数没有限制.小 R 不希望他摆放的棋子过于拥挤, ...

  8. Oracle表连接(转)

    表之间的连接 Join是一种试图将两个表结合在一起的谓词,一次只能连接2个表,表连接也可以被称为表关联.在后面的叙述中,我们将会使用”row source”来代替”表”,因为使用row source更 ...

  9. Nacos-spring-samples解析

    小白们在看这个用例的时候得注意,这个东东不知道他是为了让大家能够快速体验还是怎么的, 反正我一开始没整明白,有点想当然的去理解了: 我一直以为这个Nacos-spring-samples只是一个简单的 ...

  10. Vue中全局导入和按需导入的区别

    export {router} //按需导出 import {router} from './router' //按需导入路由模块 export default //全局导出store模块 store ...