简单实现护目镜功能

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,代码简短把,自己慢慢定制吧

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值