要创建一个反向弧形圆角Drawable/Canvas,可以使用以下代码:
public class ReverseCurveCornerDrawable extends Drawable {
private Paint mPaint;
private int mWidth;
private int mHeight;
private int mRadius;
private Path mPath;
public ReverseCurveCornerDrawable(int color, int radius) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(color);
mRadius = radius;
mPath = new Path();
}
@Override
public void draw(Canvas canvas) {
canvas.drawPath(mPath, mPaint);
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
mPaint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mWidth = bounds.width();
mHeight = bounds.height();
mPath.reset();
mPath.moveTo(0, 0);
mPath.lineTo(mWidth - mRadius, 0);
mPath.quadTo(mWidth, 0, mWidth, mRadius);
mPath.lineTo(mWidth, mHeight);
mPath.lineTo(0, mHeight);
mPath.close();
}
}
这个类继承了Drawable类,可以使用它来进行视图绘制。它使用了一个Path来绘制圆角边缘。在onBoundsChange方法中,它计算出每个角落的位置,并使用quadTo方法创建曲线路径。mRadius是要创建的圆角的半径,mWidth和mHeight是Drawable的宽度和高度。 使用以下步骤创建Drawable对象:
ReverseCurveCornerDrawable drawable = new ReverseCurveCornerDrawable(Color.RED, 50);
imageView.setBackground(drawable); //or set to any view.
设置颜色和半径即可。 这样做可以为
上一篇:Android范围滑块标签重叠