在工作中我們經常會用到圓形的圖片,但是大部分的圖片都是方形的,這就需要工程師去自定義圓形圖片,好的圓形圖片庫有很多,大家可以在github或者泡網等網站上搜一下。今天要說的就是簡單幾行代碼搞定圓形圖片,既然是簡單當然就沒有考慮各種花哨的屬性,以及可擴展性。
首先我的原圖是一張盛開的方形的花朵:
image.png
下面我們就來用兩種方式實現簡單幾行代碼搞定圓形圖片,且看方式一:
package com.ebanswers.iqcore;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* Created by air on 2017/8/24.
*/
public class CirImageView extends ImageView {
private int mWidth, mHeight;
public CirImageView(Context context) {
this(context, null);
}
public CirImageView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CirImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;//獲取寬度
mHeight = h;//獲取高度
setScaleType(ScaleType.CENTER_CROP);//設置縮放類型
}
@Override
protected void onDraw(Canvas canvas) {
/*
*super.onDraw(canvas);以上的代碼部分即為關鍵所在
*思路是:在圖片繪制之前先將畫布裁剪成圓形的,然后繪制在該畫布上的圖片不就是圓的了嗎?
*注意一定要在繪制之前進行裁剪操作,如果在 super.onDraw(canvas);之后進行則無效果
*/
Path path = new Path();
path.addCircle(mWidth / 2, mHeight / 2, Math.min(mWidth / 2, mHeight / 2), Path.Direction.CCW);
canvas.clipPath(path);
super.onDraw(canvas);
}
}
看看運行效果如何:
image.png
再來看看方式二,利用PorterDuff.Mode來實現:
package com.ebanswers.iqcore;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* Created by air on 2017/8/24.
*/
public class CirImageViewTwo extends ImageView {
private Paint mPaint;
private int mWidth, mHeight;
public CirImageViewTwo(Context context) {
this(context, null);
}
public CirImageViewTwo(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CirImageViewTwo(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHeight = h;
setScaleType(ScaleType.CENTER_CROP);
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//設置抗鋸齒
mPaint.setDither(true);//設置防止抖動
mPaint.setAlpha(0);//設置Alpha等于0
mPaint.setStyle(Paint.Style.FILL);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//在新的layer上進行繪制
int id = canvas.saveLayer(0,0,mWidth,mHeight,null);
canvas.drawColor(Color.WHITE);
canvas.drawCircle(mWidth / 2, mHeight / 2, Math.min(mWidth / 2, mHeight / 2), mPaint);
canvas.restoreToCount(id);
}
}
運行效果:
image.png
眼尖的朋友會發現圓形圖片背景是是白色的,沒錯因為canvas.drawColor(Color.WHITE);設置了顏色為白色,至于PorterDuff.Mode的講解可以看看我的另一篇文章。本篇僅僅介紹了最簡單的實現方式,只作為一種思路的記錄,可以基于此完善擴展。