欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-05 13:51:42
效果图如下 第一步:新建dragview继承relativelayout package com.rong.activity; import com....

效果图如下

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>

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