【android記錄】自定義view繪製矩形框

public class FackMask extends View {

    private static final String TAG = "FackMask";

    private Paint paint;
    private Rect rect;

    private int left;
    private int top;
    private int right;
    private int bottom;

    private Canvas canvas;


    public FackMask(Context context){
        super(context);
    }

    public FackMask(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FackMask(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }


    /**
     * 外部調用的接口
     * */
    public void setRect(int left,int top,int right,int bottom){
        this.left=left;
        this.top=top;
        this.right=right;
        this.bottom=bottom;
        Log.d(TAG, "setRect: 控件");

        invalidate();//更新調用onDraw重新繪製
    }



    private void initPaint(){
        paint=new Paint();
        left=100;
        top=100;
        right=300;
        bottom=300;
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);//設置空心
        rect=new Rect(left,top,right,bottom);
        canvas.drawRect(rect,paint);
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章