Modify onDraw(Canvas canvas) method of MyView.java in the exercise of LinearGradient.
@Override
protected void onDraw(Canvas canvas) {
float w, h, cx, cy, radius;
w = getWidth();
h = getHeight();
cx = w/2;
cy = h/2;
if(w > h){
radius = h/4;
}else{
radius = w/4;
}
canvas.drawRect(0, 0, w, h, BackPaint);
Paint MyPaint = new Paint();
MyPaint.setStyle(Paint.Style.FILL);
float shaderCx = 0;
float shaderCy = 0;
float shaderRadius = w;
int shaderColor0 = Color.WHITE;
int shaderColor1 = Color.BLACK;
MyPaint.setAntiAlias(true);
Shader radialGradientShader;
radialGradientShader = new RadialGradient(
shaderCx, shaderCy, shaderRadius,
shaderColor0, shaderColor1,
Shader.TileMode.MIRROR);
MyPaint.setShader(radialGradientShader);
canvas.drawRect(0, 0, w, h, MyPaint);
shaderCx = cx;
shaderCy = cy;
shaderRadius = radius;
shaderColor0 = Color.RED;
shaderColor1 = Color.BLUE;
radialGradientShader = new RadialGradient(
shaderCx, shaderCy, shaderRadius,
shaderColor0, shaderColor1,
Shader.TileMode.MIRROR);
MyPaint.setShader(radialGradientShader);
canvas.drawCircle(cx, cy, radius, MyPaint);
};
Download the files.
- More samples of drawing on canvas.
thanks man :) helpful
ReplyDeleteI just had to change MIRROR into CLAMP for doing what I wanted
thanks :)
What is BackPaint? This code doesn't compile for me...
ReplyDeleteBackPaint in http://android-er.blogspot.com/2012/09/example-of-paintsetshader-with.html
ReplyDelete