Android使用WindowManager制作一个可拖动的控件

 更新时间:2016年08月02日 16:35:38   投稿:lijiao  
这篇文章主要为大家详细介绍了Android使用WindowManager制作一个可拖动的控件的相关资料,感兴趣的小伙伴们可以参考一下

效果图如下

第一步:新建DragView继承RelativeLayout

package com.rong.activity;

import com.rong.test.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

public class DragView extends RelativeLayout {
 private WindowManager windowManager;// 用于可拖动的浮动窗口
 private WindowManager.LayoutParams windowParams;// 浮动窗口的参数
 private Button myButton;

 public DragView(Context context, AttributeSet attrs) {
 super(context, attrs);
 init();
 }

 private void init() {
 View.inflate(getContext(), R.layout.layout_my, this);
 myButton = new Button(getContext());
 myButton.setText("我的");
 myButton.setBackgroundColor(Color.RED);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
 // 获取当前点的xy位置
 int currentX = (int) event.getX();
 int currentY = (int) event.getY();
 switch (event.getAction()) {
 case MotionEvent.ACTION_DOWN:
  if (windowManager == null) {
  setWindowParams(currentX, currentY);
  windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
  windowManager.addView(myButton, windowParams);
  }
  break;
 case MotionEvent.ACTION_MOVE:
  windowParams.x = currentX;
  windowParams.y = currentY;
  windowManager.updateViewLayout(myButton, windowParams);
  break;
 case MotionEvent.ACTION_UP:
  // windowManager.removeView(myButton);
  break;
 }
 return true;
 }

 private void setWindowParams(int x, int y) {
 // 建立item的缩略图
 windowParams = new WindowManager.LayoutParams();
 windowParams.gravity = Gravity.TOP | Gravity.LEFT;// 这个必须加
 // 得到preview左上角相对于屏幕的坐标
 windowParams.x = x;
 windowParams.y = y;
 // 设置宽和高
 windowParams.width = 200;
 windowParams.height = 200;
 windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
  | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
  | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
 windowParams.format = PixelFormat.TRANSLUCENT;
 windowParams.windowAnimations = 0;
 }
}

第二步:新建布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/main_touchlayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ffffff"
  android:orientation="vertical" >

  <com.rong.activity.DragView
    android:id="@+id/main_touchview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:background="#ff0000" />

</RelativeLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android实现淘宝底部图标导航栏

    Android实现淘宝底部图标导航栏

    这篇文章主要为大家详细介绍了Android实现淘宝底部图标导航栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • android 多线程技术应用

    android 多线程技术应用

    能够在屏幕上“实时地显示”时间的流逝,单线程程序是无法实现的,必须要多线程程序才可以实现,即便有些计算机语言可以通过封装好的类实现这一功能,但从本质上讲这些封装好的类就是封装了一个线程,具体祥看本文
    2012-12-12
  • Android教你如何发现APP卡顿的实现

    Android教你如何发现APP卡顿的实现

    这篇文章主要介绍了Android教你如何发现APP卡顿的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Android应用中使用ViewPager和ViewPager指示器来制作Tab标签

    Android应用中使用ViewPager和ViewPager指示器来制作Tab标签

    这篇文章主要介绍了Android中使用ViewPager和ViewPager指示器来制作Tab标签的方法,ViewPager指示器ViewPageIndicator是一个开源库,文中举了一个仿网易新闻客户端Tab标签的例子,需要的朋友可以参考下
    2016-03-03
  • Android系统状态栏定制图标显示逻辑控制

    Android系统状态栏定制图标显示逻辑控制

    这篇文章主要为大家介绍了Android系统状态栏定制图标显示逻辑控制,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Android列表实现(3)_自定义列表适配器思路及实现代码

    Android列表实现(3)_自定义列表适配器思路及实现代码

    Android 自定义列表适配器会提供很多的便利;下面的例子为使用自定义的列表适配器来显示列表,感兴趣的朋友可以研究下
    2012-12-12
  • Android实现IM多人员组合的群组头像

    Android实现IM多人员组合的群组头像

    这篇文章主要为大家详细介绍了Android实现IM多人员组合的群组头像,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • Android进度条ProgressBar的实现代码

    Android进度条ProgressBar的实现代码

    这篇文章主要为大家详细介绍了Android进度条ProgressBar的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • 详解Android JNI的基本使用(CMake)

    详解Android JNI的基本使用(CMake)

    本文介绍了Android JNI的基本使用(CMake),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • 分享一个Android设置圆形图片的特别方法

    分享一个Android设置圆形图片的特别方法

    圆形图片想必是项目开发中也是不少用的一个知识点吧。那么这里学习一下简单的制作圆形图片,这个方法不用于平时的实现方法,有需要的可以参考借鉴。
    2016-09-09

最新评论