前言
GradientDrawable類似與Xml布局裡的shape,常用在一些自己封裝的對話框控件的背景或者其他View中,優勢是不需要你在帶着xml布局檔案一起封包..
畫線
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 = newGradientDrawable();
gradientDrawable.setShape(GradientDrawable.LINE);
gradientDrawable.setStroke(1, Color.BLACK, 10, 10);//第一個參數為線的寬度 第二個參數是線的顔色 第三個參數是虛線段的長度 第四個參數是虛線段之間的間距長度
mTextView.setBackground(gradientDrawable);
也可以在布局裡關閉指定view的硬體加速
android:layerType="software"
效果圖:
畫圓
GradientDrawable gradientDrawable = newGradientDrawable();
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setColor(Color.BLUE);
gradientDrawable.setSize(50,50);
mTextView.setBackground(gradientDrawable);
效果圖:
畫圓環
GradientDrawable gradientDrawable = newGradientDrawable();
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setColor(Color.BLUE);
gradientDrawable.setStroke(10,Color.YELLOW);
gradientDrawable.setSize(50,50);
mTextView.setBackground(gradientDrawable);
效果圖:
圓角矩形
GradientDrawable gradientDrawable = newGradientDrawable();
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 = newGradientDrawable();
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= newGradientDrawable();
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= newGradientDrawable();
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= newGradientDrawable();
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= newGradientDrawable();
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 = newGradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setColor(Color.YELLOW);
gradientDrawable.setAlpha(70);//設定透明度
mTextView.setBackground(gradientDrawable);