【问题标题】:How to create Circular view on android wear?如何在 android wear 上创建圆形视图?
【发布时间】:2016-07-19 08:49:45
【问题描述】:

如何像在 android wear 2.0 中一样为圆形手表创建圆形列表?

像这样:

在 android wear 应用启动器中可以看到循环列表。

【问题讨论】:

标签: android listview android-wear-2.0


【解决方案1】:

首先,您需要将 ListView 替换为 WearableRecyclerView。 它可以像普通的 ListView 一样使用。但请确保您从android.support.wear.widget导入正确的不要使用来自android.support.wearable.view 的那个。这个应该被划掉,所以你不会花很长时间来检查你是否使用了正确的。如果只有一个 WearableRecyclerView 可供选择,请确保将 compile 'com.android.support:wear:27.0.0' 添加到 build.gradle (wear) 文件中的依赖项中。 还要确保您在 activity.xml 中使用了<android.support.wear.widget.WearableRecyclerView/>。如果您只想要一个没有任何自定义项目缩放的圆形 ListView,只需在您的 onLayoutInflated() 方法中调用它:

your_recyclerview.setEdgeItemsCenteringEnabled(true);
your_recyclerview.setLayoutManager(new WearableLinearLayoutManager(your_activity_context));

如果您想让项目在靠近屏幕中心时按比例放大,事情会变得有点复杂。

首先:将其粘贴到您的 Activity.java 中:

private class CustomScrollingLayoutCallback extends WearableLinearLayoutManager.LayoutCallback {

        private static final float MAX_ICON_PROGRESS = 2F;

        @Override
        public void onLayoutFinished(View child, RecyclerView parent) {

            float centerOffset = ((float) child.getHeight() / 2.0f) / (float) parent.getHeight();
            float yRelativeToCenterOffset = (child.getY() / parent.getHeight()) + centerOffset;

            float progresstoCenter = (float) Math.sin(yRelativeToCenterOffset * Math.PI);

            float mProgressToCenter = Math.abs(0.5f - yRelativeToCenterOffset);

            mProgressToCenter = Math.min(mProgressToCenter, MAX_ICON_PROGRESS);

            child.setScaleX(1 - mProgressToCenter);
            child.setScaleY(1 - mProgressToCenter);
            child.setX(+(1 - progresstoCenter) * 100);
        }

    }

然后返回到您的 onLayoutInflated() 方法,并输入以下内容:

    CustomScrollingLayoutCallback customScrollingLayoutCallback = new CustomScrollingLayoutCallback();
    your_recycler_view.setLayoutManager(new WearableLinearLayoutManager(your_context, customScrollingLayoutCallback));
    your_recycler_view.setCircularScrollingGestureEnabled(true);

完成。

【讨论】:

  • 在我的情况下,如果我上下滚动,中间图标大小不会增加大小。我该如何解决这个问题?
【解决方案2】:

现在可以使用 Android Wear 2.0 的WearableRecyclerView

根据Android Developer Docs

Wear 2.0 引入了WearableRecyclerView 类,用于显示和 操作为圆形显示优化的垂直项目列表。 WearableRecyclerView 将现有的 RecyclerView 类扩展为 在可穿戴设备中提供弯曲布局和圆形滚动手势 应用程序。

您可能想了解更多关于Android Wear 2.0 Preview 3的信息。

【讨论】:

  • 不赞成这个答案的人,请至少发表你的推理。这不是“仅链接”的答案。它引用了链接中的相关文本。如果您有更好的答案,请不要隐藏在反对票后面,而是将其作为答案发布。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 2020-12-24
  • 2017-01-23
  • 2019-11-14
相关资源
最近更新 更多