如何创建具有上一页和下一页预览的 ViewPager,并从预览中缩放居中

Posted

技术标签:

【中文标题】如何创建具有上一页和下一页预览的 ViewPager,并从预览中缩放居中【英文标题】:How can i create ViewPager with previous and next page preview, and centred one zoomed from preview 【发布时间】:2016-05-30 05:56:56 【问题描述】:
viewPager.setClipToPadding(false);
viewPagerMusicCategory.setPadding(width/3, 0, width /3, 0);
viewPagerMusicCategory.setPageMargin(40));

我通过上面的代码获得了下一个和上一个预览。但现在我需要居中页面的缩放效果。

提前致谢

【问题讨论】:

我建议使用 ViewPropertyAnimator 来缩放视图。 例如view.animate().scaleX(1.5).scaleY(1.5).setDuration(250); 您想以交互方式将中心放置在房间上,或者只是在图像到达中心时进行缩放动画 我想放大中间的一个 如何在 setPadding 中设置宽度 【参考方案1】:
viewPagerMusicCategory.setPageTransformer(false, new ViewPager.PageTransformer() 
        @Override
        public void transformPage(View page, float position) 
            Log.e("pos",new Gson().toJson(position));
            if (position < -1) 
                page.setScaleY(0.7f);
                page.setAlpha(1);
             else if (position <= 1) 
                float scaleFactor = Math.max(0.7f, 1 - Math.abs(position - 0.14285715f));
                page.setScaleX(scaleFactor);
                Log.e("scale",new Gson().toJson(scaleFactor));
                page.setScaleY(scaleFactor);
                page.setAlpha(scaleFactor);
             else 
                page.setScaleY(0.7f);
                page.setAlpha(1);
            
        
    
);

....

【讨论】:

【参考方案2】:

recyclerview 中使用它并使用 snap 助手

public class CenterZoomLayoutManager extends LinearLayoutManager 

    private final float mShrinkAmount = 0.15f;
    private final float mShrinkDistance = 0.9f;

    public CenterZoomLayoutManager(Context context) 
        super(context);
    

    public CenterZoomLayoutManager(Context context, int orientation, boolean reverseLayout) 
        super(context, orientation, reverseLayout);
    


    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) 
        int orientation = getOrientation();
        if (orientation == VERTICAL) 
            int scrolled = super.scrollVerticallyBy(dy, recycler, state);
            float midpoint = getHeight() / 2.f;
            float d0 = 0.f;
            float d1 = mShrinkDistance * midpoint;
            float s0 = 1.f;
            float s1 = 1.f - mShrinkAmount;
            for (int i = 0; i < getChildCount(); i++) 
                View child = getChildAt(i);
                float childMidpoint =
                        (getDecoratedBottom(child) + getDecoratedTop(child)) / 2.f;
                float d = Math.min(d1, Math.abs(midpoint - childMidpoint));
                float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
                child.setScaleX(scale);
                child.setScaleY(scale);
            
            return scrolled;
         else 
            return 0;
        
    

    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) 
        int orientation = getOrientation();
        if (orientation == HORIZONTAL) 
            int scrolled = super.scrollHorizontallyBy(dx, recycler, state);

            float midpoint = getWidth() / 2.f;
            float d0 = 0.f;
            float d1 = mShrinkDistance * midpoint;
            float s0 = 1.f;
            float s1 = 1.f - mShrinkAmount;
            for (int i = 0; i < getChildCount(); i++) 
                View child = getChildAt(i);
                float childMidpoint =
                        (getDecoratedRight(child) + getDecoratedLeft(child)) / 2.f;
                float d = Math.min(d1, Math.abs(midpoint - childMidpoint));
                float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
                child.setScaleX(scale);
                child.setScaleY(scale);
            
            return scrolled;
         else 
            return 0;
        

    

recyclerview 中使用它并使用 snap helper

recyclerView.setAdapter(mAdapter);
CenterZoomLayoutManager layoutManager = new CenterZoomLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);

recyclerView.setLayoutManager(layoutManager);
        new LinearSnapHelper().attachToRecyclerView(recyclerView);

recyclerView.setScrollingTouchSlop(  recyclerView.TOUCH_SLOP_PAGING);

【讨论】:

以上是关于如何创建具有上一页和下一页预览的 ViewPager,并从预览中缩放居中的主要内容,如果未能解决你的问题,请参考以下文章

wordpress 如何在子页面中导航上一页和下一页

laravel5.5中添加对分页样式的修改上一页和下一页

php WordPress |自定义上一页和下一页链接

使用session处理用户搜索后数据的上一页和下一页跳转

Laravel 提交按钮不跳转到下一页和上一页

word编辑过程中格式突然变了,上一页和下一页的不一致怎么办?