1.shader
在绘图过程中,着色器是返回颜色水平范围的对象的基类。在paint中调用setShader(shader)方法
设置一个shader的子类 , 之后任何对象(位图除外)使用该颜料绘制的颜色将从着色器中获取。
/**
* Shader is the base class for objects that return horizontal spans of colors
* during drawing. A subclass of Shader is installed in a Paint calling
* paint.setShader(shader). After that any object (other than a bitmap) that is
* drawn with that paint will get its color(s) from the shader.
*/
public class Shader {
...
public enum TileMode {
/**
* Replicate the edge color if the shader draws outside of its
* original bounds.
*/
CLAMP (0),
/**
* Repeat the shader's image horizontally and vertically.
*/
REPEAT (1),
/**
* Repeat the shader's image horizontally and vertically, alternating
* mirror images so that adjacent images always seam.
*/
MIRROR(2),
/**
* Render the shader's image pixels only within its original bounds. If the shader
* draws outside of its original bounds, transparent black is drawn instead.
*/
DECAL(3);
...
}
}
TileMode :着色器的平铺模式
CLAMP:如果着色器超出原始边界,超出的部分用着色器的边缘颜色
REPEAT :如果着色器超出原始边界,复制着色器的图片,包含横向或者竖向
MIRROR:镜像着色器的内容,倒影形式
DECAL:渲染着色器原本边界的图像像素,如果超出,绘制黑色透明
shader有几个子类:
2.LinearGradient
Create a shader that draws a linear gradient along a line.
LinearGradient 属于shader的子类,实现了线性着色的逻辑。且可以绘制多个点,不同的方向来着色。
float x0, float y0:起点坐标
float x1, float y1:终点坐标
int[] colors:颜色集,如果多个颜色,默认平分绘制
float[] positions:颜色绘制的比例 范围:0-1
TileMode tile:绘制的类型;
public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int[] colors,
@Nullable float[] positions, @NonNull TileMode tile) {
this(x0, y0, x1, y1, convertColors(colors), positions, tile,
ColorSpace.get(ColorSpace.Named.SRGB));
}
CLAMP
组件宽度为充满
着色器的宽度为mTranslate + shimer_width = 100
<com.lll.testshimmer.LinearGradientView
android:layout_width="match_parent"
android:layout_margin="22dp"
android:layout_height="50dp"
/>
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int[] mColors = {Color.YELLOW, Color.BLUE, Color.RED};
LinearGradient linearGradient =
new LinearGradient(mTranslate, 0, mTranslate + shimer_width, 0,
mColors,
new float[]{0f, 0.5f, 1.0f},
Shader.TileMode.REPEAT);
// Shader.TileMode.CLAMP);
mPaint.setShader(linearGradient);
canvas.drawRect(mTranslate, 0, getWidth(), getHeight(), mPaint);
}
REPEAT
直译:重复。效果图里,着色器会重复着色
MIRROR
MIRROR和REPEAT 相反
DECAL
需要sdk 31(android 12 )
目前测试无效果
3.BitmapShader
bitmap:位图对象
tileX: x轴的tile模式
tileY:y轴的tile模式
BitmapShader (Bitmap bitmap,
Shader.TileMode tileX,
Shader.TileMode tileY)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int w = getWidth();
int h = getHeight();
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.yourname);
mShader = new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(mShader);
canvas.drawRect(0,0,w,h,mPaint);
}
4.RadialGradient
环形着色器
RadialGradient (float centerX,
float centerY,
float radius,
int centerColor,
int edgeColor,
Shader.TileMode tileMode)
//centerX 圆心的X坐标
//centerY 圆心的Y坐标
//radius 圆的半径
//centerColor 中心颜色
//edgeColor 边缘颜色
//tileMode 这个不用介绍了吧?
5.SweepGradient
顺时针扫描 着色器
SweepGradient (float cx,
float cy,
int color0,
int color1)
//cx cy 为绘制的起点坐标
//color0是起始颜色
//color1是终止颜色
6.ComposeShader
混合着色器,就是同时有多种着色器绘制
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.repeat);
Bitmap result = Bitmap.createScaledBitmap(bmp,w,h,false);
//1. 编写1个BitmapShader.
BitmapShader bitmapShader = new BitmapShader(result, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
//2. 编写1个RadiasGradient。
RadialGradient radialGradient = new RadialGradient(radius,radius,radius,Color.BLACK,Color.TRANSPARENT, Shader.TileMode.CLAMP);
//3. 将它们进行混合产生新的Shader.
ComposeShader composeShader = new ComposeShader(bitmapShader,radialGradient,new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mPaint.setShader(composeShader);
//4. 以新的Shader绘制一个圆。
canvas.drawCircle(w/2,h/2,radius,mPaint);
参考链接:https://blog.csdn.net/briblue/article/details/53694042