如何在Android Studio上制作旋转菜单?

时间:2019-01-23 21:00:46

标签: java android android-recyclerview rotation android-viewpager

我正在尝试编写儿童应用程序的旋转菜单。这样做的目的是让孩子们在不同的场景之间切换,每个场景都包含不同的动物。

动物表现在圆形表面上,当孩子们向右或向左滑动时,地球旋转,旧动物逐渐消失,新动物逐渐消失,就像这样:

image1

我想要达到的结果类似于iOS iCarousel滚轮效果。

pages rotating as if on a wheel

Toxic Bakery的ViewPagerTransforms库(https://github.com/ToxicBakery/ViewPagerTransforms)具有一种非常类似于Rotate Down的效果,但是我无法根据需要进行调整。页面可以自行滚动,而不是沿公共轴旋转。

我也尝试过CursorWheelLayout(https://github.com/BCsl/CursorWheelLayout),但是由于图像而导致许多性能问题,导致应用崩溃。

我最近的尝试是SpinMenu(https://github.com/Hitomis/SpinMenu),这很棒。片段确实会在公共轴上旋转,但只有在缩小时才旋转。我还没有想办法让它改变页面(就像ViewPager那样),而片段却在视图中旋转。

是否有什么建议可达到预期效果?

1 个答案:

答案 0 :(得分:0)

这是我找到的解决方案:

1)实现CarouselView库:https://gtomato.github.io/carouselview/

2)随着CarouselView从RecyclerView扩展,请创建CarouselView的布局以进行充气(我称之为carouselview_item):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

3)随着CarouselView从RecyclerView扩展,创建一个CarouselViewAdapter从RecyclerView.Adapter扩展

public class CarouselViewAdapter extends RecyclerView.Adapter<CarouselViewAdapter.ViewHolder> {

    private List<Fragment> list;

    public CarouselViewAdapter(List<Fragment> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public CarouselViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.carouselview_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CarouselViewAdapter.ViewHolder holder, int position) {
        Fragment fragment = list.get(position);
        holder.bind(object);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    // In case you are using, let's say, RxJava for getting the list
    public void setObjects(List<Fragment> list) {
        if (list.size() == 0) {
            this. = list;
        } else {
            this.list.addAll(list);
            notifyDataSetChanged();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private T example;

        public ViewHolder(View itemView) {
            super(itemView);
            // Find your views:
            // example = itemView.findViewById(R.id.example);
        }

        public void bind(T object){
            // Bind the info you need to the view:
            // example.setInfo(object.getInfo)
            }
        }
    }
}

4)对活动进行设置,包括所需的Transformer和适配器:

public class MainActivity extends AppCompatActivity {

    private CarouselView carouselView;
    private List<Fragment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initAssets();

        Fragment object1 = new T(/* necessary parameters, if any*/);
        Fragment object2 = new T(/* necessary parameters, if any*/);
        Fragment object3 = new T(/* necessary parameters, if any*/);

        list.add(object1);
        list.add(object2);
        list.add(object3);

        carouselView.setTransformer(new WheelViewTransformer());
        carouselView.setAdapter(new CarouselViewAdapter(list));
    }

    private void initAssets() {
        carouselView = findViewById(R.id.carouselview);
        list = new ArrayList<>();
    }
}

它像这样对我有用,尽管由于我现在使用的图像而导致性能不佳。

享受!