写点什么

实现长图片自动循环滚动效果

用户头像
Android架构
关注
发布于: 17 小时前

//全屏 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.setLayout


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


Manager(new ScollLinearLayoutManager(MainActivity.this));


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


1. 无限循环


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


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


private int imgWidth;


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


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


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


@Overridepublic 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);}


}


}


2.控制 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;}


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


@Overrideprotected 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);}


}


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


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


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


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


@Overrideprotected 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);}}


}


4.这里需要注意的是、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.FitImageViewandroid:id="@+id/item_bg"android:layout_width="match_parent"android:layout_height="wrap_content"android:src="@mipmap/ww1" />


<!-- <ImageView

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
实现长图片自动循环滚动效果