廢話少說,來例子是最實際的 XD
以下是source code
public void draw(Canvas c, long time) {
int width = c.getWidth(), height = c.getHeight();
Paint paint = new Paint();
paint.setColor(Color.GREEN);
LinearGradient lgCLAMP = new LinearGradient(0, 0, 100, 200, Color.RED, Color.BLUE, Shader.TileMode.CLAMP);
LinearGradient lgMIRROR = new LinearGradient(0, 0, 100, 200, Color.RED, Color.BLUE, Shader.TileMode.MIRROR);
LinearGradient lgREPEAT = new LinearGradient(0, 0, 100, 200, Color.RED, Color.BLUE, Shader.TileMode.REPEAT);
c.drawColor(Color.GRAY);
// Separate the screen into four areas
// Top-left area
c.save();
c.clipRect(0, 0, width / 2, height / 2);
paint.setShader(lgCLAMP);
c.drawPaint(paint);
c.drawText("MODE: CLAMP", width / 4, height / 4 + 40 / 2, TEXT_PAINT);
c.restore();
// Top-right
c.save();
c.clipRect(width / 2, 0, width, height / 2);
c.translate(width / 2, 0);
paint.setShader(lgMIRROR);
c.drawPaint(paint);
c.drawText("MODE: MIRROR", width / 4, height / 4 + 40 / 2, TEXT_PAINT);
c.restore();
// Button-left
c.save();
c.clipRect(0, height / 2, width / 2, height);
c.translate(0, height / 2);
paint.setShader(lgREPEAT);
c.drawPaint(paint);
c.drawText("MODE: REPEAT", width / 4, height / 4 + 40 / 2, TEXT_PAINT);
c.restore();
// There is no button-right :)
}