首页 > 其他分享 >DrawerLayout配合WindowManager在service中使用

DrawerLayout配合WindowManager在service中使用

时间:2023-05-16 14:11:59浏览次数:40  
标签:service LayoutParams void WindowManager context DrawerLayout MyWindow public

1.原理理解

抽屉组件依附在WindowManager上,WindowManager大于DrawerLayout,因此DrawerLayout抽出和放回是基于WindowManager已经展示出来的情况。

2.布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/sliding_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00000000"
        android:clickable="false"></FrameLayout>

    <com.test.drawerlayouttest.MyDrawLayout
        android:id="@+id/sliding"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:id="@+id/sliding_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left">

            <ImageView
                android:layout_width="736px"
                android:layout_height="776px"
                android:layout_marginTop="152px"
                android:background="#2D3035" />
            
        </RelativeLayout>

    </com.test.drawerlayouttest.MyDrawLayout>

</RelativeLayout>



3.DrawerLayout类

public class MyDrawLayout extends DrawerLayout {
    private int mGravity;

    public MyDrawLayout(@NonNull Context context) {
        super(context);
    }

    public MyDrawLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mGravity = Gravity.LEFT;
    }

    public MyDrawLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mGravity = Gravity.LEFT;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public synchronized void openDrawer() {
        this.openDrawer(mGravity, true);
    }

    public synchronized void closeDrawer(boolean animate) {
        this.closeDrawer(mGravity, animate);
    }

    public boolean isDrawerOpened() {
        return this.isDrawerOpen(mGravity);
    }

}

4.MyWindow类

public class MyWindow {
    private final String TAG = getClass().getSimpleName();
    private Context mContext;
    public static WindowManager mWindowManager;
    public static WindowManager.LayoutParams lp;
    public static MyWindow mWindow;
    public static View mView;
    public static boolean hasMainView;
    public static MyDrawLayout mSliding;

    public synchronized static MyWindow getInstance(Context context) {
        if (mWindow == null) {
            mWindow = new MyWindow(context);
        }
        return mWindow;
    }

    private MyWindow(Context context) {
        Log.d(TAG, "MyWindow init:begin ");
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.windowlayout, null);
        mSliding = (MyDrawLayout) mView.findViewById(R.id.sliding);
        //默认抽屉打开
        mSliding.openDrawer();
        if (mSliding != null) {
            mSliding.setEnabled(true);
            mSliding.addDrawerListener(new DrawerLayout.DrawerListener() {
                @Override
                public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                }

                @Override
                public void onDrawerOpened(@NonNull View drawerView) {

                }

                @Override
                public void onDrawerClosed(View drawerView) {
                    Log.d(TAG, "onDrawerClosed");
                    //抽屉销毁后,同时关闭mView
                    mWindowManager.removeViewImmediate(mView);
                    hasMainView = false;
                }

                @Override
                public void onDrawerStateChanged(int newState) {

                }
            });
        }
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        lp = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        lp.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        lp.format = PixelFormat.RGBA_8888;
        Log.d(TAG, "MyWindow init:finish ");
    }


}

5.APP类

public class MyApp extends Application {

    private static Context mAppContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = getApplicationContext();
        MyWindow.getInstance(mAppContext);
        Log.d("AvmApp","AvmApp onCreated");
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }
}

6.Service类

public class MyService extends Service {

    public MyService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //显示MyWindow窗口,窗口上包含抽屉组件
        MyWindow.mWindowManager.addView(MyWindow.mView, MyWindow.lp);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

可能会遇到的错误

img

标签:service,LayoutParams,void,WindowManager,context,DrawerLayout,MyWindow,public
From: https://www.cnblogs.com/zybao/p/17405475.html

相关文章

  • Spring 3 & jBPM 5 & LocalTaskService
    帖子地址:[url]https://community.jboss.org/thread/195386[/url]HiGuys,IamalsostrugglinginconfigSpring+LocalHumanTask.Iamusing[color=red][b]Spring3.0,JBPM5.4.0.Final,Drools5.5.0.Final[/b][/color]IcanconfigtouseJTA......
  • springboot 整合webservice 相关说明
    1.环境依赖jdk8,springboot2.3.12.release,cxf版本需要根据springboot版本修改,方法:查看springboot版本的发布日期,然后根据日期找相近的两个版本<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><versi......
  • Microsoft.Exchange.WebServices.Data;
    using Microsoft.Exchange.WebServices.Data;using Microsoft.Identity.Client;using System;using System.Configuration; namespace EwsOAuth{   class Program  {     static async System.Threading.Tasks.Task Main(string[] args) ......
  • Parameter 9 of constructor in com.xxx.impl.xxxServiceImpl required a bean of ty
    1查看Service实现类是否加了@AllArgsConstructor2删除@AllArgsConstructor3给每个要注入的serviceBean加@Resource原因lombok的@AllArgsConstructor注解会代替@Autowired注入,导致某些不需要手动注入的bean重复加了@Autowired......
  • Relationship between services and profile roles of LE Audio
    Exampleoftherelationshipbetweenservicesandprofileroles;theCSIP/CSISprofiles/services, implementationissharedbetweenCaptureandRenderingControlandAudioStreamTransitions,butisdepictedas twoidenticalroles. ......
  • PostMan 調用WebServices 方法説明
    智慧系统接口 1.     webservices接口説明--> http://10.66.101.185/fService/services/CheckSNAndLine.asmx方法--》getCheckResult检查传递SN和线体以及轨道代码检查条码是否正确 参数stringsn条码stringlin......
  • Visual Studio Connected Services 生成http api代码
    生成的代码将和接口对应的参数、返回值一一对应,本文底层使用的工具为NSwag.exe,其他可替代的方案还有AutoSet.exe1.配置连接2.配置生成的代码相关属性3.如果遇到报错考虑将version添加到生成的swagger.json文件中底层实际是调用NSwag.exe生成的代码生成的文件位置此......
  • 微服务架构 & service mesh
    微服务架构,服务治理怎么做,采用什么技术?微服务架构实施服务治理的方法和技术:容器化:Docker+Kubernetes服务注册发现:ZooKeeper、Consul、Etcd负载均衡:HAProxy、Nginx、AmazonELBAPI网关:Kong、ShenYu,中大厂一般自研监控和日志:ELKStack、Prometheus、Grafana分布式跟......
  • 【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
    问题描述在中国区Azure上,使用MediaService服务,想要使用.NET的代码来对上传视频创建缩略图(Thumbnail)。通过官网文档(https://docs.azure.cn/zh-cn/media-services/latest/samples/samples-encoding-reference#create-a-thumbnail-sprite)下载.NET示例,配置appsettings.json......
  • Web Services:Apache XML-RPC
    XML-RPC(http://ws.apache.org/xmlrpc/ )的全称是XML Remote Procedure Call,即XML远 程方法 调 用。是JAVA 实现 的XML-RPC。        这种远程过程调用使用http作为传输协议,XML作为传送信息的编码格式。Xml-Rpc的定义尽XML-RPC(http://ws.apache.org/xml......