现在Android对于图片的处理要求越来越高,今天主要实现下面截图的圆形图片功能。
圆形圆角图片的主要实现方法有三种:
1. PorterDuffXfermode
2. BitmapShader
3. OutlineProvider
代码如下:
1. 通过PorterDuffXferMode来实现:
package com.jackie.processing.image.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.util.AttributeSet; import android.view.View; import com.jackie.processing.image.R; /** * Created by Jackie on 2016/2/27. * 圆角矩形 */ public class RoundRectXfermodeView extends View { private Bitmap mSrcBitmap; private Bitmap mDstBitmap; private Paint mPaint; public RoundRectXfermodeView(Context context) { this(context, null); } public RoundRectXfermodeView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RoundRectXfermodeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } private void initView() { //关闭硬件加速 setLayerType(LAYER_TYPE_SOFTWARE, null); mSrcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test1); mDstBitmap = Bitmap.createBitmap(mSrcBitmap.getWidth(), mSrcBitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(mDstBitmap); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); //dst // canvas.drawCircle(300, 200, 150, mPaint); canvas.drawRoundRect(0, 0, mSrcBitmap.getWidth(), mSrcBitmap.getHeight(), 50, 50, mPaint); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //src canvas.drawBitmap(mSrcBitmap, 0, 0, mPaint); mPaint.setXfermode(null); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(mDstBitmap, 0, 0, null); } }2. 通过BitmapShader实现
package com.jackie.processing.image.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Shader; import android.util.AttributeSet; import android.view.View; import com.jackie.processing.image.R; /** * Created by Jackie on 2016/2/29. */ public class BitmapShaderView extends View { private Bitmap mBitmap; private Paint mPaint; public BitmapShaderView(Context context) { this(context, null); } public BitmapShaderView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public BitmapShaderView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test2); BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setShader(shader); canvas.drawCircle(300, 200, 200, mPaint); } }3. 通过OutlineProvider来实现
ViewOutlineProvider mViewOutlineProvider = new ViewOutlineProvider() { @Override public void getOutline(View view, Outline outline) { outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), view.getHeight() / 2); } }; mImageView.setOutlineProvider(mViewOutlineProvider);效果如下: