Code example for LinearGradient

Methods: setShader

0
    { 
      super.onDraw(canvas);
      /* ³]©w­I´º¬°¥Õ¦â */ 
      canvas.drawColor(Color.WHITE);
      /* ³]©wº¥¼h¦â */ 
      Shader mShader=new LinearGradient(0, 0,100,100,
          new int[]{Color.RED, Color.GREEN,Color.BLUE,Color.YELLOW},
          null, Shader.TileMode.REPEAT);
       
      ShapeDrawable sd;
      /* µe¤@­Ó¹ê¤ß¥¿¤è§Î*/ 
      sd=new ShapeDrawable(new RectShape());
      sd.getPaint().setShader(mShader);
      sd.setBounds(20,20,100,100);
      sd.draw(canvas);
       
      float[] R = new float[] { 10, 10, 10, 10,10, 10, 10, 10 };
      RectF   inset = new RectF(10,10,10,10);
      /* µe¤@­Ó¦³©·¨¤ªº¥¿¤è§Î */  
      sd=new ShapeDrawable(new RoundRectShape(R, null,null));
      sd.getPaint().setShader(mShader);
      sd.setBounds(120,20,200,100);
      sd.draw(canvas);
      /* µe¤@­ÓªÅ¤ß¥B¦³©·¨¤ªº¥¿¤è§Î */  
      sd=new ShapeDrawable(new RoundRectShape(R,inset,R));
      sd.getPaint().setShader(mShader);
      sd.setBounds(220,20,300,100);
      sd.draw(canvas);
      /* µe¤@­ÓªÅ¤ß¶ê */ 
      sd=new ShapeDrawable(new OvalShape());
      sd.getPaint().setStrokeWidth(3);
      sd.getPaint().setStyle(Paint.Style.STROKE);
      sd.getPaint().setShader(mShader);
      sd.setBounds(20,120,100,200);
      sd.draw(canvas);
      /* µe¤@­Ó¹ê¤ß¾ò¶ê§Î*/ 
      sd=new ShapeDrawable(new OvalShape());
      sd.getPaint().setShader(mShader);
      sd.setBounds(120,120,200,200);
      sd.draw(canvas);
      /* µe¤@­Ó¹ê¤ß¤T¨¤§Î*/ 
      Path path = new Path();
      path.moveTo(220,200);
      path.lineTo(300,200);
      path.lineTo(260,120);
      path.close();
      sd=new ShapeDrawable(new PathShape(path,100,100));
      sd.getPaint().setShader(mShader);
      sd.setBounds(10,10,100,100);
      sd.draw(canvas);
    } 
  } 
}