一 Drawable setBounds()
注: Drawable.setBounds(int left,int top,int right,int bottom); 四边 永远平行坐标系x y
参数一 left 为矩形左侧距离y坐标系的距离 top为矩形区域上方距离x坐标系的距离 right 为 右侧 y坐标系距离的距离
bottom 为矩形下方距离x坐标系的距离
方法 1:gradientDrawable.setBounds(RectF r); 传入矩形区域
方法2 :Drawable.setBounds(int left,int top,int right,int bottom); 传入上下左右坐标点 形成矩形区域
作用:这个四参数 或 RectF 指的是drawable将在被绘制在canvas的哪个矩形区域内。
例子:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = canvas.getWidth();
int height = canvas.getHeight();
canvas.translate(400, 400);
// 绘制坐标线
mPaint.setColor(Color.RED);
canvas.drawLine(0, 0, width, 0, mPaint);
//绘制蓝色Y轴
mPaint.setColor(Color.BLUE);
canvas.drawLine(0, 0, 0, height, mPaint);
canvas.translate(0,10);
mPaint.setColor(Color.RED);
canvas.drawLine(0,0,100,0,mPaint);
// 设置gradientDrawable将被绘制在哪个矩形区域内
gradientDrawable.setBounds(10, 10, 100, 100);
gradientDrawable.draw(canvas);
}