我正在使用这个自定义ImageView类来基本模拟具有圆角的图像:
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;
public class CustomImageView extends ImageView {
public static float radius = 18.0f;
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
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.sixerstrivia.CustomImageView
android:id="@+id/customIV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/button_background" />
这适用于我之前运行Android 3.x.x的另一台设备。我现在在Android 4.2.2上运行它并且角落不再是四舍五入的。我的问题是:是什么原因导致我无法在运行4.2.2的当前设备上运行?有没有办法解决这个问题?谢谢!