【androidレコード】カスタムview長方形ボックスの描画

7161 ワード

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);
    }

}