自定义带 vip 标识的 圆形头像(圆形ImageView)

举报
再见孙悟空_ 发表于 2022/01/12 22:36:41 2022/01/12
【摘要】 import java.lang.ref.WeakReference; import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.graphics.C...

  
import java.lang.ref.WeakReference;
 


  
  1. import android.content.Context;
  2. import android.content.res.TypedArray;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.PorterDuff;
  8. import android.graphics.PorterDuffXfermode;
  9. import android.graphics.RectF;
  10. import android.graphics.Xfermode;
  11. import android.graphics.drawable.Drawable;
  12. import android.util.AttributeSet;
  13. import android.util.TypedValue;
  14. import android.widget.ImageView;
  15. import com.ccvideo.R;
  16. public class RoundImageView extends ImageView {
  17. private Paint mPaint;
  18. private Xfermode mXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
  19. private WeakReference<Bitmap> mWeakBitmap;
  20. private WeakReference<Bitmap> mMaskWeakBitmap;
  21. private WeakReference<Canvas> mWeakCanvas;
  22. public static final int TYPE_CIRCLE = 0;
  23. public static final int TYPE_ROUND = 1;
  24. private static final int BORDER_RADIUS_DEFAULT = 10;
  25. private static final int BORDER_WIDTH_DEFAULT = 0;
  26. private static final int BORDER_COLOR_DEFAULT = R.color.black_alpha_percent_10;
  27. private int mShapeType;
  28. private int mBorderRadius;
  29. private int mBorderWidth;
  30. private int mBorderColor;
  31. private int mOverLayColor;
  32. public RoundImageView(Context context, int BorderWidth, int BorderColor) {
  33. this(context, null);
  34. this.mBorderWidth = BorderWidth;
  35. this.mBorderColor = BorderColor;
  36. mPaint = new Paint();
  37. mPaint.setAntiAlias(true);
  38. }
  39. public RoundImageView(Context context, AttributeSet attrs) {
  40. super(context, attrs);
  41. mPaint = new Paint();
  42. mPaint.setAntiAlias(true);
  43. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
  44. mBorderRadius = a.getDimensionPixelSize(R.styleable.RoundImageView_border_radius,
  45. (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
  46. BORDER_RADIUS_DEFAULT, getResources().getDisplayMetrics()));// Default 10dp
  47. mShapeType = a.getInt(R.styleable.RoundImageView_type, TYPE_CIRCLE);
  48. mBorderWidth = a.getDimensionPixelSize(R.styleable.RoundImageView_border_width,
  49. (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
  50. BORDER_WIDTH_DEFAULT, getResources().getDisplayMetrics()));
  51. mBorderColor = a.getColor(R.styleable.RoundImageView_border_color, BORDER_COLOR_DEFAULT);
  52. mOverLayColor = a.getColor(R.styleable.RoundImageView_overlay, android.R.color.transparent);
  53. a.recycle();
  54. }
  55. @Override
  56. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  57. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  58. if (mShapeType == TYPE_CIRCLE) {
  59. int width = Math.min(getMeasuredWidth(), getMeasuredHeight());
  60. // setMeasuredDimension(width, width);
  61. }
  62. }
  63. @Override
  64. public void invalidate() {
  65. mWeakBitmap = null;
  66. mMaskWeakBitmap = null;
  67. mWeakCanvas = null;
  68. super.invalidate();
  69. }
  70. @Override
  71. protected void onDraw(Canvas canvas) {
  72. Drawable drawable = getDrawable();
  73. if (drawable == null || getWidth() == 0 || getHeight() == 0) {
  74. return;
  75. }
  76. Bitmap bitmap = mWeakBitmap == null ? null : mWeakBitmap.get();
  77. if (bitmap == null || bitmap.isRecycled()) {
  78. bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
  79. }
  80. Canvas drawCanvas = mWeakCanvas == null ? null : mWeakCanvas.get();
  81. if (drawCanvas == null) {
  82. drawCanvas = new Canvas(bitmap);
  83. }
  84. float scale = 1.0f;
  85. int drawableWidth = drawable.getIntrinsicWidth();
  86. int drawableHeight = drawable.getIntrinsicHeight();
  87. if (mShapeType == TYPE_ROUND) {
  88. scale = Math.max(getWidth() * 1.0f / drawableWidth, getHeight() * 1.0f / drawableHeight);
  89. } else {
  90. scale = getWidth() * 1.0f / Math.min(drawableWidth, drawableHeight);
  91. }
  92. drawable.setBounds(0, 0, (int) (scale * drawableWidth), (int) (scale * drawableHeight));
  93. drawable.draw(drawCanvas);
  94. mPaint.reset();
  95. mPaint.setColor(mOverLayColor);
  96. drawCanvas.drawColor(mOverLayColor);
  97. mPaint.reset();
  98. mPaint.setFilterBitmap(false);
  99. mPaint.setXfermode(mXfermode);
  100. Bitmap maskBitmap = mMaskWeakBitmap == null ? null : mMaskWeakBitmap.get();
  101. if (maskBitmap == null || maskBitmap.isRecycled()) {
  102. maskBitmap = getBitmap();
  103. mMaskWeakBitmap = new WeakReference<Bitmap>(maskBitmap);
  104. }
  105. drawCanvas.drawBitmap(maskBitmap, 0, 0, mPaint);
  106. mPaint.setXfermode(null);
  107. if (mBorderWidth > 0) {
  108. drawCircleBorder(drawCanvas);
  109. }
  110. canvas.drawBitmap(bitmap, 0, 0, null);
  111. }
  112. private Bitmap getBitmap() {
  113. Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
  114. Canvas canvas = new Canvas(bitmap);
  115. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  116. paint.setColor(Color.BLACK);
  117. if (mShapeType == TYPE_ROUND) {
  118. canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()),
  119. mBorderRadius, mBorderRadius, paint);
  120. } else {
  121. canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2 - mBorderWidth, paint);
  122. }
  123. return bitmap;
  124. }
  125. private void drawCircleBorder(Canvas canvas) {
  126. Paint paint = new Paint();
  127. paint.setAntiAlias(true);
  128. paint.setFilterBitmap(true);
  129. paint.setDither(true);
  130. paint.setColor(mBorderColor);
  131. paint.setStyle(Paint.Style.STROKE);
  132. paint.setStrokeWidth(mBorderWidth);
  133. if (mShapeType == TYPE_ROUND) {
  134. canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()),
  135. mBorderRadius, mBorderRadius, paint);
  136. } else {
  137. canvas.drawCircle(getWidth() / 2, getWidth() / 2, (getWidth() - mBorderWidth) / 2, paint);
  138. }
  139. }
  140. public void setBorderWidth(int borderWidth) {
  141. this.mBorderWidth = mBorderWidth;
  142. }
  143. public void setBorderColor(int borderColor) {
  144. this.mBorderColor = mBorderColor;
  145. }
  146. }

  

  
  1. package com.yizhibo.video.view;
  2. import java.lang.ref.WeakReference;
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.Canvas;
  8. import android.graphics.Color;
  9. import android.graphics.Matrix;
  10. import android.graphics.Paint;
  11. import android.graphics.PorterDuff;
  12. import android.graphics.PorterDuffXfermode;
  13. import android.graphics.Xfermode;
  14. import android.graphics.drawable.Drawable;
  15. import android.util.AttributeSet;
  16. import android.util.TypedValue;
  17. import android.widget.ImageView;
  18. import com.ccvideo.R;
  19. public class MyUserPhoto extends ImageView {
  20. private static final int BORDER_SMALL_WIDTH_DEFAULT = 10;
  21. private static final int BORDER_SMALL_HEIGHT_DEFAULT = 10;
  22. private static final int IS_VIP = 1;
  23. private static final int BORDER_WIDTH_DEFAULT = 0;
  24. private static final int BORDER_COLOR_DEFAULT = R.color.black_alpha_percent_10;
  25. private int mSmallImageWidth;
  26. private int mSmallImageHeight;
  27. private int mIsVip;
  28. private int mBorderWidth;
  29. private int mBorderColor;
  30. private int mSmallIconResId;
  31. private Paint mPaint;
  32. private Xfermode mXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
  33. private WeakReference<Bitmap> mWeakBitmap;
  34. private WeakReference<Bitmap> mMaskWeakBitmap;
  35. private WeakReference<Canvas> mWeakCanvas;
  36. public MyUserPhoto(Context context) {
  37. super(context);
  38. }
  39. public MyUserPhoto(Context context, AttributeSet attrs) {
  40. super(context, attrs);
  41. mPaint = new Paint();
  42. mPaint.setAntiAlias(true);
  43. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyUserPhoto);
  44. mSmallImageHeight = a.getDimensionPixelSize(R.styleable.MyUserPhoto_small_height,
  45. BORDER_SMALL_HEIGHT_DEFAULT);
  46. mSmallImageWidth = a.getDimensionPixelSize(R.styleable.MyUserPhoto_small_width,
  47. BORDER_SMALL_WIDTH_DEFAULT);
  48. mIsVip = a.getInt(R.styleable.MyUserPhoto_is_vip, 0);
  49. mSmallIconResId = a.getResourceId(R.styleable.MyUserPhoto_small_icon,
  50. R.drawable.floating_screen_v);
  51. mBorderWidth = a.getDimensionPixelSize(R.styleable.MyUserPhoto_border_width,
  52. (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
  53. BORDER_WIDTH_DEFAULT, getResources().getDisplayMetrics()));
  54. mBorderColor = a.getColor(R.styleable.MyUserPhoto_border_color, BORDER_COLOR_DEFAULT);
  55. setWillNotDraw(false);
  56. a.recycle();
  57. }
  58. @Override
  59. public void invalidate() {
  60. mWeakBitmap = null;
  61. mMaskWeakBitmap = null;
  62. mWeakCanvas = null;
  63. super.invalidate();
  64. }
  65. @Override
  66. protected void onDraw(Canvas canvas) {
  67. Drawable drawable = getDrawable();
  68. if (drawable == null || getWidth() == 0 || getHeight() == 0) {
  69. return;
  70. }
  71. Bitmap bitmap = mWeakBitmap == null ? null : mWeakBitmap.get();
  72. if (bitmap == null || bitmap.isRecycled()) {
  73. bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
  74. mWeakBitmap = new WeakReference<Bitmap>(bitmap);
  75. }
  76. Canvas drawCanvas = mWeakCanvas == null ? null : mWeakCanvas.get();
  77. if (drawCanvas == null) {
  78. drawCanvas = new Canvas(bitmap);
  79. mWeakCanvas = new WeakReference<Canvas>(drawCanvas);
  80. }
  81. float scale = 1.0f;
  82. int drawableWidth = drawable.getIntrinsicWidth();
  83. int drawableHeight = drawable.getIntrinsicHeight();
  84. scale = getWidth() * 1.0f / Math.min(drawableWidth, drawableHeight);
  85. drawable.setBounds(0, 0, (int) (scale * drawableWidth), (int) (scale * drawableHeight));
  86. drawable.draw(drawCanvas);
  87. mPaint.reset();
  88. mPaint.setFilterBitmap(false);
  89. mPaint.setXfermode(mXfermode);
  90. Bitmap maskBitmap = mMaskWeakBitmap == null ? null : mMaskWeakBitmap.get();
  91. if (maskBitmap == null || maskBitmap.isRecycled()) {
  92. maskBitmap = getMaskBitmap();
  93. mMaskWeakBitmap = new WeakReference<Bitmap>(maskBitmap);
  94. }
  95. drawCanvas.drawBitmap(maskBitmap, 0, 0, mPaint);
  96. mPaint.setXfermode(null);
  97. if (mBorderWidth > 0) {
  98. drawCircleBorder(drawCanvas);
  99. }
  100. if (mIsVip == IS_VIP && (mSmallImageWidth > 0 || mSmallImageHeight > 0)) {
  101. drawSmallIcon(drawCanvas);
  102. }
  103. canvas.drawBitmap(bitmap, 0, 0, null);
  104. }
  105. private Bitmap getMaskBitmap() {
  106. Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
  107. Canvas canvas = new Canvas(bitmap);
  108. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  109. paint.setColor(Color.BLACK);
  110. canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2 - mBorderWidth, paint);
  111. return bitmap;
  112. }
  113. private void drawCircleBorder(Canvas canvas) {
  114. Paint paint = new Paint();
  115. paint.setAntiAlias(true);
  116. paint.setFilterBitmap(true);
  117. paint.setDither(true);
  118. paint.setColor(mBorderColor);
  119. paint.setStyle(Paint.Style.STROKE);
  120. paint.setStrokeWidth(mBorderWidth);
  121. canvas.drawCircle(getWidth() / 2, getWidth() / 2, (getWidth() - mBorderWidth) / 2, paint);
  122. }
  123. private void drawSmallIcon(Canvas canvas) {
  124. int smallIconSize = mSmallImageWidth;
  125. Bitmap smallIconBitmap = BitmapFactory.decodeResource(getResources(), mSmallIconResId);
  126. smallIconBitmap = scaleImage(smallIconBitmap, smallIconSize, smallIconSize);
  127. float marginTop = getWidth() / 2.0f + (float)Math.sqrt(2) * (getHeight() / 2.0f) / 2.0f
  128. - smallIconSize / 2.0f;
  129. float marginLeft = marginTop;
  130. canvas.drawBitmap(smallIconBitmap, marginLeft, marginTop, null);
  131. smallIconBitmap.recycle();
  132. }
  133. private Bitmap scaleImage(Bitmap bitmap, float newWidth, float newHeight) {
  134. float width = bitmap.getWidth();
  135. float height = bitmap.getHeight();
  136. Matrix matrix = new Matrix();
  137. float scaleWidth = newWidth / width;
  138. float scaleHeight = newHeight / height;
  139. matrix.postScale(scaleWidth, scaleHeight);
  140. return Bitmap.createBitmap(bitmap, 0, 0, (int) width, (int) height, matrix, true);
  141. }
  142. public ImageView getRoundImageView() {
  143. return this;
  144. }
  145. public void setIsVip(int isVip) {
  146. if (mIsVip != isVip) {
  147. mIsVip = isVip;
  148. invalidate();
  149. }
  150. }
  151. }

文章来源: wukong.blog.csdn.net,作者:再见孙悟空_,版权归原作者所有,如需转载,请联系作者。

原文链接:wukong.blog.csdn.net/article/details/49890705

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。