70、二维码生成+圆形头像

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

<?xml version="1.0" encoding="UTF-8"?>
<resources>
   <declare-styleable name="CircleImageView">
        <attr name="border_width" format="dimension" />
        <attr name="border_color" format="color" />
    </declare-styleable>
</resources>
  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     @Override
100     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     @Override
112     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     @Override
145     public void setImageBitmap(Bitmap bm) {
146         super.setImageBitmap(bm);
147         mBitmap = bm;
148         setup();
149     }
150 
151     @Override
152     public void setImageDrawable(Drawable drawable) {
153         super.setImageDrawable(drawable);
154         mBitmap = getBitmapFromDrawable(drawable);
155         setup();
156     }
157 
158     @Override
159     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 <LinearLayout
 2         android:id="@+id/layout_head"
 3         android:layout_width="170dp"
 4         android:layout_height="170dp"
 5         android:layout_marginTop="70dp"
 6         android:background="@android:color/black"
 7         android:gravity="center"
 8         android:layout_gravity="center">
 9         <com.trainee.app.widget.CircleImageView
10             android:id="@+id/img_head_circle"
11             android:layout_width="25dp"
12             android:layout_height="25dp"
13             android:layout_gravity="center"
14             android:src="@drawable/login_touxiang" />
15 </LinearLayout>
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();
    }

}
原文地址:https://www.cnblogs.com/androidsj/p/5051380.html