本文共 2283 字,大约阅读时间需要 7 分钟。
背景:如何在跨越两个或两个以上的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); } }
@Overridepublic 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);}