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