如何设置android 背景悬浮窗完全透明
答案:2 悬赏:50 手机版
解决时间 2021-02-23 03:00
- 提问者网友:辞取
- 2021-02-22 04:47
如何设置android 背景悬浮窗完全透明
最佳答案
- 五星知识达人网友:逃夭
- 2021-02-22 05:37
无法设置的,别人都是通过刷机实现的。
全部回答
- 1楼网友:罪歌
- 2021-02-22 06:53
可以根据项目需要改变其相应布局。
package com.zk.me;
import java.util.list;
import android.app.activity;
import android.content.componentname;
import android.content.context;
import android.content.intent;
import android.content.pm.packageinfo;
import android.content.pm.packagemanager;
import android.content.pm.packagemanager.namenotfoundexception;
import android.content.pm.resolveinfo;
import android.os.bundle;
import android.view.gravity;
import android.view.menu;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.window;
import android.view.windowmanager;
import android.widget.imageview;
import android.widget.toast;
public class mainactivity extends activity implements onclicklistener {
private windowmanager windowmanager = null;
private windowmanager.layoutparams windowmanagerparams = null;
private myfloatview floatview = null;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
requestwindowfeature(window.feature_no_title);// 取消标题栏
getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,
windowmanager.layoutparams.flag_fullscreen);// 全屏
setcontentview(r.layout.activity_main);
createview();
}
public void ondestroy() {
super.ondestroy();
// 在程序退出(activity销毁)时销毁悬浮窗口
}
private void createview() {
floatview = new myfloatview(getapplicationcontext());
floatview.setonclicklistener(this);
imageview view = new imageview(this);
view.setimageresource(r.drawable.ic_launcher);
floatview.addview(view); // 这里简单的用自带的icon来做演示
windowmanager = (windowmanager) getapplicationcontext()
.getsystemservice("window");
// 设置layoutparams(全局变量)相关参数
windowmanagerparams = ((floatapplication) getapplication())
.getwindowparams();
windowmanagerparams.type = 2003; // 设置window type
// windowmanagerparams.format = pixelformat.rgba_8888; // 设置图片格式,效果为背景透明
// 设置window flag
windowmanagerparams.flags = 40;
windowmanagerparams.format = 1;
// 调整悬浮窗口至左上角,便于调整坐标
windowmanagerparams.gravity = gravity.left | gravity.top;
// 以屏幕左上角为原点,设置x、y初始值
windowmanagerparams.x = 0;
windowmanagerparams.y = 0;
// 设置悬浮窗口长宽数据
windowmanagerparams.width = 40;
windowmanagerparams.height = 40;
// 显示myfloatview图像
windowmanager.addview(floatview, windowmanagerparams);
}
public void onclick(view v) {
toast.maketext(this, "clicked", toast.length_short).show();
opencld("com.jovian.android.pqgl", getapplicationcontext());
}
public static void opencld(string packagename, context context) {// 打开移动警务应用
packagemanager packagemanager = context.getpackagemanager();
packageinfo pi = null;
try {
pi = packagemanager.getpackageinfo(packagename, 0);
} catch (namenotfoundexception e) {
}
intent resolveintent = new intent(intent.action_main, null);
resolveintent.addcategory(intent.category_launcher);
resolveintent.setpackage(packagename);
list apps = packagemanager.queryintentactivities(
resolveintent, 0);
resolveinfo resolveinfo = apps.iterator().next();
if (resolveinfo != null) {
string classname = resolveinfo.activityinfo.name;
intent intent = new intent(intent.action_main);
intent.addcategory(intent.category_launcher);
intent.setflags(intent.flag_activity_new_task);
componentname cn = new componentname(packagename, classname);
intent.setcomponent(cn);
context.startactivity(intent);
}
}
@override
protected void onrestoreinstancestate(bundle savedinstancestate) {
// todo auto-generated method stub
super.onrestoreinstancestate(savedinstancestate);
}
@override
public boolean oncreateoptionsmenu(menu menu) {
getmenuinflater().inflate(r.menu.activity_main, menu);
return true;
}
}
package com.zk.me;
import android.content.context;
import android.graphics.rect;
import android.util.log;
import android.view.gravity;
import android.view.motionevent;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.windowmanager;
import android.widget.linearlayout;
public class myfloatview extends linearlayout {
private float mtouchx;
private float mtouchy;
private float x;
private float y;
private float mstartx;
private float mstarty;
private onclicklistener mclicklistener;
private windowmanager windowmanager = (windowmanager) getcontext()
.getapplicationcontext().getsystemservice("window");
// 此windowmanagerparams变量为获取的全局变量,用以保存悬浮窗口的属性
private windowmanager.layoutparams windowmanagerparams = ((floatapplication) getcontext()
.getapplicationcontext()).getwindowparams();
public myfloatview(context context) {
super(context);
}
@override
public boolean ontouchevent(motionevent event) {
// 获取到状态栏的高度
rect frame = new rect();
getwindowvisibledisplayframe(frame);
int statusbarheight = frame.top;
system.out.println("statusbarheight:" + statusbarheight);
// 获取相对屏幕的坐标,即以屏幕左上角为原点
x = event.getrawx();
y = event.getrawy() - statusbarheight; // statusbarheight是系统状态栏的高度
log.i("tag", "currx" + x + "====curry" + y);
switch (event.getaction()) {
case motionevent.action_down: // 捕获手指触摸按下动作
// 获取相对view的坐标,即以此view左上角为原点
mtouchx = event.getx();
mtouchy = event.gety();
mstartx = x;
mstarty = y;
log.i("tag", "startx" + mtouchx + "====starty" + mtouchy);
break;
case motionevent.action_move: // 捕获手指触摸移动动作
updateviewposition();
break;
case motionevent.action_up: // 捕获手指触摸离开动作
updateviewposition();
mtouchx = mtouchy = 0;
if ((x - mstartx) < 5 && (y - mstarty) < 5) {
if (mclicklistener != null) {
mclicklistener.onclick(this);
}
}
break;
}
return true;
}
@override
public void setonclicklistener(onclicklistener l) {
this.mclicklistener = l;
}
private void updateviewposition() {
// 更新浮动窗口位置参数
windowmanagerparams.x = (int) (x - mtouchx);
windowmanagerparams.y = (int) (y - mtouchy);
windowmanager.updateviewlayout(this, windowmanagerparams); // 刷新显示
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯