【问题标题】:How to make a circular drawable with stroke, programmatically?如何以编程方式用笔画制作圆形可绘制对象?
【发布时间】:2018-01-18 08:50:58
【问题描述】:

背景

我正在尝试制作一个实心圆,具有特定颜色和宽度的笔触,以及里面的图像。

这可以很容易地在 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 上有一些解决方案,但我找不到一个效果很好的解决方案。我找到的只有一个here,但它的笔划线被剪掉了。

然而,我已经部分成功地解决了这个问题,即使用 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);

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

【问题讨论】:

  • 我在下面给出的答案不需要 stroke_drawable.xml。
  • @LalitSinghFauzdar 我想设置一个可绘制对象。背景用于其他用途(如压制效果)

标签: android android-drawable layerdrawable android-shapedrawable


【解决方案1】:

您可以像
一样以编程方式执行此操作 在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"/>

在你的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 设置描边宽度和描边颜色。
我为颜色、宽度等创建了一些局部变量,以使其更易于理解。
结果

增加的padding越多,圆圈的大小就会越大。

【讨论】:

  • 椭圆形的部分在哪里?为什么它有“getBackground”?
  • 您能否展示如何在椭圆形而不是视图上进行操作?就像 XML 显示的那样?
  • ImageView 已经可以有背景了。我想创建一个可绘制的内容以设置为内容,包括圆圈内的内容(在您的情况下为“ic_launcher_round”),正如我在问题中所展示的那样。它是圆圈、轮廓和内容。我希望将它们全部放在一个不在 XML 中的可绘制对象中。您已将其中的一部分放在 XML 中。不过,我认为这可以通过 LayerDrawable 解决,不是吗?
  • 我希望以编程方式获得与我在可绘制 XML 上显示的内容相同的内容。可绘制对象应包含 XML 所具有的所有内容。但是,我认为这可以使用 LayerDrawable 来完成。我认为你的解决方案可以工作,如果它有它而不是在 XML 中设置“src”。
  • 我整天看到的最佳答案...谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 2021-01-31
  • 2012-01-16
  • 2019-11-21
  • 1970-01-01
  • 2020-12-07
相关资源
最近更新 更多