Android利⽤WindowManager⽣成悬浮按钮及悬浮菜单简介
本⽂模仿实现的是360⼿机卫⼠基础效果,同时后续会补充⼀些WindowManager的原理知识。
整体思路
360⼿机卫⼠的内存球其实就是⼀个没有画⾯的应⽤程序,整个应⽤程序的主体是⼀个Service。我们的程序开始以后,启动⼀个service,同时关闭activity即可:
public class MainActivity extends Activity {
private static final String TAG = SimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
startService(new Intent(this, FloatWindowService.class));
finish();
}
}
import android.os.IBinder;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class FloatWindowService extends Service {
private static final String TAG = SimpleName();
public FloatWindowService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "on start command");
FloatWindowManager.instance(getApplicationContext()).createFloatWindow();
StartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
我们要注意的是,传统的Service默认是运⾏在UI线程中的,这点与封装了⼀个Thread和Handler的intentService不同,所以我们可以直接在Service中更改UI相关的内容。
再来看⼀下FloatWindowManager中的⽅法:
public void createFloatWindow() {
if (isWindowShowing()) return;
WindowManager windowManager = getWindowManger(context);
int screenWidth = DefaultDisplay().getWidth();
int screenHeight = DefaultDisplay().getHeight();
if (floatLayout == null) {
floatLayout = new FloatLayout(context);
if (smallLayoutParams == null) {
smallLayoutParams = new WindowManager.LayoutParams();
smallLayoutParams.format = PixelFormat.RGBA_8888;
smallLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
smallLayoutParams.width = FloatLayout.viewWidth;
smallLayoutParams.height = FloatLayout.viewHeight;
smallLayoutParams.x = screenWidth;
smallLayoutParams.y = screenHeight / 2;
}
}
windowManager.addView(floatLayout,smallLayoutParams);
}
以及⾃定义的View:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:id="@+id/small_layout"
android:background="@drawable/bg_small"
android:orientation="vertical" android:layout_width="60dip"
android:layout_height="25dip">
<TextView
android:layout_width="match_parent"
android:gravity="center"
android:text="悬浮窗"
android layout布局android:layout_height="match_parent" />
</LinearLayout>
public class FloatLayout extends LinearLayout {
public static int viewWidth;
public static int viewHeight;
private WindowManager windowManager;
public FloatLayout(final Context context) {
super(context);
windowManager = (WindowManager) SystemService(Context.WINDOW_SERVICE);
LayoutInflater.from(context).inflate(R.layout.small_layout, this);
View view = findViewById(R.id.small_layout);
viewWidth = LayoutParams().width;
viewHeight = LayoutParams().height;
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
FloatWindowManager.instance(context).createFloatMenu();
return true;
}
});
}
}
⾃定义的View除了加载了⼀个布局,就是设置了⼀个Touch,⽤于点击悬浮窗弹出菜单。注意这⾥要使⽤LayoutParams() 来获取视图的宽和⾼,因为在构造⽅法中,这个View并没有被measure完成,所以采⽤Height得到的宽⾼是0。
创建菜单的⽅法类似,同样通过WindowManager:
public void createFloatMenu() {
if (menuLayout != null) return;
Log.d(TAG, "create float menu");
WindowManager windowManager = getWindowManger(context);
if (menuLayout == null){
menuLayout = new MenuLayout(context);
menuLayoutParams = new WindowManager.LayoutParams();
menuLayoutParams.format = PixelFormat.RGBA_8888;
}
windowManager.addView(menuLayout,menuLayoutParams);
}
⾃定义的菜单将背景设置成半透明,同时分成上下两部分,上部分点击删除菜单,下部分是⼀些展⽰的内容:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#96000000"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/trans_part"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="0dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:layout_height="0dp">
<TextView
android:layout_width="match_parent"
android:text="存放content"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
public class MenuLayout extends LinearLayout {
public MenuLayout(final Context context) {
super(context);
LayoutInflater.from(context).inflate(ansparent_layout,this);
View view = findViewById(ans_part);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FloatWindowManager.instance(context).removeMenuLayout();
}
});
}
}
可以看见,实现悬浮窗,其实就是通过windowManager.addView时,在LayoutParam 的type设置为TYPE_PHONE,这样你的视图就是系统级视图,可以覆盖在全部程序的最上⾯。其余的,更多的是⾃定义View的知识。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。