如何旋转ImageView和旋转木马

时间:2019-04-25 15:11:56

标签: android kotlin imageview carousel

我正在使用库gtomato/carouselview来创建轮播。轮播本身已经完成并且正在工作。但是我在旋转木马旁边有一个ImageView,我希望它随旋转木马一起旋转/变换。而是顺时针,逆时针旋转或停止,它应该跟随转盘。

轮播看起来像这样:

enter image description here

随着旋转,图像应随之旋转。

我没有运气就尝试了以下方法。图像顺时针旋转约25度,仅此而已。即使我逆时针旋转转盘,图像也会顺时针旋转约25度。当我“旋转”旋转木马以使其连续旋转时,也是如此。

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        var v = inflater.inflate(R.layout.fragment_carousel, container, false)

        carousel = v.findViewById(R.id.carousel)
        cylinder = v.findViewById(R.id.cylinder)

        carousel?.transformer = WheelViewTransformer()
        carousel?.adapter = CarouselAdapter()
        carousel?.isInfinite = true
        carousel?.gravity = Gravity.CENTER_HORIZONTAL

        carousel?.setOnScrollListener(object: CarouselView.OnScrollListener() {
            override fun onScrollBegin(carouselView: CarouselView?) {

                super.onScrollBegin(carouselView)

                var scroll = carouselView?.currentOffset
                var rotPercentage = scroll!! / carouselView?.adapter!!.itemCount

                var r = cylinder?.rotation

                cylinder?.animate()!!.rotationBy(((2 * Math.PI -rotPercentage)).toFloat())
            }
        })

        return v
}

使用iOS和iCarousel,我可以执行以下操作,并且可以正常运行:

func carouselDidScroll(_ carousel: iCarousel) {
    let scroll: CGFloat = carousel.scrollOffset
    let rotPercentage = scroll / CGFloat(carousel.numberOfVisibleItems)
    _cylinderImage.transform = CGAffineTransform(rotationAngle: 2 * .pi * -rotPercentage) 
}

1 个答案:

答案 0 :(得分:0)

下面的以下库效果更好。随附了一种将背景图像添加到轮播的方法。

https://github.com/LukeDeighton/WheelView