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

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

1. 无限循环

将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 =imgWidth5;
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);
}

}

}

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;
}

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

}

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

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

}

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”>

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

结尾

  • 腾讯T4级别Android架构技术脑图;查漏补缺,体系化深入学习提升

img

  • 一线互联网Android面试题含详解(初级到高级专题)

这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率

img

有Android开发3-5年基础,希望突破瓶颈,成为架构师的小伙伴,可以关注我

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

g-3xbvacYl-1713597662370)]

有Android开发3-5年基础,希望突破瓶颈,成为架构师的小伙伴,可以关注我

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值