如何以编程方式使用笔画制作圆形绘图?

and*_*per 4 android android-drawable layerdrawable android-shapedrawable

背景

我想要一个填充圆圈,具有一定颜色和宽度的笔划,以及内部的图像.

这可以很容易地用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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

问题

我需要具有以上某些属性以编程方式进行更改,因此我无法使用XML文件,但这个想法仍然是相同的.

事实是,我无法找到一种简单的方法来在OvalShape drawable上绘制,就像在XML中一样.我找不到这样做的功能.

我尝试了什么

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>
Run Code Online (Sandbox Code Playgroud)

码:

    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);
Run Code Online (Sandbox Code Playgroud)

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

这个问题

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


编辑:我尝试了这里建议的,而不是一个额外的drawable为背景,因为我需要一个drawable,我使用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);
Run Code Online (Sandbox Code Playgroud)

这是有效的,但由于某种原因,可拉伸的内部(星形)正在被拉伸:

在此输入图像描述

Lal*_*dar 11

您可以通过编程这样做,因为
YourActivity.XMl,建立一个ImageView像往常一样.

<ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/id1"
            android:src="@mipmap/ic_launcher_round"                                
            android:padding="15dp"/>
Run Code Online (Sandbox Code Playgroud)

在你的 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);
Run Code Online (Sandbox Code Playgroud)

setColor这里设置背景颜色并setStroke设置笔触宽度和笔触颜色.
我已经为颜色,宽度等创建了一些局部变量,使其更容易理解.
结果
在此输入图像描述
你增加填充越多,圆圈的大小就越多.