Android實(shí)現(xiàn)弧形View或者ImageView功能
在Android項(xiàng)目開發(fā)中,經(jīng)常會遇到下面這兩種弧形布局設(shè)計(jì):

圖一上方是一個(gè)外凸的紅色弧形背景,圖二上方是一個(gè)內(nèi)凹的背景圖片
實(shí)現(xiàn)方案:
圖一可以通過自定義View,繪制出這個(gè)弧形View;
圖二可以繼承自ImageView,并通過clipPth的方式,讓ImageView在弧形區(qū)域內(nèi)繪制實(shí)現(xiàn)。
圖一弧形View具體實(shí)現(xiàn):
圖一弧形背景可以分解成兩部分,矩形部分+弧形部分

矩形部分繪制很簡單,下面弧形部分可以通過二階貝塞爾曲線來繪制
繪制部分代碼如下:
@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);mPaint.setStyle(Paint.Style.FILL);mPaint.setColor(mBgColor);//矩形部分Rect rect = new Rect(0, 0, mWidth, mHeight - mArcHeight);canvas.drawRect(rect, mPaint);//弧形部分Path path = new Path();path.moveTo(0, mHeight - mArcHeight);path.quadTo(mWidth / 2, mHeight, mWidth, mHeight - mArcHeight);????????canvas.drawPath(path,?mPaint);}
ArcView完整代碼:
public class ArcView extends View {private int mWidth;private int mHeight;/*** 弧形高度*/private int mArcHeight;/*** 背景顏色*/private int mBgColor;private Paint mPaint;private Context mContext;public ArcView(Context context) {this(context, null);}public ArcView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public ArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArcView);mArcHeight = typedArray.getDimensionPixelSize(R.styleable.ArcView_arcHeight, 0);mBgColor=typedArray.getColor(R.styleable.ArcView_bgColor,Color.parseColor("#303F9F"));typedArray.recycle();mContext = context;mPaint = new Paint();}protected void onDraw(Canvas canvas) {super.onDraw(canvas);mPaint.setStyle(Paint.Style.FILL);mPaint.setColor(mBgColor);Rect rect = new Rect(0, 0, mWidth, mHeight - mArcHeight);canvas.drawRect(rect, mPaint);Path path = new Path();path.moveTo(0, mHeight - mArcHeight);path.quadTo(mWidth / 2, mHeight, mWidth, mHeight - mArcHeight);????????canvas.drawPath(path,?mPaint);}protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int widthMode = MeasureSpec.getMode(widthMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);if (widthMode == MeasureSpec.EXACTLY) {mWidth = widthSize;}if (heightMode == MeasureSpec.EXACTLY) {mHeight = heightSize;}setMeasuredDimension(mWidth, mHeight);}}
使用:
android:layout_width="match_parent"android:layout_height="250dp"app:arcHeight="80dp"app:bgColor="@color/colorRed"/>
實(shí)現(xiàn)效果:

圖二弧形ImageView實(shí)現(xiàn):
圖二弧形ImageView實(shí)現(xiàn)原理其實(shí)和圖一弧形View是一樣的,只不過是
繼承ImageView,然后裁剪出一個(gè)矩形+弧形區(qū)域繪制范圍,使得ImageView的繪制范圍變成這個(gè)帶弧形的區(qū)域即可。
只要重寫ImageView的onDraw方法,在super.onDraw方法之前通過clipPath裁剪出矩形+弧形繪制區(qū)域
protected void onDraw(Canvas canvas) {Path path = new Path();path.moveTo(0, 0);path.lineTo(0, getHeight());path.quadTo(getWidth() / 2, getHeight() - 2 * mArcHeight, getWidth(), getHeight());path.lineTo(getWidth(), 0);path.close();canvas.clipPath(path);super.onDraw(canvas);}
ArcImageView完整代碼如下:
public class ArcImageView extends ImageView {/**弧形高度*/private int mArcHeight;private static final String TAG = "ArcImageView";public ArcImageView(Context context) {this(context, null);}public ArcImageView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public ArcImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArcImageView);mArcHeight = typedArray.getDimensionPixelSize(R.styleable.ArcImageView_arcHeight, 0);typedArray.recycle();}protected void onDraw(Canvas canvas) {Path path = new Path();path.moveTo(0, 0);path.lineTo(0, getHeight());path.quadTo(getWidth() / 2, getHeight() - 2 * mArcHeight, getWidth(), getHeight());path.lineTo(getWidth(), 0);path.close();canvas.clipPath(path);super.onDraw(canvas);????}}
使用:
<com.arcview.ArcImageViewandroid:layout_width="match_parent"android:layout_height="250dp"app:arcHeight="50dp"android:scaleType="centerCrop"android:src="@drawable/test"/>
實(shí)現(xiàn)效果:

最后附上ArcView和ArcImageView的自定義屬性定義部分,attrs.xml文件
<resources><attr name="arcHeight" format="dimension"/><declare-styleable name="ArcView"><attr name="arcHeight"/><attr name="bgColor" format="color"/>declare-styleable><declare-styleable name="ArcImageView"><attr name="arcHeight"/>declare-styleable>resources>
以上就是這兩種弧形布局實(shí)現(xiàn)方式。
評論
圖片
表情
