public class RollItemDecoration extends RecyclerView.ItemDecoration {
private static final String TAG = "RollItemDecoration";
private int index;
private int topMin = 400;//小于等于展示图片的头部
private int topMax = 800;//大于等于展示图片的尾部
private Paint paint = new Paint();
private Bitmap bitmap;
private View bindView;
/**
* 画在Item下面
*
* @param c
* @param parent
* @param state
*/
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
if (bindView == null) {
return;
}
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) bindView
.getLayoutParams();
final int top = bindView.getBottom() + params.bottomMargin;
//触发顶部重绘
final int bottom = top + 300;
RectF destRect = new RectF(left, top, right, bottom);
//确定中间的滚动效果
Rect srcRect;
if (top < topMin) {
srcRect = new Rect(0, 0, bitmap.getWidth(), 300);
} else if (top > topMax) {
srcRect = new Rect(0, bitmap.getHeight() - 300, bitmap.getWidth(), bitmap.getHeight());
} else {
//触发底部重绘
int srcTop = bitmap.getHeight() * (top - 400) / (800 - 400);
srcRect = new Rect(0, srcTop, bitmap.getWidth(), srcTop + 300);
}
c.drawBitmap(bitmap, srcRect, destRect, paint);
}
/**
* 画在Item上面
*
* @param c
* @param parent
* @param state
*/
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (parent.getChildAdapterPosition(view) == index) {
this.bindView = view;
outRect.set(0, 0, 0, 300);
}
}
public void bindIndex(int index) {
this.index = index;
}
public void bindImage(@Nullable Bitmap reFly) {
this.bitmap = reFly;
}
}
RecyclerView.ItemDecoration 简单应用
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 项目中经常碰到列表当中的每一项之间需要设置间距(分割线)的问题,对于RecyclerView来说,官方没有提供专门...
- Linearlayoutmanager RecyclerView嵌套GridLayoutManager Recyc...
- 简单解决RecyclerView嵌套的RecyclerView条目显示不全和宽度不能铺满 分类:Android (...