Dan*_*ith 11 android gradient drawable android-xml
使用标准的开始,中心和结束颜色在android的径向渐变中很容易实现左下方的渐变,其中start = yellow,center = purple,end = blue.然而,右边的圆圈需要重新定位中心颜色.这可能吗?

左边的结果可以通过以下方式重现:
<shape android:shape="oval">
<gradient
android:endColor="#0000ff"
android:gradientRadius="my_radius"
android:centerColor="#770077"
android:startColor="#00ffff"
android:type="radial"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
我想知道我是否可以移动中心颜色来实现右边的渐变.我相信答案是否定的,但我想看看是否有人发现了这样做的方法.谢谢!
遗憾的是,这无法通过 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);
}
}
Run Code Online (Sandbox Code Playgroud)
您应该注意两件事:
在颜色数组中,您必须指定 alpha(第一个字符)。在我的示例中,我将两者都指定为“ff”,表示不透明。如果不指定 alpha,它将默认为 0。
Positions 数组指定渐变中每种颜色的位置或强度。尝试一下以达到您想要的结果。
希望这可以帮助 :)
| 归档时间: |
|
| 查看次数: |
4068 次 |
| 最近记录: |