版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/voidreturn/article/details/77718470
背景:如何在跨越兩個或兩個以上的item繪制一個view,該view需要跟随recyclerView的滑動而整體移動。
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
final View child = parent.getChildAt(1);
if (child != null) {
final int left = child.getLeft();
final int right = child.getRight();
final int top = child.getTop();
final int bottom = child.getBottom();
int rightV = (right - left) / 6;
int leftV = (right - rightV) - VAUtils.dip2px(mContext, 225);
int topV = bottom - VAUtils.dip2px(mContext, 16);
int bottomV = topV + VAUtils.dip2px(mContext, 82);
ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(mImageView.getLayoutParams());
margin.setMargins(leftV, topV, rightV, bottomV);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
mImageView.setLayoutParams(layoutParams);
if (!imageClicked) {
mImageView.setVisibility(View.VISIBLE);
}
//畫筆
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//圓心 x 坐标
final float x = parent.getWidth() / 2 + left;
////圓心 y 坐标
final float y = 100 + top;
//半徑
final float radius = 100;
//漸變着色器 坐标随意設定的
final LinearGradient shader = new LinearGradient(x-50, 0, x+100, 200, Color.RED, Color.YELLOW, Shader.TileMode.REPEAT);
paint.setShader(shader);
//繪制圓
c.drawCircle(x, y, radius, paint);
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
//畫筆
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//圓心 x 坐标
final float x = parent.getWidth() / 2;
////圓心 y 坐标
final float y = 100;
//半徑
final float radius = 100;
//漸變着色器 坐标随意設定的
final LinearGradient shader = new LinearGradient(x-50, 0, x+100, 200, Color.RED, Color.YELLOW, Shader.TileMode.REPEAT);
paint.setShader(shader);
//繪制圓
c.drawCircle(x, y, radius, paint);
}