如何以编程方式使用笔画绘制圆形? [英] How to make a circular drawable with stroke, programmatically?
问题描述
背景
我正在尝试创建一个实心圆,具有特定颜色和宽度的笔触,并在其中添加一个图像.
这可以在 XML 中轻松完成,因此(这只是一个示例):
<项目><shape android:shape="椭圆形"><尺寸android:width="120dp" android:height="120dp"/><solid android:color="#ffff0000"/><中风android:width="3dp" android:color="#ff00ff00"/></形状></项目><项目android:width="60dp" android:height="60dp" android:drawable="@android:drawable/btn_star"机器人:重力=中心"/></层列表>
问题
我需要有上面的某些属性以编程方式改变,所以我不能使用XML文件,但想法还是一样的.
问题是,我找不到像在 XML 中那样在 OvalShape 可绘制对象上添加笔划的简单方法.我找不到执行此操作的函数.
我尝试了什么
StackOverflow 上有一些解决方案,但我找不到一个效果很好的解决方案.我找到的只有一个是
您可以以编程方式执行此操作
在 YourActivity.XMl
中,像往常一样设置 ImageView
.
在你的 MainActivity.java
ImageView iV = (ImageView) findViewById(R.id.id1);intstrokeWidth = 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
设置描边宽度和描边颜色.
我为颜色、宽度等创建了一些局部变量,以使其更易于理解.
结果
填充越多,圆圈的大小就会增加越多.
Background
I'm trying to have a filled circle, with a stroke of certain color and width, and an image inside.
This can easily be done in XML, as such (this is just a sample) :
<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>
The problem
I need to have certain properties of the above to change programmatically, so I can't use the XML file, but the idea is still the same.
Thing is, I can't find an easy way to put a stroke on an OvalShape drawable, as done in XML. I can't find the function to do it.
What I tried
There are solutions out there here on StackOverflow, but I couldn't find one that works well. Only one I've found is here, but its stroke line is being cut .
I have, however, partially succeeded in one way to solve this, by using an XML just for the stroke itself:
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>
code:
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);
It works, but it's not fully programmatically (stroke is defined in XML).
The question
How to change the code to be fully programmatic ?
EDIT: I tried what was suggested here, yet instead of an additional drawable for a background, since I needed it all in one drawable, I used 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);
This works, but for some reason the drawable inside (the star) is being stretched:
You can do this programmatically as
In YourActivity.XMl
, Set-up an ImageView
as usual.
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/id1"
android:src="@mipmap/ic_launcher_round"
android:padding="15dp"/>
And in your 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
here sets the background color and setStroke
sets the stroke width and stroke color.
I have created some local variables for color, width etc to make it more easy to understand.
Result
More you increase the padding, more will the size of circle increase.
这篇关于如何以编程方式使用笔画绘制圆形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!