Wear上创建圆形视图

Wear上创建圆形视图

本文介绍了如何在Android Wear上创建圆形视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在android wear 2.0中创建圆形手表的圆形列表?

How can I create circular list for round watch as in android wear 2.0 ?.

像这样:

在Android Wear应用启动器中看到了循环列表.

Circular list is seen in android wear app launcher.

推荐答案

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

First of all you need to replace your ListView with a WearableRecyclerView.It can be used like a normal ListView. But make sure, you import the right one from android.support.wear.widget. DON'T use the one from android.support.wearable.view. This one should be crossed out, so it won't take you long to check if you're using the right one. If there's just one WearableRecyclerViewto choose from, make sure to add compile 'com.android.support:wear:27.0.0' to the dependencies in your build.gradle (wear) file.Also make sure that you're using <android.support.wear.widget.WearableRecyclerView/> in your activity.xml. If you just want a circular ListView without any custom item-scaling, just call this in your onLayoutInflated() method:

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

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

If you want to make items scaling up when they get closer to the center of your screen, things get a little more complicated.

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

First: paste this in your 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()方法,然后键入以下内容:

Then go back to your onLayoutInflated() method, and type the following:

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

完成.

这篇关于如何在Android Wear上创建圆形视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:47