《Android-RecyclerView实现封面滑动到指定位置放大》---ViewPager封面指示器

一、实现效果

请添加图片描述

二、关键代码

1、自定义:LinearLayoutManager

指定位置放大item


import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ScaleLayoutManager extends LinearLayoutManager {
   

    private static final float MAX_SCALE = 1.45f; // 最大放大比例
    private static final float MIN_SCALE = 1.0f; // 最小缩放比例
    private static  int MAX_LEFT_DISTANCE = 300; // 距离左边x px 时放大到最大

    private int mWidth;
    private int mHeight;

    public ScaleLayoutManager(Context context) {
   
        super(context, LinearLayoutManager.HORIZONTAL, false);

        // 转换dp到像素
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        MAX_LEFT_DISTANCE = (int) (78 * metrics.density);     // 100dp
        mWidth = (int) (65 * metrics.density);         // 16dp
        mHeight = (int) (74 * metrics.density);  // 200dp
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
   
        super.onLayoutChildren(recycler, state);
        scaleChildViews();
    }

    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
   
        int scrolled = super.scrollHorizontallyBy(dx, recycler, state);
        scaleChildViews();
        return scrolled;
    }

    private void scaleChildViews() {
   

        for (int i = 0; i < getChildCount(); i++) {
   
            View child = getChildAt(i);
            if (child == null) continue;

            float childCenterX = (child.getLeft() + child.getRight()) / 2f;
            float distance = Math.abs(MAX_LEFT_DISTANCE - childCenterX);

            // 计算缩放比例
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值