코딩하는 일용직 노동자

View Background를 Rounded corner 형태로 만들기 본문

안드로이드

View Background를 Rounded corner 형태로 만들기

bacass 2020. 5. 12. 09:43

라운드된 배경에 알맞게 view를 잘려 보이도록 처리하는 방법입니다.

흰색 라운드 배경위에 RecyclerView 를 배치했는데 배경의 라운드와 딱맞게 이미지가 가려져 보이도록 처리했습니다.

round 배경에 맞게 아이템이 잘려보인다.

https://youtu.be/d1BPxyT_Iog

실제 구현 영상
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 * Created by LeeWW on 2018. 7. 27.
 */

public class RoundedRecyclerView extends RecyclerView {

    private Path path;
    private Context mContext;

    public RoundedRecyclerView(Context context) {
        super(context);
        mContext = context;
    }

    public RoundedRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public RoundedRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        if (path == null) {
            path = new Path();
            path.addRoundRect(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), Util.dpToPx(mContext, 31), Util.dpToPx(mContext, 31), Path.Direction.CW);
        }
        canvas.clipPath(path);

        super.dispatchDraw(canvas);
    }
}