五种方式:
BitmapShader bitmapShader;//位图渲染
LinearGradient linearGradient;//线性渲染
SweepGradient sweepGradient;//渐变渲染
RadialGradient radialGradient;//环形渲染
ComposeShader composeShader;//组合渲染
其中组合渲染和组合动画类似,但是组合渲染只能将两个渲染结合起来。
1、位图渲染
类似于给图片设置背景,这里有三种方式:图片比内容小,第一种就拉伸最后一个像素,第二种就重复图片,第三种是做镜像对称。可以分别设置X/Y方向上的填充模式
demo:X方向上使用重复。Y轴使用镜像
private void useBitmapShader(Canvas canvas) {
//使用BitmapShader
// Shader.TileMode.CLAMP;//拉伸最后一个像素去铺满
// Shader.TileMode.REPEAT;//重复以铺满
// Shader.TileMode.MIRROR;//镜像翻转铺满
bitmapShader = new BitmapShader(BitmapFactory.decodeResource(getResources(), R.drawable.aipay),
Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
paint.setShader(bitmapShader);
canvas.drawRect(new Rect(getPaddingLeft(), getPaddingTop(), getMeasuredWidth()
- getPaddingLeft() * 2
, getMeasuredHeight()), paint);
;
//使用shapeDrawable也可以实现同样的效果
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
shapeDrawable.getPaint().setShader(bitmapShader);
shapeDrawable.setBounds(0, 0, ScreenSizeUtil.getScreenSize_Width(getContext()), getMeasuredHeight());
// shapeDrawable.draw(canvas);
}
效果图:
2.线性渐变
private void userLinerGradient(Canvas canvas) {
int color[] = {Color.parseColor("#000000"), Color.parseColor("#ff0000"), Color.p