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); } }
【android记录】自定义view绘制矩形框
最新推荐文章于 2025-06-18 10:59:09 发布
本文介绍了一个自定义Android视图的实现方法,通过继承View并重写onDraw方法来绘制矩形框。该视图支持外部设置矩形的位置和大小,并能够实时更新显示。

3059

被折叠的 条评论
为什么被折叠?



