Android渲染器Shader:环状放射渐变渲染器RadialGradient(三)
Android RadialGradient渲染器提供一种环状、发散、放射形状的渐变渲染器。
写一个例子:
package zhangphil.demo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Phil on 2016/7/25.
*/
public class RadialGradientView extends View {
private Paint mPaint = null;
private float radius=480;
private RadialGradient mRadialGradient = null;
public RadialGradientView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mRadialGradient = new RadialGradient(this.getWidth()/2, this.getHeight()/2, radius, new int[] {Color.RED, Color.TRANSPARENT,Color.BLACK}, null,Shader.TileMode.CLAMP);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(mRadialGradient);
canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, radius, mPaint);
}
}
代码运行结果: