当前位置: 移动技术网 > IT编程>移动开发>Android > Android使用WindowManager制作一个可拖动的控件

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

六年级上册语文教案,磁县一中,syntpenh.exe是什么进程

效果图如下

第一步:新建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>

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网