java Android RoundeImage视图不工作
我有一个用户定义的类,它扩展了ImageView
类。我想要在ImageView
上有圆角
当我应用该类时,它在图形布局中显示图像的圆角。但是当我运行这个项目时,它并没有向图像显示圆角
我没有更改MainActivity.java
圆形图像视图。java
package com.round.image;
import 安卓.content.Context;
import 安卓.graphics.Canvas;
import 安卓.graphics.Path;
import 安卓.graphics.RectF;
import 安卓.util.AttributeSet;
import 安卓.widget.ImageView;
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) {
float radius = 90.0f; // 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);
}
}
主要活动。xml
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.round.image.MainActivity" >
<com.round.image.RoundedImageView
安卓:id="@+id/imageView1"
安卓:layout_width="200dp"
安卓:layout_height="200dp"
安卓:layout_alignParentBottom="true"
安卓:layout_alignParentRight="true"
安卓:layout_marginBottom="110dp"
安卓:layout_marginRight="40dp"
安卓:src="@drawable/bg_image" />
</RelativeLayout>
# 1 楼答案
使用上面的链接。 这对我很管用。希望这会有用的
http://www.androidhub4you.com/2014/10/android-custom-shape-imageview-rounded.html