前言
GradientDrawable 支持漸變色的Drawable,與shapeDrawable在畫型上是類似的,多了支持漸變色。代碼上的GradientDrawable比在xml里的shape下gradient屬性強大的多,因為shape下gradient屬性只支持三色階漸變,而GradientDrawable可以有更多的色階漸變。
畫線
GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.LINE); gradientDrawable.setStroke(5, Color.YELLOW);//線的寬度 與 線的顏色 mTextView.setBackground(gradientDrawable);
效果圖:
畫虛線
mTextView.setLayerType(View.LAYER_TYPE_SOFTWARE,null); //要顯示虛線一定要關閉硬件加速 GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.LINE); gradientDrawable.setStroke(1, Color.BLACK, 10, 10);//第一個參數為線的寬度 第二個參數是線的顏色 第三個參數是虛線段的長度 第四個參數是虛線段之間的間距長度 mTextView.setBackground(gradientDrawable);
也可以在布局里關閉指定view的硬件加速
android:layerType="software"
效果圖:
畫圓
GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.OVAL); gradientDrawable.setColor(Color.BLUE); gradientDrawable.setSize(50,50); mTextView.setBackground(gradientDrawable);
效果圖:
畫圓環
GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.OVAL); gradientDrawable.setColor(Color.BLUE); gradientDrawable.setStroke(10,Color.YELLOW); gradientDrawable.setSize(50,50); mTextView.setBackground(gradientDrawable);
效果圖:
圓角矩形
GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.RECTANGLE); gradientDrawable.setColor(Color.RED); gradientDrawable.setStroke(10,Color.BLUE); gradientDrawable.setCornerRadius(10); gradientDrawable.setSize(50,50); mTextView.setBackground(gradientDrawable);
效果圖:
虛線矩形
GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.LINEAR_GRADIENT); gradientDrawable.setStroke(1, Color.GREEN,30, 30); mTextView.setBackground(gradientDrawable);
效果圖:
顏色漸變
線性漸變
int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE}; GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.RECTANGLE); gradientDrawable.setColors(colors); //添加顏色組 gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);//設置線性漸變 gradientDrawable.setSize(50,50); mTextView.setBackground(gradientDrawable);
效果圖:
改變線性漸變方向
int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE}; GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.RECTANGLE); gradientDrawable.setColors(colors); //添加顏色組 gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);//設置線性漸變 gradientDrawable.setOrientation(GradientDrawable.Orientation.RIGHT_LEFT);//設置漸變方向 gradientDrawable.setSize(50,50); mTextView.setBackground(gradientDrawable);
效果圖:
半徑漸變
int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE}; GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.RECTANGLE); gradientDrawable.setColors(colors); //添加顏色組 gradientDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);//設置半徑漸變 gradientDrawable.setGradientRadius(50);//漸變的半徑值 gradientDrawable.setSize(50,50); mTextView.setBackground(gradientDrawable);
效果圖:
掃描漸變
int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE}; GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.RECTANGLE); gradientDrawable.setColors(colors); //添加顏色組 gradientDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);//設置掃描漸變 gradientDrawable.setGradientCenter(0.5f,0.5f);//漸變中心點 gradientDrawable.setSize(50,50); mTextView.setBackground(gradientDrawable);
防抖
gradientDrawable.setDither(true);
可以讓漸變的時候顏色階梯降低,變得更柔和
透明度
GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.RECTANGLE); gradientDrawable.setColor(Color.YELLOW); gradientDrawable.setAlpha(70);//設置透明度 mTextView.setBackground(gradientDrawable);