我一直在尝试使用以下代码在android中绘制彩虹色的戒指:
public void init(){
ringPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
ringPaint.setStyle(Paint.Style.STROKE);
ringPaint.setStrokeWidth(8f);
ringPaint.setShader(new SweepGradient(0, 0, COLORS2, null));
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(getWidth()/2f,getHeight()/2f ,getHeight()/2f - 16f,ringPaint);
}
COLOR2
的值如下:
final int[] COLORS2 = new int[]{Color.parseColor("#33004c"), Color.parseColor("#4600d2"),
Color.parseColor("#0000ff"), Color.parseColor("#0099ff"),
Color.parseColor("#00eeff"),Color.parseColor("#00FF7F"),
Color.parseColor("#48FF00"),Color.parseColor("#B6FF00"),
Color.parseColor("#FFD700"),Color.parseColor("#ff9500"),
Color.parseColor("#FF6200"),Color.parseColor("#FF0000"),
Color.parseColor("#33004c")};
问题是我只能得到颜色的一个子集,也许是阵列中不同颜色的3或4种,而且我不完全确定为什么,任何人都可以就这种情况提供一些建议?
答案 0 :(得分:0)
创建SweepGradient时,您应该指定视图中心而不是0,0
请看示例
public class CustomView extends View {
private Paint ringPaint;
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public void init() {
ringPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
ringPaint.setStyle(Paint.Style.STROKE);
ringPaint.setStrokeWidth(8f);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
ringPaint.setShader(new SweepGradient(getWidth() / 2, getHeight() / 2, COLORS2, null));
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, getHeight() / 2f - 16f, ringPaint);
}
}