博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
70、二维码生成+圆形头像
阅读量:6261 次
发布时间:2019-06-22

本文共 9639 字,大约阅读时间需要 32 分钟。

values/attrs.xml 自定义控件属性

3 /**  4  *  圆形图片自定义控件  5  */  7 import android.content.Context;  8 import android.content.res.TypedArray;  9 import android.graphics.Bitmap; 10 import android.graphics.BitmapShader; 11 import android.graphics.Canvas; 12 import android.graphics.Color; 13 import android.graphics.Matrix; 14 import android.graphics.Paint; 15 import android.graphics.RectF; 16 import android.graphics.Shader; 17 import android.graphics.drawable.BitmapDrawable; 18 import android.graphics.drawable.ColorDrawable; 19 import android.graphics.drawable.Drawable; 20 import android.util.AttributeSet; 21 import android.widget.ImageView; 23 import com.trainee.app.R; 24  25 public class CircleImageView extends ImageView {
27 private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP; 29 private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888; 30 private static final int COLORDRAWABLE_DIMENSION = 1; 32 private static final int DEFAULT_BORDER_WIDTH = 0; 33 private static final int DEFAULT_BORDER_COLOR = Color.BLACK; 35 private final RectF mDrawableRect = new RectF(); 36 private final RectF mBorderRect = new RectF(); 38 private final Matrix mShaderMatrix = new Matrix(); 39 private final Paint mBitmapPaint = new Paint(); 40 private final Paint mBorderPaint = new Paint(); 41 42 private int mBorderColor = DEFAULT_BORDER_COLOR; 43 private int mBorderWidth = DEFAULT_BORDER_WIDTH; 44 45 private Bitmap mBitmap; 46 private BitmapShader mBitmapShader; 47 private int mBitmapWidth; 48 private int mBitmapHeight; 49 50 private float mDrawableRadius; 51 private float mBorderRadius; 52 53 private boolean mReady; 54 private boolean mSetupPending; 55 56 public CircleImageView(Context context) { 57 super(context); 58 } 59 60 public CircleImageView(Context context, AttributeSet attrs) { 61 this(context, attrs, 0); 62 } 63 64 public CircleImageView(Context context, AttributeSet attrs, int defStyle) { 65 super(context, attrs, defStyle); 66 super.setScaleType(SCALE_TYPE); 67 68 TypedArray a = context.obtainStyledAttributes(attrs, 69 R.styleable.CircleImageView, defStyle, 0); 70 71 mBorderWidth = a.getDimensionPixelSize( 72 R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH); 73 mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, 74 DEFAULT_BORDER_COLOR); 76 a.recycle(); 78 mReady = true; 79 80 if (mSetupPending) { 81 setup(); 82 mSetupPending = false; 83 } 84 } 85 86 @Override 87 public ScaleType getScaleType() { 88 return SCALE_TYPE; 89 } 90 91 @Override 92 public void setScaleType(ScaleType scaleType) { 93 if (scaleType != SCALE_TYPE) { 94 throw new IllegalArgumentException(String.format( 95 "ScaleType %s not supported.", scaleType)); 96 } 97 } 98 99 @Override100 protected void onDraw(Canvas canvas) {101 if (getDrawable() == null) {102 return;103 }104 105 canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius,106 mBitmapPaint);107 canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius,108 mBorderPaint);109 }110 111 @Override112 protected void onSizeChanged(int w, int h, int oldw, int oldh) {113 super.onSizeChanged(w, h, oldw, oldh);114 setup();115 }116 117 public int getBorderColor() {118 return mBorderColor;119 }120 121 public void setBorderColor(int borderColor) {122 if (borderColor == mBorderColor) {123 return;124 }126 mBorderColor = borderColor;127 mBorderPaint.setColor(mBorderColor);128 invalidate();129 }130 131 public int getBorderWidth() {132 return mBorderWidth;133 }134 135 public void setBorderWidth(int borderWidth) {136 if (borderWidth == mBorderWidth) {137 return;138 }140 mBorderWidth = borderWidth;141 setup();142 }143 144 @Override145 public void setImageBitmap(Bitmap bm) {146 super.setImageBitmap(bm);147 mBitmap = bm;148 setup();149 }150 151 @Override152 public void setImageDrawable(Drawable drawable) {153 super.setImageDrawable(drawable);154 mBitmap = getBitmapFromDrawable(drawable);155 setup();156 }157 158 @Override159 public void setImageResource(int resId) {160 super.setImageResource(resId);161 mBitmap = getBitmapFromDrawable(getDrawable());162 setup();163 }164 165 private Bitmap getBitmapFromDrawable(Drawable drawable) {166 if (drawable == null) {167 return null;168 }169 170 if (drawable instanceof BitmapDrawable) {171 return ((BitmapDrawable) drawable).getBitmap();172 }174 try {175 Bitmap bitmap;176 177 if (drawable instanceof ColorDrawable) {178 bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION,179 COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);180 } else {181 bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),182 drawable.getIntrinsicHeight(), BITMAP_CONFIG);183 }185 Canvas canvas = new Canvas(bitmap);186 drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());187 drawable.draw(canvas);188 return bitmap;189 } catch (OutOfMemoryError e) {190 return null;191 }192 }193 194 private void setup() {195 if (!mReady) {196 mSetupPending = true;197 return;198 }200 if (mBitmap == null) {201 return;202 }204 mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP,205 Shader.TileMode.CLAMP);206 207 mBitmapPaint.setAntiAlias(true);208 mBitmapPaint.setShader(mBitmapShader);209 210 mBorderPaint.setStyle(Paint.Style.STROKE);211 mBorderPaint.setAntiAlias(true);212 mBorderPaint.setColor(mBorderColor);213 mBorderPaint.setStrokeWidth(mBorderWidth);214 216 mBitmapHeight = mBitmap.getHeight();217 mBitmapWidth = mBitmap.getWidth();218 221 mBorderRect.set(0, 0, getWidth(), getHeight());222 mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2,223 (mBorderRect.width() - mBorderWidth) / 2);224 225 mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width()226 - mBorderWidth, mBorderRect.height() - mBorderWidth);227 mDrawableRadius = Math.min(mDrawableRect.height() / 2,228 mDrawableRect.width() / 2);229 230 updateShaderMatrix();231 invalidate();232 }233 234 private void updateShaderMatrix() {235 float scale;236 float dx = 0;237 float dy = 0;238 239 mShaderMatrix.set(null);240 241 if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width()242 * mBitmapHeight) {243 scale = mDrawableRect.height() / (float) mBitmapHeight;244 dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;245 } else {246 scale = mDrawableRect.width() / (float) mBitmapWidth;247 dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;248 }249 250 mShaderMatrix.setScale(scale, scale);251 mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth,252 (int) (dy + 0.5f) + mBorderWidth);253 254 mBitmapShader.setLocalMatrix(mShaderMatrix);255 }256 257 }
1 
9
15
private LinearLayout layout_head;private CircleImageView img_head_circle;img_head_circle = (CircleImageView) view.findViewById(R.id.img_head_circle);layout_head = (LinearLayout) view.findViewById(R.id.layout_head);try {     String contentString = UserInfo.vipcode;  // 得到二维码内容     if (!contentString.equals("")) {         //根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(600*600)         Bitmap qrCodeBitmap = EncodingHandler.createQRImage2(contentString, 700, 700);         //------------------添加logo部分------------------//         /*Bitmap logoBmp = BitmapFactory.decodeResource(mActivity.getResources(),                        R.drawable.ic_launcher);*/         //二维码和logo合并         Bitmap bitmap = Bitmap.createBitmap(qrCodeBitmap.getWidth(), qrCodeBitmap                        .getHeight(), qrCodeBitmap.getConfig());         Canvas canvas = new Canvas(bitmap);         //二维码         canvas.drawBitmap(qrCodeBitmap, 0, 0, null);         //logo绘制在二维码中央         /*canvas.drawBitmap(logoBmp, qrCodeBitmap.getWidth() / 2                        - logoBmp.getWidth() / 2, qrCodeBitmap.getHeight()                        / 2 - logoBmp.getHeight() / 2, null);*/         //------------------添加logo部分------------------//         //   qrImgImageView.setImageBitmap(bitmap);         layout_head.setBackground(ImageTools.bitmapToDrawable(bitmap));     } else {         Toast.makeText(mActivity, "Text can not be empty", Toast.LENGTH_SHORT).show();     }         mActivity.mToolBitmap.display(img_head_circle, UserInfo.headportraits);    } catch (Exception e) {            e.printStackTrace();    }}

 

转载地址:http://aqzpa.baihongyu.com/

你可能感兴趣的文章
携程Apollo(阿波罗)配置中心使用Google代码风格文件(在Eclipse使用Google代码风格)(配合阿里巴巴代码规约快速设置)...
查看>>
Hadoop(七)HDFS容错机制详解
查看>>
字符串中去除多余的空格保留一个(C#)
查看>>
Python随机字符串验证码
查看>>
SQL中 patindex函数的用法
查看>>
Vmware 虚拟机无法启动
查看>>
LeetCode: Partition List 解题报告
查看>>
如何查看Python对象的属性
查看>>
你所需要知道的一些git 的使用命令:历史
查看>>
mysql explain输出中type的取值说明
查看>>
iPhone开发之 - 苹果推送通知服务(APNs)编程
查看>>
linux下so动态库一些不为人知的秘密(上)
查看>>
文本框设置只读,后台可获取
查看>>
JAVA:URL之String组件
查看>>
架构,改善程序复用性的设计~目录(附核心原代码)
查看>>
逆向反汇编代码推算C++的局部变量
查看>>
100个推荐的图片/内容滑动条
查看>>
秋式广告杀手:广告拦截原理与杀手组织
查看>>
内存溢出
查看>>
如何重启IIS进程
查看>>