椭圆形编程方式创建剪辑时, [英] Oval shape clipped when created programmatically
问题描述
我有一个剪辑的问题。
首先,我试图显示椭圆形使用XML而已。我有以下code:
First, I tried to display an oval shape with Xml only. I had the following code:
RES /绘制/ circle.xml 的
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:width="240dp"
android:height="240dp" />
<solid
android:color="#FFFFFF" />
<stroke
android:width="2dp"
android:color="#EEEEEE" />
</shape>
* RES /布局/的test.xml
*res/layout/test.xml
....
<RelativeLayout
android:id="@+id/circle_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:src="@drawable/circle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
....
它完美,并给了我这个:截图
现在的问题是,由于种种原因,我必须做同样的progammatically。
The problem is, for various reason, I must do the same progammatically.
我这有code:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.circle_layout);
ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(Color.parseColor("#EEEEEE"));
drawable.getPaint().setStyle(Style.STROKE);
drawable.getPaint().setStrokeWidth(dpToPx(2));
drawable.getPaint().setAntiAlias(true);
drawable.setIntrinsicHeight(dpToPx(240));
drawable.setIntrinsicWidth(dpToPx(240));
ImageView iv = new ImageView(this);
iv.setImageDrawable(drawable);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
iv.setLayoutParams(lp);
layout.addView(iv);
dpToPx功能是:
dpToPx function is:
private float scale = 0;
private float getScale() {
if (scale == 0)
scale = this.getResources().getDisplayMetrics().densityDpi / 160f;
return scale;
}
public float dpToPx(float dp) {
return dp * getScale();
}
...这应该给了我同样的事情吗?那么它给了一个稍微大一圈,与上,右,下和左边缘裁剪。这里有一个截屏(屏幕比previous一个相同区域): Screehshot
有人有一个想法是什么,为什么?
Someone has an idea on what and why?
感谢你。
编辑:
如果我设置了行程宽度为更高的值(12dp),我有这样的:的截图
if I set the stoke width to a higher value (12dp), I've got this: Screenshot
推荐答案
我发现这个页面上的解决办法:<一href="http://www.betaful.com/2012/01/programmatic-shapes-in-android/">http://www.betaful.com/2012/01/programmatic-shapes-in-android/
I found the solution on this page: http://www.betaful.com/2012/01/programmatic-shapes-in-android/
在Android中,当你画一个行程,它吸引了行程在您绘制形状的边界的中心。正如你所看到的,行程越来越裁剪的图像的边界。幸运的是,你可以在画布上进行转换。我们想要做的是改变我们的笔划形状比边界略小 - !并有一个矩阵运算为的
matrix.setRectToRect(new RectF(0, 0, canvas.getClipBounds().right, canvas.getClipBounds().bottom),
new RectF(strokeWidth/2, strokeWidth/2, canvas.getClipBounds().right - strokeWidth/2,
canvas.getClipBounds().bottom - strokeWidth/2),
Matrix.ScaleToFit.FILL);
修改
...在链接的注释部分更好,更容易找到解决办法:
... Better and easier solution find in the comment section of the link:
正如你可以在Android文档中看到的,在资源形状绘制对象实际上是映射到GradientDrawable,而不是一个ShapeDrawable:的
所以,我有以下code正常使用:
And so, I've got the following code working perfectly:
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(Color.TRANSPARENT);
drawable.setShape(GradientDrawable.OVAL);
drawable.setStroke((int)cET.dpToPx(2), Color.parseColor("#EEEEEE"));
drawable.setSize((int)cET.dpToPx(240), (int)cET.dpToPx(240));
这篇关于椭圆形编程方式创建剪辑时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!