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;// 浮動窗口的參數(shù)
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) {
// 獲取當(dāng)前點(diǎn)的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左上角相對于屏幕的坐標(biāo)
windowParams.x = x;
windowParams.y = y;
// 設(shè)置寬和高
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)WebView刪除緩存的方法
這篇文章主要介紹了Android實(shí)現(xiàn)WebView刪除緩存的方法,實(shí)例分析了Android針對WebView操作緩存的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Android實(shí)現(xiàn)炫酷的網(wǎng)絡(luò)直播彈幕功能
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)絡(luò)直播彈幕功能的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
iOS開發(fā)中TableView類似QQ分組的折疊與展開效果
這篇文章主要介紹了iOS開發(fā)中TableView類似QQ分組的折疊與展開效果,其實(shí)要做這個效果我先想到的是在tableView中再嵌套多個tableView。下面通過本文給大家分享實(shí)現(xiàn)思路,需要的朋友可以參考下2016-12-12
Android創(chuàng)建文件時出現(xiàn)java.io.IOException:?Operation?not?permitte
最近使用android10創(chuàng)建文件失敗,并拋出權(quán)限異常,這篇文章主要給大家介紹了Android創(chuàng)建文件時出現(xiàn)java.io.IOException:?Operation?not?permitted異常的解決方法,需要的朋友可以參考下2023-05-05
Android?Jetpack組件Navigation導(dǎo)航組件的基本使用
本篇主要簡單介紹了一下?Navigation?是什么?以及使用它的流程是什么,并且結(jié)合實(shí)際案例?操作了一番,Navigation?還有很多其他用法,如條件導(dǎo)航、嵌套圖、過度動畫?等等功能?有機(jī)會再操作,需要的朋友可以參考下2022-06-06
Android studio 運(yùn)行main 函數(shù)的方法
這篇文章主要介紹了Android studio 運(yùn)行main 函數(shù)的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法示例
這篇文章主要介紹了Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法,實(shí)例分析了Android中TextView控件的使用技巧,需要的朋友可以參考下2016-07-07
舉例講解Android中ViewPager中的PagerTitleStrip子控件
這篇文章主要介紹了Android中ViewPager中的PagerTitleStrip子控件使用例子,講解了PagerTitleStrip子控件的嵌入與設(shè)置標(biāo)題的用法,需要的朋友可以參考下2016-03-03

