gpt4 book ai didi

android - 如何在android wear上创建圆形 View ?

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:31 27 4
gpt4 key购买 nike

如何像在 android wear 2.0 中那样为圆形 watch 创建循环列表?。

像这样:

enter image description here

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

最佳答案

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

完成。

关于android - 如何在android wear上创建圆形 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38453663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com