要在Android中使用ImageView只有一个圆角的圆角,可以使用以下步骤:
RoundCornerImageView
的自定义ImageView类,继承自ImageView类。public class RoundCornerImageView extends ImageView {
private Path mClipPath;
private float mCornerRadius;
public RoundCornerImageView(Context context) {
super(context);
init();
}
public RoundCornerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RoundCornerImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mClipPath = new Path();
mCornerRadius = getResources().getDimension(R.dimen.corner_radius);
}
@Override
protected void onDraw(Canvas canvas) {
int saveCount = canvas.save();
mClipPath.reset();
RectF rect = new RectF(0, 0, getWidth(), getHeight());
mClipPath.addRoundRect(rect, mCornerRadius, mCornerRadius, Path.Direction.CW);
canvas.clipPath(mClipPath);
super.onDraw(canvas);
canvas.restoreToCount(saveCount);
}
}
res/values
文件夹中创建一个名为dimens.xml
的资源文件,并在其中定义圆角的半径。
20dp
这样,你就可以在Android中使用ImageView只有一个圆角的圆角了。
上一篇:Android - ImageButton在CardView中的onClickListener不起作用
下一篇:Android - In-App Updates. 在哪里指定更新(新的APK上传到Play商店)是立即更新还是灵活更新?