代码之家  ›  专栏  ›  技术社区  ›  Sterling

在Android Wear中编程确定屏幕形状

  •  8
  • Sterling  · 技术社区  · 12 年前

    我正在寻找一种在Java中确定Android Wear设备屏幕是圆形还是矩形的技术。注意,这不仅仅是布局;我的代码实际上需要知道它使用的是哪个形状,因为它们的处理方式不同。

    就我在网上看到的代码示例来看,两种不同的方法应该是可行的——但我一直无法让它们中的任何一种发挥作用。我将在这里包括它们,以从运行中消除它们,或进行可能的故障排除(如果有人可以看到它们的问题)。请不要让我参考另一篇SO帖子,它只是重复了那些对我不起作用的解决方案。

    注意,这里的所有代码都在手表上运行。此外,我仍在使用Eclipse,FWIW。

    我见过的最简单的方法是添加 onApplyWindowInsets() 收听我布局中的视图。所以我创建了一个如下的监听器:

    @Override
    public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
        if (insets.isRound()) {
         displayShape = "round";
        } else {
         displayShape = "rectangular";
        }
        return null;
    }
    

    并将其添加到布局的根视图中,代码如下:

    view.setOnApplyWindowInsetsListener(this);
    

    在我的onCreate()方法中。就目前来看,看起来还可以——但听众永远不会被打电话。我还发现有人建议我需要手动调用它,比如:

    view.requestApplyInsets();
    

    但这似乎没有什么区别。我尝试过将它放在不同的视图、不同的生命周期方法等等,但从未见过它在我的应用程序中被调用。这是在我的LG G手表BTW上运行的。

    第二种方法有点像黑客,它基于 WatchViewStub helper类。我跳过重重困难,将可穿戴支持库导入到Eclipse项目中,然后将以下内容添加到我的根布局中:

    <android.support.wearable.view.WatchViewStub 
        android:id="@+id/watch_view_stub"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:rectLayout="@layout/rect"
        app:roundLayout="@layout/round"
        />
    

    并创建了 rect.xml 像这样的:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/layout_type"
        android:text="rectangular"
        />
    

    和 round.xml 这样地:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/layout_type"
        android:text="round"
        />
    

    最后,在我的 onCreate() 我添加了以下Java代码:

        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                TextView layoutType = (TextView) findViewById(R.id.layout_type);
                displayShape = layoutType.getText().toString();
            }
        });
    

    这是一条很长的路,但它应该会奏效,对吗?没那么多。。。 displayShape 始终设置为 "rectangular" ,表示它总是 矩形.xml 即使在圆形仿真器上运行时,也会使用它。[我还没有圆屏硬件可以试穿。]

    那么,有人看到我在这两种方法中的哪一种上出了问题吗?或者你能提出第三种实际可行的方法吗?

    4 回复  |  直到 12 年前
        1
  •  13
  •   Sterling    12 年前

    经过几天的努力,我终于找到了答案。事实证明 android:theme 应用程序在清单中的位置。

    为了 WatchViewStub 要使用正确的矩形/圆形布局,您的应用程序 必须 使用 @android:style/Theme.DeviceDefault 作为其主题。下面是一个示例:

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault">
    

    我希望如果你使用继承自 DeviceDefault 尽管我还没有测试过。但如果您使用任何其他自定义主题, 监视视图存根 将无法正常工作。

    @WaynePiekarski,如果能在这里记录下来就好了。

    此外,这里还有一些我在学习过程中学到的其他技巧:

    1. 矩形布局总是在圆形布局之前膨胀;IOW,在圆形设备上,您将获得两个 onLayoutInflated() 回调。如果您正在使用布局膨胀将屏幕形状添加到Java代码中,这是一种痛苦,这是必要的,因为。。。

    2. 使命感 setOnApplyWindowInsetsListener() 在…上 监视视图存根 根本无法加载圆形布局(至少在我的测试中)。因此,如果您尝试使用此回调来确定屏幕形状,圆形设备仍将获得方形布局。

    最后,一个额外的问题: Android Wear不只是报告其屏幕形状作为资源限定符,有什么好的理由吗? 你知道,就像 -land , -large 等等。我们到底为什么要到处乱搞 监视视图存根 完全

        2
  •  3
  •   Steve White    6 年前

    我不确定为什么在您的情况下需要使用回调,但屏幕形状的一般问题的答案如下: Is there any way to detect if the clock is round?

    即,获取当前上下文,并测试

    context.getResources().getConfiguration().isScreenRound()
    
        3
  •  2
  •   rana_sadam    11 年前

    在CanvasWatchFaceService.Engine中,它们是一个overcode方法,可用于setOnApplyWindowInsets,您可以检查insets是圆形还是方形

    @Override
        public void onApplyWindowInsets(WindowInsets insets) {
            super.onApplyWindowInsets(insets);
            if(insets.isRound()){
              //round
            } 
            else{
              //square 
            }
    
        }
    
        4
  •  1
  •   bf2020    12 年前

    https://plus.google.com/+NicolasPomepuy/posts/ZJ3KZK6uu2e#+NicolasPomepuy/posts/ZJ3KZK6uu2e

    和来自 https://github.com/PomepuyN/WatchviewStubIssue/blob/bcad0de7fa473c757dc27f9dfe65e31561c6097f/wear/src/main/java/com/example/watchviewstubissue/ViewService.java

            mainView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { 
    51             @Override 
    52             public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { 
    53                 if (insets.isRound()) { 
    54                     Log.d("ViewService", "Round"); 
    55                 } else { 
    56                     Log.d("ViewService", "Square"); 
    57                 } 
    58                 return insets; 
    59             } 
    60         }); 
    

    你的代码和他的代码之间的区别是,你返回的是null,而他返回的是insets。