・LinearGradient
・RadialGradient
・SweepGradient
それぞれ Shader クラスを継承していて
のようにして使う
Shader s = new LinearGradient(0, 0, 10, 10, Color.BLACK, Color.WHITE, Shader.TileMode.CLAMP);
Paint p = new Paint();
p.setShader(s);
・LinearGradient
直線方向のグラデーションを描く
Public Constructors
public LinearGradient (float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode tile)
Create a shader that draws a linear gradient along a line.
Parameters
x0 : The x-coordinate for the start of the gradient line
y0 : The y-coordinate for the start of the gradient line
x1 : The x-coordinate for the end of the gradient line
y1 : The y-coordinate for the end of the gradient line
colors : The colors to be distributed along the gradient line
positions : May be null. The relative positions [0..1] of each corresponding color in the colors array. If this is null, the the colors are distributed evenly along the gradient line.
tile : The Shader tiling mode
public LinearGradient (float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile)
Create a shader that draws a linear gradient along a line.
Parameters
x0 : The x-coordinate for the start of the gradient line
y0 : The y-coordinate for the start of the gradient line
x1 : The x-coordinate for the end of the gradient line
y1 : The y-coordinate for the end of the gradient line
color0 : The color at the start of the gradient line.
color1 : The color at the end of the gradient line.
tile : The Shader tiling mode
・RadialGradient
円環のグラデーションを描く
Public Constructors
public RadialGradient (float x, float y, float radius, int[] colors, float[] positions, Shader.TileMode tile)
Create a shader that draws a radial gradient given the center and radius.
Parameters
x : The x-coordinate of the center of the radius
y : The y-coordinate of the center of the radius
radius : Must be positive. The radius of the circle for this gradient
colors : The colors to be distributed between the center and edge of the circle
positions : May be NULL. The relative position of each corresponding color in the colors array. If this is NULL, the the colors are distributed evenly between the center and edge of the circle.
tile : The Shader tiling mode
public RadialGradient (float x, float y, float radius, int color0, int color1, Shader.TileMode tile)
Create a shader that draws a radial gradient given the center and radius.
Parameters
x : The x-coordinate of the center of the radius
y : The y-coordinate of the center of the radius
radius : Must be positive. The radius of the circle for this gradient
color0 : The color at the center of the circle.
color1 : The color at the edge of the circle.
tile : The Shader tiling mode
・SweepGradient
色の変化方向が円環になっているグラデーションを描く
Public Constructors
public SweepGradient (float cx, float cy, int[] colors, float[] positions)
A subclass of Shader that draws a sweep gradient around a center point.
Parameters
cx : The x-coordinate of the center
cy : The y-coordinate of the center
colors : The colors to be distributed between around the center. There must be at least 2 colors in the array.
positions : May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly.
public SweepGradient (float cx, float cy, int color0, int color1)
A subclass of Shader that draws a sweep gradient around a center point.
Parameters
cx : The x-coordinate of the center
cy : The y-coordinate of the center
color0 : The color to use at the start of the sweep
color1 : The color to use at the end of the sweep
サンプルコードはこちら
More...
package yanzm.example.gradienttest;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.os.Bundle;
import android.view.View;
public class GradientTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GradientTestView(this));
}
private static class GradientTestView extends View {
private Paint mPaint;
private final int[] mColors;
private int CENTER_X;
private int CENTER_Y;
private Shader s1, s2, r1, r2, l1, l2;
float r;
public GradientTestView(Context context) {
super(context);
mColors = new int[] {
0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
0xFFFFFF00, 0xFFFF0000
};
s1 = new SweepGradient(0, 0, mColors, null);
s2 = new SweepGradient(0, 0, Color.BLACK, Color.GREEN);
r1 = new RadialGradient(0, 0, 50, mColors, null, Shader.TileMode.CLAMP);
r2 = new RadialGradient(0, 0, 50, Color.BLACK, Color.BLUE, Shader.TileMode.CLAMP);
l1 = new LinearGradient(-30, 30, 30, 30, mColors, null, Shader.TileMode.CLAMP);
l2 = new LinearGradient(-30, 60, 30, 60, Color.BLACK, Color.RED, Shader.TileMode.CLAMP);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(32);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.translate(CENTER_X, CENTER_Y/2);
mPaint.setShader(s1);
canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
mPaint.setShader(r1);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(0, 0, CENTER_Y/4, mPaint);
canvas.translate(0, CENTER_Y);
mPaint.setShader(s2);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
mPaint.setShader(r2);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(0, 0, CENTER_Y/4, mPaint);
canvas.translate(-CENTER_X*3/4, -CENTER_Y/2);
mPaint.setShader(l1);
canvas.drawRect(-30, -30, 30, 30, mPaint);
canvas.translate(CENTER_X*3/2, 0);
mPaint.setShader(l2);
canvas.drawRect(-30, -30, 30, 30, mPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
CENTER_X = this.getWidth()/2;
CENTER_Y = this.getHeight()/2;
r = CENTER_Y/2 - mPaint.getStrokeWidth()*0.5f;
}
}
}
0 件のコメント:
コメントを投稿