长图片自动循环滚动效果 (仿小红书)

code小生,一个专注 Android 领域的技术平台

作者:丶ZHI念
链接:https://www.jianshu.com/p/48a89e678d6c
声明:本文已获丶ZHI念授权发表,转发等请联系原作者授权

  • 先看小红书的效果

640
小红书.GIF
  • 说说思路

滚动效果用RecyclerView实现。RecyclerView有个smoothScrollToPosition方法,可以滚动到指定位置(有滚动效果,不是直接到指定位置),不了解的看这里RecycleView4种定位滚动方式演示。每一个Item是一张长图,这样首尾相接滚动起来(滚到无限远)就是无限循环的效果,然后再改变滚动的速度,完成。

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        mRecyclerView = findViewById(R.id.mRecyclerView);
        mRecyclerView.setAdapter(new SplashAdapter(MainActivity.this));
        mRecyclerView.setLayoutManager(new ScollLinearLayoutManager(MainActivity.this));

        //smoothScrollToPosition滚动到某个位置(有滚动效果)
        mRecyclerView.smoothScrollToPosition(Integer.MAX_VALUE / 2);
    }
}
  • 无限循环

将RecyclerView的Item数量设置成很大的值,用smoothScrollToPosition方法滚到很远的位置,就能实现这样的效果,很多banner轮播图的实现也是如此;

public class SplashAdapter extends RecyclerView.Adapter<SplashAdapter.ViewHolder{

    private int imgWidth;


    public SplashAdapter(Context context) {
        imgWidth = EasyUtil.getScreenWidth(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_splash, parent, false);
        return new ViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
       /* ViewGroup.LayoutParams lp = holder.item_bg.getLayoutParams();
        lp.width = imgWidth;
        lp.height =imgWidth*5;
        holder.item_bg.setLayoutParams(lp);*/

    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView item_bg;

        public ViewHolder(final View itemView) {
            super(itemView);
            item_bg = itemView.findViewById(R.id.item_bg);
        }

    }

}
  • 控制smoothScrollToPosition的滑动速度

参考RecyclerView调用smoothScrollToPosition() 控制滑动速度,修改MILLISECONDS_PER_INCH的值即可

/**
 * 更改RecyclerView滚动的速度
 */

public class ScollLinearLayoutManager extends LinearLayoutManager {
    private float MILLISECONDS_PER_INCH = 25f;  //修改可以改变数据,越大速度越慢
    private Context contxt;

    public ScollLinearLayoutManager(Context context) {
        super(context);
        this.contxt = context;
    }


    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        LinearSmoothScroller linearSmoothScroller =
                new LinearSmoothScroller(recyclerView.getContext()) {
                    @Override
                    public PointF computeScrollVectorForPosition(int targetPosition) {
                        return ScollLinearLayoutManager.this
                                .computeScrollVectorForPosition(targetPosition);
                    }

                    @Override
                    protected float calculateSpeedPerPixel
                            (DisplayMetrics displayMetrics) 
{
                        return MILLISECONDS_PER_INCH / displayMetrics.density;
                        //返回滑动一个pixel需要多少毫秒
                    }

                };
        linearSmoothScroller.setTargetPosition(position);
        startSmoothScroll(linearSmoothScroller);
    }

    //可以用来设置速度
    public void setSpeedSlow(float x) {
        //自己在这里用density去乘,希望不同分辨率设备上滑动速度相同
        //0.3f是自己估摸的一个值,可以根据不同需求自己修改
        MILLISECONDS_PER_INCH = contxt.getResources().getDisplayMetrics().density * 0.3f + (x);
    }

}
  • 图片宽度充满屏幕、高度按图片原始宽高比例自适应

@SuppressLint("AppCompatCustomView")
public class FitImageView extends ImageView {

    public FitImageView(Context context) {
        super(context);
    }

    public FitImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        Drawable drawable = getDrawable();

        if(drawable!=null){
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = (int) Math.ceil((float) width * (float) drawable.getIntrinsicHeight() / (float) drawable.getIntrinsicWidth());
            setMeasuredDimension(width, height);
        }else{
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

}
  • 这里需要注意的是、Item的根布局android:layout_height="wrap_content",否则图片高度会受限

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <com.next.scrollimagedemo.view.FitImageView
        android:id="@+id/item_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/ww1" />



    <!--  <ImageView
          android:id="@+id/item_bg"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:src="@mipmap/ww2"
          android:scaleType="centerCrop"/>-->


</android.support.constraint.ConstraintLayout>
  • 使RecyclerView不能手指触碰滑动

加层View屏蔽掉事件就好了

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#80000000"
        android:clickable="true" />


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_marginTop="80dp"
        app:layout_constraintTop_toTopOf="parent"
        android:scaleType="centerInside"
        android:src="@mipmap/slogan"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />


</android.support.constraint.ConstraintLayout>
  • 完成效果

640
WW.GIF
  • Demo

github:https://github.com/forvv231/EasyScollImage

apk:https://fir.im/gfdj

  • By the way

不要吐槽这背景图片、不喜欢、Demo代码里面还有别的( ̄▽ ̄)~*

640

分享技术我是认真的

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值