这里参考:http://www.jianshu.com/p/6b5eef0f6f3d
注意: // canvas.drawColor(Color.YELLOW); 这里稍微设置一下,否则可能肉眼看不出裁剪的效果!!!
我的效果图:
package com.example.liqing.mytextview.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
/**
- Created by liqing on 17/6/2.
*/
public class CirecleImageView extends android.support.v7.widget.AppCompatImageView {
private static String TAG = " CirecleImageView :";
private int mRadius; // 圆角的半径
private float mScale; // 缩放比例
// private Paint mPaint=new Paint();
private Paint mPaint;
public CirecleImageView(Context context) {
super(context);
}
public CirecleImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CirecleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//
// public CirecleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
// super(context, attrs, defStyleAttr, defStyleRes);
// }
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d(TAG, "onMeasure: ");
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
int he=getMeasuredHeight();
int wid =getMeasuredWidth();
Log.d(TAG, "onMeasure: he ="+he +" wid="+wid);
mRadius = size / 2;
//setMeasuredDimension(2,2);
setMeasuredDimension(size, size);
}
@Override
protected void onDraw(Canvas canvas) {
Log.d(TAG, "onDraw: ");
super.onDraw(canvas);
mPaint = new Paint();
Bitmap bp = drwableToBitmap(getDrawable());
// BitmapShader 渲染
BitmapShader bitmapShader = new BitmapShader(bp, Shader.TileMode.CLAMP, Shader.TileMode.MIRROR);
mScale = (mRadius * 2.0f) / Math.min(bp.getWidth(), bp.getHeight());
//Matrix控制变换 可以 平移、旋转角度 、倾斜、 缩放
Matrix matrix = new Matrix();
matrix.setScale(mScale, mScale);
// 设置shader的本地矩阵,如果localM为空将重置shader的本地矩阵。
bitmapShader.setLocalMatrix(matrix);
mPaint.setShader(bitmapShader);
mPaint.setAntiAlias(false); //设置画笔为无锯齿
mPaint.setColor(Color.BLACK); //设置画笔颜色
// canvas.drawColor(Color.YELLOW); //白色背景
//"#50000000"
mPaint.setStrokeWidth((float) 3.0); //线宽
//指定画的圆的 圆心x/y r
Log.d(TAG, "onDraw: canvas.drawCircle mRadius =" +mRadius );
canvas.drawCircle(mRadius,mRadius,mRadius,mPaint);
// mPaint.setAntiAlias(false); //设置画笔为无锯齿
// mPaint.setColor(Color.BLACK); //设置画笔颜色
// canvas.drawColor(Color.WHITE); //白色背景
// mPaint.setStrokeWidth((float) 3.0); //线宽
// mPaint.setStyle(Paint.Style.STROKE); //空心效果
// initPaint();
//
// canvas.drawCircle(50, 50, 10, mPaint); //绘制圆形
// canvas.drawCircle(100, 100, 20, mPaint); //绘制圆形
// canvas.drawCircle(150, 150, 30, mPaint); //绘制圆形
// canvas.drawCircle(200, 200, 40, mPaint); //绘制圆形
// canvas.drawCircle(250, 250, 50, mPaint); //绘制圆形
// canvas.drawCircle(300, 300, 60, mPaint); //绘制圆形
// canvas.drawCircle(350, 350, 70, mPaint); //绘制圆形
}
// 2.初始化画笔
private void initPaint() {
mPaint.setColor(Color.BLACK); //设置画笔颜色
mPaint.setStyle(Paint.Style.FILL); //设置画笔模式为填充
mPaint.setStrokeWidth(10f); //设置画笔宽度为10px
}
//把drawable 转为bitmap
public Bitmap drwableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
//BtimapDrawable是Drawable的子类
Log.d(TAG, "drwableToBitmap: BitmapDrawable exsit!");
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
return bitmapDrawable.getBitmap();// 最终 这里的bitmapDrawable就是Bitmap对象了
}
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Log.d(TAG, "drwableToBitmap: width:"+width+ ", height "+height);
//图片压缩 最后一个参数是色彩信息,也是影响图片占有空间的
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// Canvas();创建一个空画布,它必须绑定bitmap才有效,一般以bitmap来创建一个bitmap的操作,
// 当canvas进行绘图,他绘制的内容都绘在bitmap上。
Canvas canvas = new Canvas(bitmap);
// drawable.setBounds(0, 0, width, height);
Log.d(TAG, "drwableToBitmap: drawable.draw(canvas);");
drawable.draw(canvas);
return bitmap;
}
}