简单实现护目镜功能

    xiaoxiao2025-08-23  54

    0、前言

    的确,护目镜和云朵护眼这类软件是非常好用的,额,是晚上的时候,今天想做个项目的,但是搞着搞着搞出护目镜那功能来了,醉了,这里也不多说啦。

    1、实现代码

    import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.graphics.PixelFormat; import android.os.IBinder; import android.view.LayoutInflater; import android.view.WindowManager; import android.widget.LinearLayout; public class MyService extends Service { private WindowManager windowManager; private WindowManager.LayoutParams params; public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE)); view(); return super.onStartCommand(intent, flags, startId); } private void view() { params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, PixelFormat.TRANSLUCENT); params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE ; params.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;; params.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;// 透明状态栏 LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setBackgroundColor(Color.argb(200, 89, 89, 89)); windowManager.addView(linearLayout, params); } }

    3、权限及manifest

    在Application标签下,activity之外: 添加权限:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 添加service:<service android:name=".server.MyService"></service> //name那里不要傻傻的不去改啊!!!

    4、PS后记

    上面哪个server直接startservice就行啦,反正直接调用,我不多说,要研究,就研究一下WindowManager windowManager和WindowManager.LayoutParams params里面的width,height,type和flags,代码简短把,自己慢慢定制吧
    转载请注明原文地址: https://ju.6miu.com/read-1301968.html
    最新回复(0)