美文网首页
ViewPager2 无限循环 自动播放(修改切换动画,切换速度

ViewPager2 无限循环 自动播放(修改切换动画,切换速度

作者: 没有了遇见 | 来源:发表于2021-08-18 10:47 被阅读0次

    实现原因:

    ViewPager2虽然修改了vp的一些弊端,比如 ViewPager2的切换时间以及切换的动画,且ViewPager2被final 修饰不能被继承.所以就试着修改一下切换的动画 以及切换的时间以此记录

    device.gif

    实现难点:
    A:自动播放
    B:无限播放
    C:切换动画

    1:自动播放 Timer Handler 定时切换页面

        Timer  timer = Timer()       
        var timerTask = object : TimerTask() {
            override fun run() {
                runOnUiThread {
                  // 切换 页面以及处理 切换的速度
                    MyPagerHelper.setCurrentItem(binding!!.vpBg, count++, 300)
                }
            }
        }
        timer.schedule(timerTask, 500, 2000)      
    

    2:切换速度以及切换时长

      import android.animation.Animator;
      import android.animation.ValueAnimator;
      import android.view.animation.AccelerateDecelerateInterpolator;
    
      import androidx.viewpager2.widget.ViewPager2;
    
    /**
     * @author wkq
     * @date 2021年08月17日 15:07
     * @des
     */
    
    public class MyPagerHelper {
    /**
     * 保存前一个animatedValue
     */
    private static int previousValue;
    
    /**
     * 设置当前Item
     * @param pager    viewpager2
     * @param item     下一个跳转的item
     * @param duration scroll时长
     */
    public static void setCurrentItem(final ViewPager2 pager, int item, long duration) {
        previousValue = 0;
        int currentItem = pager.getCurrentItem();
        int pagePxWidth = pager.getWidth();
        int pxToDrag = pagePxWidth * (item - currentItem);
        final ValueAnimator animator = ValueAnimator.ofInt(0, pxToDrag);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int currentValue = (int) animation.getAnimatedValue();
                float currentPxToDrag = (float) (currentValue - previousValue);
                pager.fakeDragBy(-currentPxToDrag);
                previousValue = currentValue;
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                pager.beginFakeDrag();
            }
    
            @Override
            public void onAnimationEnd(Animator animation) {
                pager.endFakeDrag();
            }
    
            @Override
            public void onAnimationCancel(Animator animation) { }
    
            @Override
            public void onAnimationRepeat(Animator animation) { }
        });
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }
    

    }

    3:切换动画

        //设置切换动画
    private fun transformer(
    ) {
        //设置多个果冻动画 CompositePageTransformer
        val compositePageTransformer = CompositePageTransformer()
        // 叠加 渐变
        compositePageTransformer.addTransformer(ViewPagerOverlay())
        //偏移
        //        compositePageTransformer.addTransformer(MarginPageTransformer(100))
        binding!!.vpBg.setPageTransformer(compositePageTransformer)
    
    }
    

    PageTransformer:ViewPager2 的切换动画

    import android.view.View
    import androidx.viewpager2.widget.ViewPager2
    import kotlin.math.abs
    private const val MIN_SCALE = 1f
    
    class ViewPagerOverlay : ViewPager2.PageTransformer {
    
    override fun transformPage(view: View, position: Float) {
        val pageWidth = view.width
    
        when {
            position < -1 -> { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.alpha = 0f
            }
            position <= 0 -> { // [-1,0]
                // Use the default slide transition when moving to the left page
                view.alpha = 1F
                view.translationX = 0f
                view.scaleX = 1f
                view.scaleY = 1f
            }
            position <= 1 -> { // (0,1]
                // Fade the page out.
                view.alpha = 1 - position
    
                // Counteract the default slide transition
                view.translationX = pageWidth * -position
    
                // Scale the page down (between MIN_SCALE and 1)
                val scaleFactor: Float = (MIN_SCALE
                        + (1 - MIN_SCALE) * (1 - abs(position)))
                view.scaleX = scaleFactor
                view.scaleY = scaleFactor
            }
            else -> { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.alpha = 0f
            }
        }
    }
    

    }

    4: 无限播放 Int.MAX_VALUE 方案

    class ZrBgAdapter(mContext: Context) : KtAdapter<String>(mContext) {
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        var viewDatabinding= DataBindingUtil.inflate<ItemZrBgBinding>(LayoutInflater.from(mContext), R.layout.item_zr_bg,parent,false)
    
        var viewHolder= DataBindingViewHolder(viewDatabinding.root)
        viewHolder.binding=viewDatabinding
        return viewHolder
    }
    
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    
        var viewHolder=holder as DataBindingViewHolder
        var binding=viewHolder.binding as ItemZrBgBinding
        Glide.with(mContext).load(getItem(position%3)).into(binding.iv)
    
    }
    
    override fun getItemCount(): Int {
        return Int.MAX_VALUE
    }
    

    }

    项目地址: https://gitee.com/wkqoschina/ZrDemo/tree/Timer

    相关文章

      网友评论

          本文标题:ViewPager2 无限循环 自动播放(修改切换动画,切换速度

          本文链接:https://www.haomeiwen.com/subject/nudibltx.html