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

时间:2014-08-25 21:58:32

标签: android rounding screens

我在Android Wear项目中实现了onApplyWindowInsets回调。在该方法中,它调用isRound()来确定代码是否在圆形或矩形Wear Smart智能手表上运行。从圆形磨损仿真器调用此回调。 (虽然isRound()返回false,这是错误的。)但问题是onApplyWindowInsets回调永远不会从我的Samsung Gear Live调用。因此,如果从onApplyWindowInsets中膨胀视图,则视图不会膨胀。有没有人得到应用程序窗口设置来触发真实设备?似乎无法保证。如果是这样,那么如何根据屏幕类型膨胀不同的视图?

4 个答案:

答案 0 :(得分:2)

在您的布局中,您可以设置特定的" sub"圆形/矩形屏幕的布局:

 <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>

如果你对rect_layout和round_layout使用不同的id,你可以在onCreate上检查它,如下所示:

@Override
public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.main_activity);
    WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mRectBackground = (RelativeLayout) findViewById(R.id.rect_layout);
            mRoundBackground = (RelativeLayout) findViewById(R.id.round_layout);
        }
    });
}

mRectBackground或mRoundBackground都将为非null,而不是两者。文档:https://developer.android.com/training/wearables/apps/layouts.html#UiLibrary

其他人尝试使用setOnApplyWindowInsetsListener的策略,如下所示:https://github.com/xamarin/monodroid-samples/blob/master/wear/GridViewPager/GridViewPager/MainActivity.cs。它对我不起作用。

答案 1 :(得分:2)

onApplyWindowInsets实际上要求开发人员在侦听器中调用WatchViewStub的{​​{1}}方法。例如:

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; } }); 实现。

onApplyWindowInsets的文档声明:“侦听器可以可选调用参数View的onApplyWindowInsets方法,以将View的正常行为应用为其自身的一部分。”

答案 2 :(得分:0)

谢谢你!你聪明的技术似乎解决了这个问题。 (我说“似乎”,因为在将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上检测下巴的更好方法?

更新:刚刚在Moto 360上运行此代码并且此技术未检测到圆形屏幕。所以现在我只是检测高度是否等于宽度。但是,当更多Android Wear设备出现时,这对于未来来说是一种糟糕的技术。所以问题仍然存在,如何确定那些真正的Android Wear设备上的圆形屏幕?

答案 3 :(得分:0)

非官方的方式(不使用WatchViewStub) - 但对我来说这更容易。 https://github.com/tajchert/ShapeWear

只需复制ShapeWear.java类,订阅屏幕形状检测事件setOnShapeChangeListener()或调用方法ShapeWear.isRound()(可以抛出错误,形状尚未确定)或ShapeWear. getShape() - 哪个可以导致ShapeWear.SHAPE_UNSURE处于同样的情况。