在一个对话框中,我希望它有一个背景,带有3dp绿色圆角边框。我已经实现了以下内容:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="30dp"
android:background="@drawable/layout_round_corners_image"
android:splitMotionEvents="false" >
.....
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/strip1">
<shape
android:shape="rectangle" >
<solid android:color="@android:color/transparent" />
<stroke
android:width="3dp"
android:color="@color/green" />
<corners android:radius="35px" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
</item>
</layer-list>
对话框现在没有显示任何图像(strip1),只有一个带有3dp绿色圆角边框的黑色(空白)背景。
图像如何成为对话框的背景,其角落在绿色圆角边框内调整?
谢谢!
答案 0 :(得分:0)
它的工作原理是创建一个新类,编码如下:
public class RoundedImageView extends ImageView {
public RoundedImageView(Context context) {
super(context);
}
public RoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
int radius = (int) getResources().getDimension(R.dimen.corner_radius); // angle of round corners
Path clipPath = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
}
<com.example.abc.RoundedImageView
android:id="@+id/bgd_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="@dimen/dp2"
android:scaleType="fitXY"
android:src="@drawable/strip1" />