demo
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;
//觸發(fā)頂部重繪
final int bottom = top + 300;
RectF destRect = new RectF(left, top, right, bottom);
//確定中間的滾動(dòng)效果
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 {
//觸發(fā)底部重繪
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;
}
}