android点击左上角划出,使用Android中的Path和RectF在左上角右上角左下角绘制圆角...
有一個Path#addRoundRect()重載,它接受一個包含八個值的float數(shù)組,其中我們可以為四個角中的每一個指定x和y半徑.這些值為[x,y]對,從左上角開始,順時針繞其余部分.對于我們想要舍入的那些角,我們將該對的兩個值都設(shè)置為半徑值,并將它們保留為零,而不是那些.
作為一個說明性示例,一個簡單的方法將返回可在您的代碼段中使用的Path:
private Path getPath(float radius, boolean topLeft, boolean topRight,
boolean bottomRight, boolean bottomLeft) {
final Path path = new Path();
final float[] radii = new float[8];
if (topLeft) {
radii[0] = radius;
radii[1] = radius;
}
if (topRight) {
radii[2] = radius;
radii[3] = radius;
}
if (bottomRight) {
radii[4] = radius;
radii[5] = radius;
}
if (bottomLeft) {
radii[6] = radius;
radii[7] = radius;
}
path.addRoundRect(new RectF(0, 0, getWidth(), getHeight()),
radii, Path.Direction.CW);
return path;
}
根據(jù)您的示例說明,舍入左上角和右上角:
@Override
protected void onDraw(Canvas canvas) {
float radius = getContext().getResources().getDimension(R.dimen.round_corner_radius);
Path path = getPath(radius, true, true, false, false);
canvas.clipPath(path);
super.onDraw(canvas);
}
和往常一樣,我建議盡可能地保持onDraw()方法,移動任何其他地方不必存在的東西.例如,可以在構(gòu)造函數(shù)中檢索radius的資源值,并將其保存在字段中.此外,只有在必要時才能構(gòu)建路徑;即,當視圖的大小改變時,或當半徑或選定的角改變時.
由于我將一個簡單的自定義ImageView放在一起進行測試,我將在此處包含它,因為它演示了以上幾點.此自定義視圖還提供XML屬性,允許在布局中設(shè)置角半徑和圓角.
public class RoundishImageView extends ImageView {
public static final int CORNER_NONE = 0;
public static final int CORNER_TOP_LEFT = 1;
public static final int CORNER_TOP_RIGHT = 2;
public static final int CORNER_BOTTOM_RIGHT = 4;
public static final int CORNER_BOTTOM_LEFT = 8;
public static final int CORNER_ALL = 15;
private static final int[] CORNERS = {CORNER_TOP_LEFT,
CORNER_TOP_RIGHT,
CORNER_BOTTOM_RIGHT,
CORNER_BOTTOM_LEFT};
private final Path path = new Path();
private int cornerRadius;
private int roundedCorners;
public RoundishImageView(Context context) {
this(context, null);
}
public RoundishImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RoundishImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundishImageView);
cornerRadius = a.getDimensionPixelSize(R.styleable.RoundishImageView_cornerRadius, 0);
roundedCorners = a.getInt(R.styleable.RoundishImageView_roundedCorners, CORNER_NONE);
a.recycle();
}
public void setCornerRadius(int radius) {
if (cornerRadius != radius) {
cornerRadius = radius;
setPath();
invalidate();
}
}
public int getCornerRadius() {
return cornerRadius;
}
public void setRoundedCorners(int corners) {
if (roundedCorners != corners) {
roundedCorners = corners;
setPath();
invalidate();
}
}
public boolean isCornerRounded(int corner) {
return (roundedCorners & corner) == corner;
}
@Override
protected void onDraw(Canvas canvas) {
if (!path.isEmpty()) {
canvas.clipPath(path);
}
super.onDraw(canvas);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
setPath();
}
private void setPath() {
path.rewind();
if (cornerRadius >= 1f && roundedCorners != CORNER_NONE) {
final float[] radii = new float[8];
for (int i = 0; i < 4; i++) {
if (isCornerRounded(CORNERS[i])) {
radii[2 * i] = cornerRadius;
radii[2 * i + 1] = cornerRadius;
}
}
path.addRoundRect(new RectF(0, 0, getWidth(), getHeight()),
radii, Path.Direction.CW);
}
}
}
要使XML屬性起作用,需要在< resources>中進行以下操作,您可以將此文件放在項目的res / values /文件夾中,或添加到可能已存在的文件中.
attrs.xml
cornerRadius是一個維度屬性,應(yīng)指定為dp或px值. roundedCorners是一個標志屬性,可以使用豎線字符|來選擇多個角.例如:
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/riv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/magritte"
app:cornerRadius="@dimen/round_corner_radius"
app:roundedCorners="topLeft|topRight" />
總結(jié)
以上是生活随笔為你收集整理的android点击左上角划出,使用Android中的Path和RectF在左上角右上角左下角绘制圆角...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【IfICan】脚步很乱!
- 下一篇: 深入理解机械臂动力学建模