在Android上以编程方式绘制带边框(角半径)的椭圆形
我正在尝试用椭圆形绘制自定义形状,填充白色和灰色边框。我创建了一个类似这样的绘图工具:在Android上以编程方式绘制带边框(角半径)的椭圆形,android,android-drawable,shapedrawable,Android,Android Drawable,Shapedrawable,我正在尝试用椭圆形绘制自定义形状,填充白色和灰色边框。我创建了一个类似这样的绘图工具: ShapeDrawable drawable = new ShapeDrawable(new OvalShape()); drawable.getPaint().setColor(Color.GRAY); drawable.getPaint().setStyle(Style.STROKE); drawable.getPaint().setStrokeWidth(getPixels(5)); drawable.
ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(Color.GRAY);
drawable.getPaint().setStyle(Style.STROKE);
drawable.getPaint().setStrokeWidth(getPixels(5));
drawable.getPaint().setAntiAlias(true);
但结果是:
其思想是通过编程方式创建一个具有不同颜色的形状:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="@android:color/transparent"/>
</shape>
怎样才能解决这个问题呢?我找到了一种方法来避免创建新的绘图工具 A根据android XML定义了一个带边框的圆,如下所示:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="@android:color/transparent"/>
</shape>
如果我们想使用StateListDrawable从代码中创建自定义选择器,请注意-StateListDrawable清除应用于drawables筛选器。。。通过
选择器在整个选择器上应用过滤器。setColorFilter…
修复该问题。这是一个新的解决方案:
RoundedBitmapDrawable RBD = RoundedBitmapDrawableFactory.create(mContext.getResources(),YourBitmap);
RBD.setCornerRadius(20);
RBD.setAntiAlias(true);
wholeRel.setBackground(RBD);
你是说问题在于链接图像中显示的边框的剪裁?是的!如果笔划宽度为0(发际线),则没有问题,但对我来说不是解决方案。方法getPixels()返回给定倾角的像素。。。但我认为这不是问题所在.我认为问题在于你的拖鞋的尺寸。您需要减小足够的大小以适应边框的宽度。顺便说一句,文档中说边框只适用于矩形,而不适用于椭圆形。可以看出这是剪辑的完美解决方案。我遇到了一个问题:无法执行
LinearLayout.addView(可绘制,新的LinearLayout.LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容,1.0f));
您正在尝试将drawable添加到布局而不是视图…创建一个图像视图或图像按钮,然后在将其添加到布局之前将drawable设置为视图,然后将其添加到布局。您知道如何通过编程更改背景色吗?非常简单:GradientDrawable bgShape=(GradientDrawable)btn.getBackground();bgShape.setColor(Color.BLACK);这会更新笔划
颜色还是纯色
颜色?
RoundedBitmapDrawable RBD = RoundedBitmapDrawableFactory.create(mContext.getResources(),YourBitmap);
RBD.setCornerRadius(20);
RBD.setAntiAlias(true);
wholeRel.setBackground(RBD);