安卓椭圆形双色

3

我有一个线性布局视图,其背景我已经设置成椭圆形实心颜色。到目前为止,我的背景是圆形的。现在我想使用形状可绘制对象来获得具有两种颜色的圆形。请参见附件。

enter image description here


你想实现你展示的图片对吗? - Zahan Safallwa
嗨@ZahanSafallwa,是的,这就是我想要实现的。 - Ranveer Bedaysee
使用一个LayerDrawable和两个图层:一个是普通的蓝色椭圆形,第二个是围绕ClipDrawable的绿色椭圆形,但说实话,为什么要这么复杂,当你可以创建一个自定义的Drawable类,在其中绘制任何你想要的东西呢? - pskink
你好 @pskink,能否给我提供一个示例客户端可绘制类? - Ranveer Bedaysee
要么创建一个继承自Drawable的MyDrawable类,像这样:class MyDrawable extends Drawable {...},要么使用ShapeDrawable和自定义形状(Shape),像这样:class MyShape extends Shape {...} - pskink
3个回答

8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <gradient
        android:centerX="-1"
        android:type="sweep"
        android:startColor="color1"
        android:endColor="color2"
        />

</shape>

在给出答案时,最好附上解释为什么你的答案是正确的。点击这里了解更多详情。 - Stephen Rauch
1
Audo它可以工作,但是如果我想要颜色垂直分割(左/右),我该怎么做? - murt
你可以将此设置为背景后旋转视图,只需在xml中添加以下内容:android:rotation="90" - anro

3

在您的drawable文件夹中创建shape.xml

shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient android:startColor="#0000FF" android:endColor="#00FF00"
android:angle="270"/>
</shape>

将此 shape.xml 添加到您的视图背景中。 - Damini Mehra
很好的答案,只需用 android:shape="ring" 替换上面的 android:shape="oval" 行即可与上面的图像完全相同。 因此最终的 xml 将如下所示:- <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" > <gradient android:startColor="#0000FF" android:endColor="#00FF00" android:angle="270"/> </shape>总的来说,你的回答非常棒。@Damini - Adarsh Yadav

1
这可能有些晚了,但我很难找到好的答案,所以听我的看法。我使用了自定义可绘制对象来绘制圆形,并使用线性渐变着色器,通过位置数组进行配置,以使渐变没有过渡。渐变线方向在LinearGradient构造函数中进行配置(这里是对角线方向)。
public class MultiColorCircleDrawable extends Drawable {

    private Paint paint;
    private int[] colors;

    public MultiColorOvalDrawable(int[] colors) {
        this.colors = colors;
    }

    private void init() {
        paint = new Paint();
        paint.setShader(createShader());
    }

    private Shader createShader() {
        int[] colorsArray = new int[colors.length * 2];
        float[] positions = new float[colors.length * 2];

        for (int i = 0; i < colors.length; i++) {
            int y = i * 2;
            int color = colors[i];
            colorsArray[y] = color;
            colorsArray[y+1] = color;
            positions[y] = 1f / colors.length * i;
            positions[y+1] = 1f / colors.length * (i+1);
        }

        Rect bounds = getBounds();
        int width = bounds.right - bounds.left;
        int height = bounds.bottom - bounds.top;

        return new LinearGradient(0, 0, width, height, colorsArray, positions, Shader.TileMode.REPEAT);
    }

    @Override
    public void draw(Canvas canvas) {
        if (null == paint) {
            init();
        }

        Rect bounds = getBounds();
        int width = bounds.right - bounds.left;
        int height = bounds.bottom - bounds.top;
        canvas.drawCircle(width/2, height/2, (width/2) - strokeWidth, maskPaint);
    }

    @Override
    public void setAlpha(int i) {

    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接