自定义ImageView实现圆形头像
来源:互联网 发布:csgo优化fps 编辑:程序博客网 时间:2024/05/21 07:04
public class MyImageView extends ImageView { public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); } private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Bitmap mBitmap; private BitmapShader mBitmapShader; private Matrix mMatrix = new Matrix(); @Override protected void onDraw(Canvas canvas) { Bitmap bitmap = getBitmap(getDrawable()); if (bitmap != null) { int width = getWidth(); int height = getHeight(); int minSize = Math.min(width, height); float dstWidth = minSize; float dstHeight = minSize; if (mBitmapShader == null || !bitmap.equals(mBitmap)) { mBitmap = bitmap; mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); } if (mBitmapShader != null) { mMatrix.setScale(dstWidth / bitmap.getWidth(), dstHeight / bitmap.getHeight()); mBitmapShader.setLocalMatrix(mMatrix); } mPaint.setShader(mBitmapShader); float radius = minSize / 2.0f; canvas.drawCircle(radius, radius, radius, mPaint); } else { super.onDraw(canvas); } } private Bitmap getBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof ColorDrawable) { Rect rect = drawable.getBounds(); int width = rect.right - rect.left; int height = rect.bottom - rect.top; int color = ((ColorDrawable) drawable).getColor(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawARGB(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color)); return bitmap; } else { return null; } }}
1 0
- 自定义ImageView实现圆形头像
- 自定义ImageView实现圆形头像
- Android 简易 自定义ImageView 实现圆形头像
- android 圆形头像 自定义ImageView
- 自定义ImageView实现圆形头像 利用Xferomede实现
- android 自定义ImageView控件实现圆形图片-适用于用户头像
- Android ImageView实现圆形头像
- 自定义ImageView完成圆形头像自定义
- androidの自定义ImageView圆形头像
- Android 自定义ImageView, 圆形六边形头像
- android自定义圆形头像view,继承imageview
- 自定义圆形ImageView(仿QQ头像)
- 【Android】自定义圆形ImageView(圆形头像 可指定大小)
- 自定义带 vip 标识的 圆形头像(圆形ImageView)
- 【Android】自定义圆形ImageView(圆形头像 可指定大小)
- 自定义控件实现圆形头像
- 自定义ImageView实现圆形图片
- 自定义ImageView实现圆形图像
- 一些优美的git文章
- JAVA缓存的实现
- 161103
- 11.2运行异常和编译异常
- java中的缓存技术该如何实现
- 自定义ImageView实现圆形头像
- 接口和抽象类的区别
- codeforces 730K
- Wampserver图标黄色解决
- 计算方法_拉格朗日插值_C++实现_方法1
- ERROR: The node /hbase is not in ZooKeeper. It should have been written by the master. Check the val
- Caffe学习(1):Ubuntu16.04上Caffe配置安装(Only CPU)
- 数据结构实验之二叉树七:叶子问题
- SAX解析XML