9

背景

我试图有一个实心圆圈,具有一定颜色和宽度的笔画,以及里面的图像。

这可以很容易地在 XML 中完成,因此(这只是一个示例):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <size
                android:width="120dp" android:height="120dp"/>
            <solid android:color="#ffff0000"/>
            <stroke
                android:width="3dp" android:color="#ff00ff00"/>
        </shape>
    </item>
    <item
        android:width="60dp" android:height="60dp" android:drawable="@android:drawable/btn_star"
        android:gravity="center"/>
</layer-list>

在此处输入图像描述

问题

我需要有上面的某些属性才能以编程方式更改,所以我不能使用 XML 文件,但思路还是一样的。

问题是,我找不到像在 XML 中那样在 OvalShape 可绘制对象上添加笔划的简单方法。我找不到执行此操作的功能。

我试过的

StackOverflow 上有一些解决方案,但我找不到一个效果很好的解决方案。我发现只有一个在这里,但它的笔划线正在被剪掉。

然而,我已经部分成功地解决了这个问题,即使用 XML 仅用于笔划本身:

stroke_drawable.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <stroke
        android:width="4dp" android:color="@android:color/white"/>
</shape>

代码:

    final int strokeDrawableResId = R.drawable.stroke_drawable;
    Drawable innerDrawable = ResourcesCompat.getDrawable(getResources(), ..., null);
    final Drawable strokeDrawable = ResourcesCompat.getDrawable(getResources(), strokeDrawableResId, null);
    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    int size = ...;
    biggerCircle.setIntrinsicHeight(size);
    biggerCircle.setIntrinsicWidth(size);
    biggerCircle.getPaint().setColor(0xffff0000);
    biggerCircle.setBounds(new Rect(0, 0, size, size));
    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{biggerCircle, strokeDrawable, innerDrawable});

    ImageView imageView = findViewById(R.id.imageView);
    imageView.setImageDrawable(layerDrawable);

它可以工作,但不是完全以编程方式(笔画在 XML 中定义)。

问题

如何将代码更改为完全编程?


编辑:我尝试了这里的建议,但不是为背景添加额外的可绘制对象,因为我需要将所有这些都放在一个可绘制对象中,所以我使用了 LayerDrawable:

    Drawable innerDrawable = ResourcesCompat.getDrawable(getResources(), android.R.drawable.btn_star, null);
    int strokeWidth = 5;
    int strokeColor = Color.parseColor("#ff0000ff");
    int fillColor = Color.parseColor("#ffff0000");
    GradientDrawable gD = new GradientDrawable();
    gD.setColor(fillColor);
    gD.setShape(GradientDrawable.OVAL);
    gD.setStroke(strokeWidth, strokeColor);
    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{gD, innerDrawable});
    ImageView imageView = findViewById(R.id.imageView);
    imageView.setImageDrawable(layerDrawable);

这可行,但由于某种原因,内部的可绘制对象(星形)被拉伸:

在此处输入图像描述

4

1 回答 1

17

您可以像往常一样以编程方式执行此操作
In YourActivity.XMl, Set-up an 。ImageView

<ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/id1"
            android:src="@mipmap/ic_launcher_round"                                
            android:padding="15dp"/>

在你的MainActivity.java

    ImageView iV = (ImageView) findViewById(R.id.id1);
    int strokeWidth = 5;
    int strokeColor = Color.parseColor("#03dc13");
    int fillColor = Color.parseColor("#ff0000");
    GradientDrawable gD = new GradientDrawable();
    gD.setColor(fillColor);
    gD.setShape(GradientDrawable.OVAL);
    gD.setStroke(strokeWidth, strokeColor);
    iV.setBackground(gD);

setColor这里设置背景颜色并setStroke设置描边宽度和描边颜色。
我为颜色、宽度等创建了一些局部变量,以使其更易于理解。
结果您增加的填充越多,圆圈的大小就会增加得越多。
在此处输入图像描述

于 2017-08-10T08:45:36.690 回答