Android 弧形列表转盘的实现(三),View跟随RecyclerView做旋转动画;

前两篇博客:

Android 弧形转盘的实现(一),弧形列表;

Android 弧形转盘的实现(二),列表自动选中;RecyclerView滑动后自动选中居中的条目,RecyclerView实现WheelView效果;

已经大致实现了弧形转盘的效果,还有一个动画需要做,这个比较简单;

Android 弧形列表转盘的实现(三),View跟随RecyclerView做旋转动画;_第1张图片

效果图左边的刻度轮盘是个半圆,其实应该是个整圆只显示一半,另外一半在屏幕外面;

尝试使用PaddingLift把ImageView挤到屏幕外面,默认效果是可以,但是旋转的时候也是个半圆在旋转,这样就不行了;索性直接设置IamgeView是整个圆,然后做平移动画,移动一半位置到屏幕外面;

1、平移,只显示半圆;

ImageView控件时固定宽高的 400*400的 , 直接X轴移动-200dp;

iv = findViewById(R.id.iv);
setTranslationX(iv , UiUtils.dip2px(this , 200)*-1);
public void setTranslationX(View view , float dy) {
        ObjectAnimator translationYAnimator = ObjectAnimator.ofFloat(view,"translationX",0f,dy);
        translationYAnimator.setDuration(1L);
        translationYAnimator.start();
    }

2、ImageView轮盘跟随RecyclerView滑动做旋转动画;

一定要用float类型 , 不然会丢失精度;

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                float dyy = dy*-1;
                setRotate(dyy/4);
            }
        });
    private float values = 0f;
    private float newValues = 0f;
    private ObjectAnimator rotationAnimator;
    public void setRotate(float dy) {
        if (rotationAnimator == null)
            rotationAnimator = ObjectAnimator.ofFloat(iv,"rotation",0f,10f);
        newValues = values + dy;
        rotationAnimator.setDuration(10L);
        rotationAnimator.setFloatValues(values,newValues);
        rotationAnimator.start();
        values = newValues;
    }

完工,看效果! (没有轮盘图片,就用的Android的默认启动图标;)

图片有点大,加载可能稍微慢一点,GIF图感觉卡卡的,想体验效果还请下载代码自己运行起来看看;

代码已上传:https://github.com/CuiChenbo/ArcSelectList , 欢迎Star

你可能感兴趣的:(Android)