写点什么

android 方式实现 imageview 圆角

用户头像
Android架构
关注
发布于: 2 小时前

public static final int TYPE_ROUND = 1;


/**


  • 圆角大小的默认值


*/


private static final int BODER_RADIUS_DEFAULT = 10;


/**


  • 圆角的大小


*/


private int mBorderRadius;


/**


  • 绘图的 Paint


*/


private Paint mBitmapPaint;


/**


  • 圆角的半径


*/


private int mRadius;


/**


  • 3x3 矩阵,主要用于缩小放大


*/


private Matrix mMatrix;


/**


  • 渲染图像,使用图像为绘制图形着色


*/


private BitmapShader mBitmapShader;


/**


  • view 的宽度


*/


private int mWidth;


private RectF mRoundRect;


/**


  • 构造函数:获取自定义属性


*/


public RoundImageView(Context context, AttributeSet attrs) {


super(context, attrs);


mMatrix = new Matrix();


mBitmapPaint = new Paint();


mBitmapPaint.setAntiAlias(true);


TypedArray a = context.obtainStyledAttributes(attrs,


R.styleable.RoundImageView);


mBorderRadius = a.getDimensionPixelSize(


R.styleable.RoundImageView_borderRadius, (int) TypedValue


.applyDimension(TypedValue.COMPLEX_UNIT_DIP,


BODER_RADIUS_DEFAULT, getResources()


.getDisplayMetrics()));// 默认为 10dp


type = a.getInt(R.styleable.RoundImageView_type, TYPE_CIRCLE);// 默认为 Circle


a.recycle();


}


/**


  • 关于 view 的宽高:主要用于当设置类型为圆形时,我们强制让 view 的宽和高一致


*/


@Override


protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


Log.e("TAG", "onMeasure");


super.onMeasure(widthMeasureSpec, heightMeasureSpec);


/**


  • 如果类型是圆形,则强制改变 view 的宽高一致,以小值为准;如果是圆角则不用管宽高问题


*/


if (type == TYPE_CIRCLE) {


mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());


mRadius = mWidth / 2;


setMeasuredDimension(mWidth, mWidth);


}


}


/**


  • 设置 BitmapShader

  • 绘制


*/


@Override


protected void onDraw(Canvas canvas) {


if (getDrawable() == null) {


return;


}


setUpShader();


if (type == TYPE_ROUND) {


canvas.drawRoundRect(mRoundRect, mBorderRadius, mBorderRadius,


mBitmapPaint);


} else {


canvas.drawCircle(mRadius, mRadius, mRadius, mBitmapPaint);


// drawSomeThing(canvas);


}


}


@Override


protec


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


ted void onSizeChanged(int w, int h, int oldw, int oldh) {


super.onSizeChanged(w, h, oldw, oldh);


// 圆角图片的范围


if (type == TYPE_ROUND)


mRoundRect = new RectF(0, 0, getWidth(), getHeight());


}


/**


  • 初始化 BitmapShader//给 paint 附加一个具有某种魔力(缩放)的 shader

  • drawable 转化为我们的 bitmap

  • 设置 shader 的 mode

  • 设置 scale: 因为最终缩放完成的图片一定要大于我们的 view 的区域

  • scale 设置给 matrix

  • matrix 设置给 shader

  • shader 设置给 paint


*/


private void setUpShader() {


Drawable drawable = getDrawable();


if (drawable == null) {


return;


}


Bitmap bmp = drawableToBitamp(drawable);


// 将 bmp 作为着色器,就是在指定区域内绘制 bmp;此构造方法用的是拉伸 mode: clamp


mBitmapShader = new BitmapShader(bmp, CLAMP, CLAMP);


float scale = 1.0f;


if (type == TYPE_CIRCLE) {


// 拿到 bitmap 宽或高的小值//我们需要画正方形


int bSize = Math.min(bmp.getWidth(), bmp.getHeight());


scale = mWidth * 1.0f / bSize;


} else if (type == TYPE_ROUND) {


// 如果图片的宽或者高与 view 的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们 view 的宽高;所以我们这里取大值;//view 的宽度/bitmap 的宽度=缩放倍数


scale = Math.max(getWidth() * 1.0f / bmp.getWidth(), getHeight()


  • 1.0f / bmp.getHeight());


}


// shader 的变换矩阵,我们这里主要用于放大或者缩小


mMatrix.setScale(scale, scale);


// 设置变换矩阵


mBitmapShader.setLocalMatrix(mMatrix);


// 先给 paint 设置 shader;然后画笔会有所不同哦,例如


mBitmapPaint.setShader(mBitmapShader);


}


/**


  • drawable 转 bitmap

  • @param drawable

  • @return


*/


private Bitmap drawableToBitamp(Drawable drawable) {


if (drawable instanceof BitmapDrawable) {


BitmapDrawable bd = (BitmapDrawable) drawable;


return bd.getBitmap();


}


//以下为 drawable 转 bitmap 的通用方法


int w = drawable.getIntrinsicWidth();


int h = drawable.getIntrinsicHeight();


Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);


Canvas canvas = new Canvas(bitmap);


drawable.setBounds(0, 0, w, h);


drawable.draw(canvas);


return bitmap;


}


/**


  • 添加一个方法

  • bitmap 转 drawable


*/


private Drawable bitmapToDrawable(Bitmap bitmap) {


Drawable drawable = new BitmapDrawable(null, bitmap);


return drawable;


}


//状态存储与恢复


private static final String STATE_INSTANCE = "state_instance";


private static final String STATE_TYPE = "state_type";


private static final String STATE_BORDER_RADIUS = "state_border_radius";


@Override


protected Parcelable onSaveInstanceState() {


Bundle bundle = new Bundle();


bundle.putParcelable(STATE_INSTANCE, super.onSaveInstanceState());


bundle.putInt(STATE_TYPE, type);


bundle.putInt(STATE_BORDER_RADIUS, mBorderRadius);


return bundle;


}


@Override


protected void onRestoreInstanceState(Parcelable state) {


if (state instanceof Bundle) {


Bundle bundle = (Bundle) state;


super.onRestoreInstanceState(((Bundle) state)


.getParcelable(STATE_INSTANCE));


this.type = bundle.getInt(STATE_TYPE);


this.mBorderRadius = bundle.getInt(STATE_BORDER_RADIUS);


} else {


super.onRestoreInstanceState(state);


}

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
android 方式实现imageview圆角