使用径向渐变中的标准开始、中心和结束颜色可以很容易地实现左侧的渐变,其中开始 = 黄色,中心 = 紫色,结束 = 蓝色。然而,右边的圆圈需要重新定位中心颜色。这可能吗?
左边的结果可以用类似的东西重现:
<shape android:shape="oval">
<gradient
android:endColor="#0000ff"
android:gradientRadius="my_radius"
android:centerColor="#770077"
android:startColor="#00ffff"
android:type="radial"/>
</shape>
我想知道是否可以移动中心颜色以实现右侧的渐变。我相信答案是否定的,但我想看看是否有人发现了一种方法来做到这一点。谢谢!
最佳答案
遗憾的是,这无法通过 XML 声明实现,但是可以通过代码实现。
这是一个快速代码示例:
public class MyDrawing extends View
{
private Paint mPaint;
public MyDrawing(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
public MyDrawing(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public MyDrawing(Context context)
{
super(context);
init();
}
private void init()
{
int [] colors = new int[] { 0xff0000ff, 0xff00ff00, 0xffff0000 };
float [] positions = new float[] { 0.4f, 0.7f, 0.9f };
RadialGradient gradient = new RadialGradient(50, 50, 50, colors, positions, TileMode.CLAMP);
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setShader(gradient);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawCircle(circleX, circleY, circleRadius, mPaint);
}
}
需要注意的两点:
在颜色数组中,您必须指定 alpha(第 1 个到 chars)。在我的示例中,我将两者都指定为“ff”,表示没有透明度。如果您不指定 alpha,它将默认为 0。
位置数组指定渐变中每种颜色的位置或强度。尝试使用它来获得您想要的结果。
希望这有帮助:)
关于android - 中心颜色可以在 android 径向渐变绘图中重新定位吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13078845/