Monday, September 10, 2012

Example of RadialGradient

Example of RadialGradient:

RadialGradient


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 filesDownload the files.

- More samples of drawing on canvas.

3 comments:

Anonymous said...

thanks man :) helpful

I just had to change MIRROR into CLAMP for doing what I wanted

thanks :)

Anonymous said...

What is BackPaint? This code doesn't compile for me...

Erik said...

BackPaint in http://android-er.blogspot.com/2012/09/example-of-paintsetshader-with.html