如何在Android Wear-OnApplyWindowInsetts上检测圆形屏幕设备上未调用

如何在Android Wear-OnApplyWindowInsetts上检测圆形屏幕设备上未调用,android,rounding,screens,Android,Rounding,Screens,我已经在Android Wear项目中实现了onApplyWindowInsets回调。在该方法中,它调用isRound()来确定代码是在圆形还是矩形手表上运行。此回调是从round wear emulator调用的。(虽然isRound()返回false,这是错误的。)但问题是我的Samsung Gear Live从未调用onApplyWindowInsets回调。因此,如果从OnApplyWindowInset中展开视图,视图不会膨胀。有没有人能在真正的设备上使用ApplyWindowIns

我已经在Android Wear项目中实现了onApplyWindowInsets回调。在该方法中,它调用isRound()来确定代码是在圆形还是矩形手表上运行。此回调是从round wear emulator调用的。(虽然isRound()返回false,这是错误的。)但问题是我的Samsung Gear Live从未调用onApplyWindowInsets回调。因此,如果从OnApplyWindowInset中展开视图,视图不会膨胀。有没有人能在真正的设备上使用ApplyWindowInsert?这似乎不能保证。如果是这样,那么如何根据屏幕类型对不同视图进行充气?

在布局中,您可以为圆形/矩形屏幕设置特定的“子”布局:

 <FrameLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      <android.support.wearable.view.WatchViewStub
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/stub"
          app:rectLayout="@layout/rect_layout"
          app:roundLayout="@layout/round_layout"/>
 </FrameLayout>
mRectBackground或mRoundBackground将不为null,而不是两者都为null。此处的文档:


其他人尝试使用setOnApplyWindowInsetsListener的策略,如下所示:。它对我不起作用。

谢谢你,艾德!你的聪明技术似乎解决了这个问题。(我说“似乎”,因为在将mRoundBackground设置为非空值的真实设备上测试代码之前,我无法确定。)

现在有三种Android Wear形状(rect/square、round w/chin和full round),我添加了两个全局值来查找屏幕形状,然后修改回调,如下所示:

   stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            //mRectBackground = (RelativeLayout) findViewById(R.id.LinearLayout1);
            mRoundBackground = (RelativeLayout) findViewById(R.id.LinearLayout2);
            if (mRoundBackground != null) round = true;
            else round = false;

            WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            Display display = window.getDefaultDisplay();
            //Added android.graphics.Point to imports for this next section
            Point point = new Point (0,0);
            display.getSize(point);
            scrnHeight = point.y;
            scrnWidth  = point.x;
            if (round && scrnWidth == scrnHeight) chin = false;
            else chin = true;
            //round = true; //For testing on the emulator!!!!!!
       }
    });
有人知道一种更好的检测Moto360下巴的方法吗


更新:刚刚在Moto360上运行了这段代码,这项技术没有检测到圆形屏幕。所以现在我只是检测高度是否等于宽度。但当更多的安卓穿戴设备问世时,这对未来来说是一项糟糕的技术。因此,问题仍然存在,如何在现有的真正Android Wear设备上确定圆形屏幕?

onApplyWindowInsets
实际上需要开发人员在侦听器中调用
WatchViewStub
onApplyWindowInsets
方法。例如:

WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {

    @Override
    public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
        //////////////////////////////////////////////////
        // IMPORTANT - the following line is required
        stub.onApplyWindowInsets(windowInsets);

        // Set the instance variable to say whether this is round...
        mIsRound = windowInsets.isRound();

        return windowInsets;
    }
});
虽然乍一看可能并不直观,但这允许开发人员根据自己的选择覆盖applyWindowInsets的默认实现

View.onapplyWindowInsertsListener的文档说明:“监听器可以选择调用参数视图的onapplyWindowInserts方法,将视图的正常行为作为其自身行为的一部分。”

非官方方式(不使用WatchViewStub)——但对我来说,这更简单。


只需复制shapewar.java类,并订阅屏幕形状检测事件
setOnShapeChangeListener()
或调用方法
shapewar.isRound()
(如果形状尚未确定,则可能抛出错误)或
shapewar。getShape()
-在相同的情况下,这可能会导致
ShapeWear.SHAPE\u不确定

谢谢!你的聪明技术似乎解决了这个问题。(我说“似乎”,因为在将mRoundBackground设置为非空值的真实设备上测试代码之前,我无法确定)。moto360是否报告其屏幕高度为320?不要使用“高度等于宽度”之类的测试,因为至少G手表(具有方形屏幕)是这样的。不幸的是,我无法扩展插入性,因为我的主类已经扩展了FragmentActivity。我尝试了海的方法作为一个内部类,但也没有运气。到目前为止,我已经尝试了至少12种方法,但在真正的设备上检测不到圆形或方形屏幕。(ONLAYOUTPLATED方法始终返回正方形,即使在Moto 360圆形屏幕上也是如此。onApplyWindowInsets回调从不触发…等等)有人知道一种方法可以在实际设备上检测形状并膨胀正确的布局,而不仅仅是在模拟器中?我已经做了几个星期了,仍然没有答案。在一台真正的Moto 360上进行了测试,但这不起作用:(.isRound()总是返回false,无论设备是什么。上面的答案在12月11日编辑过,现在可以用了。10月25日WillT和11月15日HyLian的评论对使用插入的上一个答案有效(10月9日发布)(已弃用)。感谢您的反馈。既然您正在复制粘贴您的答案,为什么不同时参考其他答案:
WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {

    @Override
    public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
        //////////////////////////////////////////////////
        // IMPORTANT - the following line is required
        stub.onApplyWindowInsets(windowInsets);

        // Set the instance variable to say whether this is round...
        mIsRound = windowInsets.isRound();

        return windowInsets;
    }
});