Android應用開發—RecyclerView繪製蒙層

image_c發表於2017-08-30
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 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);
}


相關文章