Android实现动态圆环的图片头像控件

先看效果图:

现在大部分的app上难免会使用到圆形头像,所以今天我给大家分享一个单独使用的,并且周围带有圆环动画的花哨圆形头像控件,本控件是在圆形头像控件基础上实现的,只是在其周围再画一些不同大小的圆而已,就可以实现如图的效果。

圆形头像的基本原理是将设置的资源文件转化成Bitmap,然后通过BitmapShader类将Bitmap成为Paint的渲染器,然后在onDraw()中通过canvas.drawCircle(rx,ry,radius,paint);画布上画圆,而这个圆就是形成了圆形头像。

在xml中通过src设置的资源文件在ImageView的setImageDrawable(drawable)方法中可以得到Drawable类型的图像,然后再将Drawable转成Bitmap就可以了

public void setImageDrawable(Drawable drawable) {
  super.setImageDrawable(drawable);
  mBitmap = getBitmapFromDrawable(drawable);
  setup();
}
private Bitmap getBitmapFromDrawable(Drawable drawable) { 

  if (drawable == null) {
   return null;
  } 

  if (drawable instanceof BitmapDrawable) {
   //从bitmap中间裁剪出最大的正方形
   Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
   return getMaxSquareCenter(bitmap);
   //return ((BitmapDrawable) drawable).getBitmap();
  } 

  try {
   Bitmap bitmap; 

   if (drawable instanceof ColorDrawable) {
    bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
   } else {
    int min = Math.min(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    bitmap = Bitmap.createBitmap(min, min, BITMAP_CONFIG);
   } 

   Canvas canvas = new Canvas(bitmap);
   int left,top,right,buttom;
   int width = canvas.getWidth();
   int height = canvas.getHeight();
   int abs = Math.abs(width - height);
   if(width <= height){
    left = 0;
    top = (height - abs) / 2;
    right = width;
    buttom = height - top;
   }else{
    left = (width - abs) / 2;
    top = 0;
    right = width - left;
    buttom = height;
   }
   //drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
   drawable.setBounds(left, top, right, buttom);
   drawable.draw(canvas);
   return bitmap;
  } catch (OutOfMemoryError e) {
   return null;
  }
 }
/**
  * 从bitmap中间裁剪出最大的正方形
  * @param bitmap
  * @return
  */
 private Bitmap getMaxSquareCenter(Bitmap bitmap){
  int w = bitmap.getWidth(); // 得到图片的宽,高
  int h = bitmap.getHeight();
  int cropWidth = w >= h ? h : w;// 裁切后所取的正方形区域边长
  return Bitmap.createBitmap(bitmap, (w - cropWidth)/2 , (h - cropWidth)/2, cropWidth, cropWidth, null, false);
 }

获取到Bitmap对象后就可以将Bitmap缩小一倍后,在将其画在画布上,这样就有地方来画周围的圆环了。

private void updateShaderMatrix() {
  float scale;
  float dx = 0;
  float dy = 0; 

  mShaderMatrix.set(null); 

  if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
   scale = mDrawableRect.height() / (float) mBitmapHeight / 2; //将图片缩放在正中间 缩小一倍
   dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
  } else {
   scale = mDrawableRect.width() / (float) mBitmapWidth / 2; //将图片缩放在正中间 缩小一倍
   dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
  } 

  mShaderMatrix.setScale(scale , scale );
 //在x轴上平移mDrawableRadius,就在正中间了
  mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth + mDrawableRadius, (int) (dy + 0.5f) + mBorderWidth); 

  mBitmapShader.setLocalMatrix(mShaderMatrix);
 }

下面就是画图片周围的圆环了,就是在图片的外围画两个圆,一个半径大点,颜色浅点,一个半径小点,颜色深点就可以了,然后通过Handler通过延时操作,不断的改变两个圆的半径大小和颜色的深浅,重绘就可以了

 private float mChangeRateBorder;//记录外圆执行动画时半径变化率
 private float mChangeRateOuter;//记录内圆执行动画时半径变化率
 private float mChangeRateInner;//记录图片边框执行动画时半径变化率
 private float mChangeRange;//变化范围,View半径的1/6
//*******执行动画*******//
 //外圆执行动画时半径变化率
 private float mRateOuter[] = {
   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
   0,0.1f,0.2f,0.3f,0.4f,0.5f,0.6f,0.7f,0.8f,0.9f,1.0f
   ,0.92f,0.88f,0.85f,0.82f,0.76f,0.72f,0.68f,0.60f,0.54f,0.48f,
   0.40f,0.33f,0.28f,0.20f};
 //内圆执行动画时半径变化率
 private float mRateInner[] = {
   -1,-1,-1,-1,-1,
   0,0.1f,0.2f,0.3f,0.4f,0.5f,0.6f,0.7f,0.8f,0.9f,1.0f
   ,0.92f,0.88f,0.84f,0.80f,0.72f,0.67f,0.60f,0.54f,0.48f,-1f
   ,-1f,-1f,-1f,-1f,-1f,-1f,-1f,-1f,-1f};
 //图片边框执行动画时半径变化率
 private float mRateBorder[] = {
   0,0.1f,0.2f,0.3f,0.4f,0.5f,0.6f,0.7f,0.8f,0.9f,1.0f
   ,0.92f,0.90f,0.84f,0.78f,0.72f,0.64f,0.58f,-1f,-1f,-1f
   ,-1f,-1f,-1f,-1f,-1f,-1f,-1f,-1f,-1f,-1f
   ,-1f,-1f,-1f,-1f}; 

 private int mRateIndex;//动画变化率的索引

Handler不断改变半径的变化率和画笔的颜色

/**
  * 按住执行动画
  */
 private Handler mHandler = new Handler(){
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   int index = mRateIndex ++;
   mChangeRateBorder = mRateBorder[(index)% mRateBorder.length];
   setPaintCorlor(mBorderPaint,mChangeRateBorder,DEFAULT_BORDER_COLOR);
   setPaintAlpha(mBorderPaint,(index)% mRateBorder.length,mRateBorder);
   mChangeRateOuter = mRateOuter[(index) % mRateOuter.length];
   setPaintCorlor(mOuterPaint,mChangeRateOuter,mOuterPaintColor);
   setPaintAlpha(mOuterPaint,(index) % mRateOuter.length,mRateOuter);
   mChangeRateInner = mRateInner[(index) % mRateInner.length];
   setPaintCorlor(mInnerPaint,mChangeRateInner,mInnerPaintColor);
   setPaintAlpha(mInnerPaint,(index) % mRateInner.length,mRateInner); 

   //System.out.println("---------mChangeRate:"+mChangeRateBorder);
   invalidate();
   mHandler.removeCallbacksAndMessages(null);
   mHandler.sendEmptyMessageDelayed(0,30);
  }
 };

每执行一次handleMessage()就会触发(invalidate())View重绘,onDraw中不断的重绘,只需改变每个Circle的半径即可

@Override
 protected void onDraw(Canvas canvas) { 

  if (getDrawable() == null) {
   return;
  } 

  //画动画的图形
  canvas.drawCircle(getWidth() / 2,getHeight() / 2,getChangeRadiusOuter(mOuterRadius),mOuterPaint);
  canvas.drawCircle(getWidth() / 2,getHeight() / 2,getChangeRadiusInner(mInnerRadius),mInnerPaint); 

  canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);
  canvas.drawCircle(getWidth() / 2, getHeight() / 2, getChangeRadiusBorder(mBorderRadius), mBorderPaint);
 }

根据Handler中给的变化率来计算每个Circle的半径

private float getChangeRadiusBorder(float radius){ 

  return mChangeRateBorder * mChangeRange + radius; 

 }
 private float getChangeRadiusOuter(float radius){ 

  return mChangeRateOuter * mChangeRange + radius; 

 }
 private float getChangeRadiusInner(float radius){ 

  return mChangeRateInner * mChangeRange + radius; 

 }

这样在不断的向Handler发送消息时,也就会不断的触发重绘,不断改变外围圆的大小,形成上图所见的动画效果
如下四个方法,就是来控制圆环所展示的不同状态

/**
  * 开始动画
  */
 public void startAnim(){
 //让外围的圆环动起来
  mHandler.sendEmptyMessageDelayed(0,30); 

 }
 /**
  * 停止动画
  */
 public void stopAnim(){
 //停止外圆环的动画,展示默认的大小
  mHandler.removeCallbacksAndMessages(null);
  mChangeRateBorder = 0;
  mChangeRateOuter = 0;
  mChangeRateInner = 0; 

  initAnimColor(); 

  enterAnim();
 }
 /**
  * 进入动画
  */
 public void enterAnim(){
 //展示外圆环,显示默认大小
  mHandlerEnter.sendEmptyMessage(0);
 } 

 /**
  * 退出动画
  */
 public void exitAnim(){
 //隐藏外圆环
  mHandlerExit.sendEmptyMessage(0);
 }

好了,动态头像到这基本就实现了,进入动画和退出动画的代码就不贴出来分析了,如果发现bug或者有任何意见欢迎留言。

完整源码

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ImageView; 

/**
 * Created by cj_28 on 2016/10/15.
 */
public class DynamicAvatarView extends ImageView { 

 private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP; 

 private static final int COLORDRAWABLE_DIMENSION = 1;
 private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888; 

 private Bitmap mBitmap;
 private BitmapShader mBitmapShader; 

 private final Matrix mShaderMatrix = new Matrix();
 private final Paint mBitmapPaint = new Paint();
 private final Paint mBorderPaint = new Paint();
 private final Paint mOuterPaint = new Paint();
 private final Paint mInnerPaint = new Paint(); 

 private static final int DEFAULT_BORDER_WIDTH = 3;
 private static final int DEFAULT_BORDER_COLOR = Color.WHITE;
 private static final int OUTER_PAINT_COLOR = Color.parseColor("#55FFFFFF");
 private static final int INNER_PAINT_COLOR = Color.parseColor("#66FFFFFF"); 

 private int mBorderColor = DEFAULT_BORDER_COLOR;
 private int mBorderWidth = DEFAULT_BORDER_WIDTH; 

 private int mOuterPaintColor = OUTER_PAINT_COLOR;
 private int mInnerPaintColor = INNER_PAINT_COLOR; 

 private int mBitmapWidth;
 private int mBitmapHeight; 

 private final RectF mDrawableRect = new RectF();
 private final RectF mBorderRect = new RectF(); 

 private float mDrawableRadius;//显示的图片
 private float mBorderRadius;//..//显示的图片上的边框
 private float mOuterRadius;//外层动画
 private float mInnerRadius;//..//内层动画 

 private float mRealDrawableRadius;//这是View没有被缩放之前的mDrawableRadius的半径
 private float mRealBorderRadius;//.. 

 private boolean mReady;
 private boolean mSetupPending; 

 private float mChangeRateBorder;//记录外圆执行动画时半径变化率
 private float mChangeRateOuter;//记录内圆执行动画时半径变化率
 private float mChangeRateInner;//记录图片边框执行动画时半径变化率
 private float mChangeRange;//变化范围,View半径的1/6 

 //*******执行动画*******//
 //外圆执行动画时半径变化率
 private float mRateOuter[] = {
   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
   0,0.1f,0.2f,0.3f,0.4f,0.5f,0.6f,0.7f,0.8f,0.9f,1.0f
   ,0.92f,0.88f,0.85f,0.82f,0.76f,0.72f,0.68f,0.60f,0.54f,0.48f,
   0.40f,0.33f,0.28f,0.20f};
 //内圆执行动画时半径变化率
 private float mRateInner[] = {
   -1,-1,-1,-1,-1,
   0,0.1f,0.2f,0.3f,0.4f,0.5f,0.6f,0.7f,0.8f,0.9f,1.0f
   ,0.92f,0.88f,0.84f,0.80f,0.72f,0.67f,0.60f,0.54f,0.48f,-1f
   ,-1f,-1f,-1f,-1f,-1f,-1f,-1f,-1f,-1f};
 //图片边框执行动画时半径变化率
 private float mRateBorder[] = {
   0,0.1f,0.2f,0.3f,0.4f,0.5f,0.6f,0.7f,0.8f,0.9f,1.0f
   ,0.92f,0.90f,0.84f,0.78f,0.72f,0.64f,0.58f,-1f,-1f,-1f
   ,-1f,-1f,-1f,-1f,-1f,-1f,-1f,-1f,-1f,-1f
   ,-1f,-1f,-1f,-1f}; 

 //private int mColor[] = {0x55FFFFFF,0x44FFFFFF,0x33FFFFFF,0x22FFFFFF,0x11FFFFFF,0x00FFFFFF,0x00FFFFFF,0x00FFFFFF,0x00FFFFFF,0x00FFFFFF};
 private int mRateIndex;//动画变化率的索引 

 //*******进入动画*******//
 //外圆执行动画时半径变化率
 private float mRateOuterEnter[] = {
   -2,-2,//外圆要缩小2个mChangeRange才会完全隐藏
   -2,-2,-2,-2,-2,
   -0.8f,-0.6f,-0.4f,-0.2f,-0.1f,
   0,0.1f,0.2f,0.3f,0.4f,0.5f
   ,0.44f,0.4f,0.35f,0.3f,0.25f
   ,0.20f,0.15f,0.1f,0.0f};
 //内圆执行动画时半径变化率
 private float mRateInnerEnter[] = {
   -1,-1,//外圆要缩小1个mChangeRange才会完全隐藏
   -0.8f,-0.6f,-0.4f,-0.2f,-0.1f,
   0,0.1f,0.2f,0.3f,0.35f,0.4f
   ,0.45f,0.5f,0.45f,0.4f,0.35f
   ,0.3f,0.25f,0.2f,0.15f,0.1f
   ,0.05f,0f,0f,0f};
 private int mRateIndexEnter;//进入动画变化率的索引 

 //*******退出动画*******//
 //外圆执行动画时半径变化率
 private float mRateOuterExit[] = { 

   0.0f,-0.2f,-0.4f,-0.6f,-0.8f
   ,-1f,-1.2f,-1.4f,-1.6f,-1.8f
   ,-2f};
 //内圆执行动画时半径变化率
 private float mRateInnerExit[] = { 

   0.0f,-0.1f,-0.2f,-0.3f,-0.4f
   ,-0.5f,-0.6f,-0.7f,-0.8f,-0.9f
   ,-1f};
 private int mRateIndexExit;//进入动画变化率的索引 

 /**
  * 按住执行动画
  */
 private Handler mHandler = new Handler(){
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   int index = mRateIndex ++;
   mChangeRateBorder = mRateBorder[(index)% mRateBorder.length];
   setPaintCorlor(mBorderPaint,mChangeRateBorder,DEFAULT_BORDER_COLOR);
   setPaintAlpha(mBorderPaint,(index)% mRateBorder.length,mRateBorder);
   mChangeRateOuter = mRateOuter[(index) % mRateOuter.length];
   setPaintCorlor(mOuterPaint,mChangeRateOuter,mOuterPaintColor);
   setPaintAlpha(mOuterPaint,(index) % mRateOuter.length,mRateOuter);
   mChangeRateInner = mRateInner[(index) % mRateInner.length];
   setPaintCorlor(mInnerPaint,mChangeRateInner,mInnerPaintColor);
   setPaintAlpha(mInnerPaint,(index) % mRateInner.length,mRateInner); 

   //System.out.println("---------mChangeRate:"+mChangeRateBorder);
   invalidate();
   mHandler.removeCallbacksAndMessages(null);
   mHandler.sendEmptyMessageDelayed(0,30);
  }
 }; 

 private Handler mHandlerEnter = new Handler(){
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   int index = mRateIndexEnter ++;
   if(index >= mRateOuterEnter.length) {
    mRateIndexEnter = 0;
    mHandlerEnter.removeCallbacksAndMessages(null);
    return;
   } 

   mChangeRateOuter = mRateOuterEnter[(index) % mRateOuterEnter.length]; 

   mChangeRateInner = mRateInnerEnter[(index) % mRateInnerEnter.length]; 

   invalidate();
   mHandlerEnter.removeCallbacksAndMessages(null);
   mHandlerEnter.sendEmptyMessageDelayed(0,20);
  }
 }; 

 private Handler mHandlerExit = new Handler(){
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   int index = mRateIndexExit ++;
   if(index >= mRateOuterExit.length) {
    mRateIndexExit = 0;
    mHandlerExit.removeCallbacksAndMessages(null);
    return;
   } 

   mChangeRateOuter = mRateOuterExit[(index) % mRateOuterExit.length]; 

   mChangeRateInner = mRateInnerExit[(index) % mRateInnerExit.length]; 

   invalidate();
   mHandlerExit.removeCallbacksAndMessages(null);
   mHandlerExit.sendEmptyMessageDelayed(0,20);
  }
 }; 

 /**
  * 设置outer和inner的画笔颜色
  * @param paint
  * @param rate
  * @param color
  */
 private void setPaintCorlor(Paint paint,float rate,int color){
  if(rate < 0){
   paint.setColor(Color.TRANSPARENT);
  }else{
   paint.setColor(color);
  }
 } 

 /**
  * 设置透明度
  * @param paint
  * @param index
  * @param rate
  */
 private void setPaintAlpha(Paint paint,int index,float[] rate){
  int pre = index -1;
  if(pre >= 0 ){
   if(rate[pre] > rate[index] && rate[index] > 0){
    int color = paint.getColor(); 

    int colorTransparent = color & 0xff000000;
    int colorValue = color & 0x00ffffff;
    colorTransparent = colorTransparent >>> 7;
    paint.setColor((int)(rate[index] * colorTransparent) << 7 | colorValue); 

   }
  }
 } 

 public DynamicAvatarView(Context context) {
  this(context,null);
 } 

 public DynamicAvatarView(Context context, AttributeSet attrs) {
  this(context, attrs,0); 

 } 

 public DynamicAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  super.setScaleType(SCALE_TYPE); 

  //可以执行了
  mReady = true; 

  if (mSetupPending) {
   setup();
   mSetupPending = false;
  }
 } 

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

  int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  int heightSize = MeasureSpec.getSize(heightMeasureSpec); 

  int size = Math.min(widthSize, heightSize); 

  super.onMeasure(MeasureSpec.makeMeasureSpec(size,widthMode), MeasureSpec.makeMeasureSpec(size,heightMode));
 } 

 @Override
 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 

  super.onLayout(changed, left, top, right, bottom);
  enterAnim();
 } 

 @Override
 protected void onDraw(Canvas canvas) { 

  if (getDrawable() == null) {
   return;
  } 

  //画动画的图形
  canvas.drawCircle(getWidth() / 2,getHeight() / 2,getChangeRadiusOuter(mOuterRadius),mOuterPaint);
  canvas.drawCircle(getWidth() / 2,getHeight() / 2,getChangeRadiusInner(mInnerRadius),mInnerPaint); 

  canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);
  canvas.drawCircle(getWidth() / 2, getHeight() / 2, getChangeRadiusBorder(mBorderRadius), mBorderPaint);
 } 

 private float getChangeRadiusBorder(float radius){ 

  return mChangeRateBorder * mChangeRange + radius; 

 }
 private float getChangeRadiusOuter(float radius){ 

  return mChangeRateOuter * mChangeRange + radius; 

 }
 private float getChangeRadiusInner(float radius){ 

  return mChangeRateInner * mChangeRange + radius; 

 } 

 private void initAnimColor(){
  mOuterPaint.setStyle(Paint.Style.FILL);
  mOuterPaint.setAntiAlias(true);
  mOuterPaint.setColor(mOuterPaintColor);
  mInnerPaint.setStyle(Paint.Style.FILL);
  mInnerPaint.setAntiAlias(true);
  mInnerPaint.setColor(mInnerPaintColor);
  //图片边框(默认白色)
  mBorderPaint.setColor(mBorderColor); 

  mOuterRadius = mRealBorderRadius / 6 * 5;
  mInnerRadius = mRealBorderRadius / 6 * 4;
  mChangeRange = mRealBorderRadius / 6;
  mRateIndex = 0;
 } 

 /**
  * 开始动画
  */
 public void startAnim(){
  mHandler.sendEmptyMessageDelayed(0,30); 

 }
 /**
  * 停止动画
  */
 public void stopAnim(){
  mHandler.removeCallbacksAndMessages(null);
  mChangeRateBorder = 0;
  mChangeRateOuter = 0;
  mChangeRateInner = 0;
//  mBorderPaint.setColor(DEFAULT_BORDER_COLOR);
//  mOuterPaint.setColor(mOuterPaintColor);
//  mInnerPaint.setColor(mInnerPaintColor);
//  mRateIndex = 0;
  initAnimColor(); 

  //invalidate();
  enterAnim();
 }
 /**
  * 进入动画
  */
 public void enterAnim(){
  mHandlerEnter.sendEmptyMessage(0);
 } 

 /**
  * 退出动画
  */
 public void exitAnim(){
  mHandlerExit.sendEmptyMessage(0);
 } 

 /**
  * 设置外圆动画的颜色
  * @param outerPaintColor
  */
 public void setOuterPaintColor(int outerPaintColor) {
  if (outerPaintColor == mOuterPaintColor) {
   return;
  }
  mOuterPaintColor = outerPaintColor;
  mOuterPaint.setColor(mOuterPaintColor);
  invalidate();
 } 

 /**
  * 设置内圆动画的颜色
  * @param innerPaintColor
  */
 public void setInnerPaintColor(int innerPaintColor) {
  if (innerPaintColor == mInnerPaintColor) {
   return;
  }
  mInnerPaintColor = innerPaintColor;
  mInnerPaint.setColor(mInnerPaintColor);
  invalidate();
 } 

 /**
  * 设置图片边框的颜色
  * @param borderColor
  */
 public void setBorderColor(int borderColor) {
  if (borderColor == mBorderColor) {
   return;
  } 

  mBorderColor = borderColor;
  mBorderPaint.setColor(mBorderColor);
  invalidate();
 } 

 /**
  * 设置图片边框的宽度
  * @param borderWidth
  */
 public void setBorderWidth(int borderWidth) {
  if (borderWidth == mBorderWidth) {
   return;
  } 

  mBorderWidth = borderWidth;
  setup();
 } 

 @Override
 public ScaleType getScaleType() {
  return SCALE_TYPE;
 } 

 @Override
 public void setScaleType(ScaleType scaleType) {
  if (scaleType != SCALE_TYPE) {
   throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));
  }
 } 

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  //只有在此方法中调用setup,setup中的getWidth方法得到的值才不会是0,
  setup();
 } 

 @Override
 public void setImageBitmap(Bitmap bm) {
  super.setImageBitmap(bm);
  mBitmap = getMaxSquareCenter(bm);
  setup();
 } 

 /**
  * mxl中设置src就会走此方法
  * @param drawable
  */
 @Override
 public void setImageDrawable(Drawable drawable) {
  super.setImageDrawable(drawable);
  mBitmap = getBitmapFromDrawable(drawable);
  setup();
 } 

 @Override
 public void setImageResource(int resId) {
  super.setImageResource(resId);
  mBitmap = getBitmapFromDrawable(getDrawable());
  setup();
 } 

 /**
  * 从bitmap中间裁剪出最大的正方形
  * @param bitmap
  * @return
  */
 private Bitmap getMaxSquareCenter(Bitmap bitmap){
  int w = bitmap.getWidth(); // 得到图片的宽,高
  int h = bitmap.getHeight();
  int cropWidth = w >= h ? h : w;// 裁切后所取的正方形区域边长
  return Bitmap.createBitmap(bitmap, (w - cropWidth)/2 , (h - cropWidth)/2, cropWidth, cropWidth, null, false);
 } 

 private Bitmap getBitmapFromDrawable(Drawable drawable) { 

  if (drawable == null) {
   return null;
  } 

  if (drawable instanceof BitmapDrawable) {
   //从bitmap中间裁剪出最大的正方形
   Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
   return getMaxSquareCenter(bitmap);
   //return ((BitmapDrawable) drawable).getBitmap();
  } 

  try {
   Bitmap bitmap; 

   if (drawable instanceof ColorDrawable) {
    bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
   } else {
    int min = Math.min(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    bitmap = Bitmap.createBitmap(min, min, BITMAP_CONFIG);
   } 

   Canvas canvas = new Canvas(bitmap);
   int left,top,right,buttom;
   int width = canvas.getWidth();
   int height = canvas.getHeight();
   int abs = Math.abs(width - height);
   if(width <= height){
    left = 0;
    top = (height - abs) / 2;
    right = width;
    buttom = height - top;
   }else{
    left = (width - abs) / 2;
    top = 0;
    right = width - left;
    buttom = height;
   }
   //drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
   drawable.setBounds(left, top, right, buttom);
   drawable.draw(canvas);
   return bitmap;
  } catch (OutOfMemoryError e) {
   return null;
  }
 } 

 private void setup() {
  //只有执行过构造函数之后,所有的成员才被初始化完毕
  if (!mReady) {
   mSetupPending = true;
   return;
  } 

  if (mBitmap == null) {
   return;
  } 

  mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 

  mBitmapPaint.setAntiAlias(true);
  mBitmapPaint.setShader(mBitmapShader); 

  mBorderPaint.setStyle(Paint.Style.STROKE);
  mBorderPaint.setAntiAlias(true);
  mBorderPaint.setColor(mBorderColor);
  mBorderPaint.setStrokeWidth(mBorderWidth); 

  mBitmapHeight = mBitmap.getHeight();
  mBitmapWidth = mBitmap.getWidth();
  //图片边框设置的范围
  mBorderRect.set(0, 0, getWidth(), getHeight());
  mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2) / 2;
  mRealBorderRadius = 2 * mBorderRadius;
  //图片显示的范围
  mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width() - mBorderWidth, mBorderRect.height() - mBorderWidth);
  //让图片显示的范围是控件大小的一半
  mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2) / 2;
  mRealDrawableRadius = 2 * mDrawableRadius; 

  updateShaderMatrix();
  initAnimColor();
  invalidate();
 } 

 private void updateShaderMatrix() {
  float scale;
  float dx = 0;
  float dy = 0; 

  mShaderMatrix.set(null); 

  if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
   scale = mDrawableRect.height() / (float) mBitmapHeight / 2; //将图片缩放在正中间
   dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
  } else {
   scale = mDrawableRect.width() / (float) mBitmapWidth / 2;
   dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
  } 

  mShaderMatrix.setScale(scale , scale );
  mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth + mDrawableRadius, (int) (dy + 0.5f) + mBorderWidth); 

  mBitmapShader.setLocalMatrix(mShaderMatrix);
 } 

} 

参考:https://github.com/hdodenhof/CircleImageView

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android实现带头像的用户注册页面

    1.首先是注册页面的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"

  • Android手机拍照或选取图库图片作为头像

    package zhangpgil.photo; import java.io.File; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import android.content.Intent; import

  • Android中通过访问本地相册或者相机设置用户头像实例

    目前几乎所有的APP在用户注册时都会有设置头像的需求,大致分为三种情况: (1)通过获取本地相册的图片,经过裁剪后作为头像. (2)通过启动手机相机,现拍图片然后裁剪作为头像. (3)在APP中添加一些自带的头像资源,供用户选择(不够人性化,目前很少使用). 这次我们简单介绍下通过获取本地相册以及相机拍摄的方法设置头像,实现思路如下: (1)通过startActivityForResult方法,分别传递调用系统相册的Intent和调用相机拍照的Intent来做选择 (2)调用Android系统中

  • Android仿微信QQ设置图形头像裁剪功能

    最近在做毕业设计,想有一个功能和QQ一样可以裁剪头像并设置圆形头像,额,这是设计狮的一种潮流. 而纵观现在主流的APP,只要有用户系统这个功能,这个需求一般都是在(bu)劫(de)难(bu)逃(xue)! 图片裁剪实现方式有两种,一种是利用系统自带的裁剪工具,一种是使用开源工具Cropper.本节就为大家带来如何使用系统自带的裁剪工具进行图片裁剪~ 还是先来个简单的运行图. 额,简单说下,我待会会把代码写成小demo分享给大家,在文章末尾会附上github链接,需要的可以自行下载~ 下面来简单分

  • Android实现本地上传图片并设置为圆形头像

    先从本地把图片上传到服务器,然后根据URL把头像处理成圆形头像. 因为上传图片用到bmob的平台,所以要到bmob(http://www.bmob.cn)申请密钥. 效果图: 核心代码: 复制代码 代码如下: public class MainActivity extends Activity {         private ImageView iv;         private String appKey="";                //填写你的Applicatio

  • Android实现用户头像更换操作

    你以为头像更换很容易?或许对于用户来讲,在微信上更换一个头像只是点击头像,选择拍照或相册,裁剪返回而已.但是对于程序员来说,要实现其实也挺吃力的(小火柴花了一个下午整理~_~). 正如用户使用那样,代码的实现也是按照操作的顺序而逐步展开.如下图: 接下来主要来讲解一下代码: 1. 弹框选择相册或拍照 比较简单的方式就是直接使用AlertDialog弹出选项供用户进行选择 public static void showImagePickDialog(final Activity activity)

  • Android仿微信群聊头像

    工作中需要实现仿钉钉群头像的一个功能,就是个人的头像拼到一起显示,看了一下市场上的APP好像微信的群聊头像是组合的,QQ的头像不是,别的好像也没有了. 给大家分享一下怎么实现的吧.首先我们先看一下效果图: 好了,下面说一下具体怎么实现的: 实现思路 1.首先获取Bitmap图片(本地.网络) 2.创建一个指定大小的缩略图 3.组合Bitmap图片 很简单,本地图片需要我们从本地读取,如果是网络图片我们也可以根据URL来获取bitmap进行组合 具体实现过程 1.布局文件: <LinearLayo

  • Android通过手机拍照或从本地相册选取图片设置头像

    像微信.QQ.微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式: 1.让用户通过选择本地相册之类的图片库中已有的图像,裁剪后作为头像. 2.让用户启动手机的相机拍照,拍完照片后裁剪,然后作为头像. 我现在写一个简单的完整代码例子,说明如何在android中实现上述两个头像设置功能. MainActivity.Java文件: package zhangpgil.photo; import java.io.File; import android.support.v7.app.A

  • Android个人中心的头像上传,图片编码及截取实例

    首先需要有网络权限,然后我们这里匹配的网络请求是之前封装好的Okhttp. 非常的简单方便,直接复制进去,依赖一下包,然后调用方法即可. 这里是把图片转换成Base64.decode(imageString, Base64.DEFAULT); 转成Base64编码上传.具体内容也不少,需要完全整明白,还是要花点时间慢慢看的. 先看看简单的效果图: 那么万事具备,只欠东风了.直接上代码: public class MainActivity extends AppCompatActivity imp

  • Android自定义控件仿QQ编辑和选取圆形头像

    android大家都有很多需要用户上传头像的需求,有的是选方形,有的是圆角矩形,有的是圆形. 首先我们要做一个处理图片的自定义控件,把传入的图片,经过用户选择区域,处理成一定的形状. 有的app是通过在图片上画一个矩形区域表示选中的内容,有的则是通过双指放大缩小,拖动图片来选取图片.圆形头像,还是改变图片比较好 圆形区域可调节大小. 这个自定义View的图像部分分为三个,背景图片,半透明蒙层,和亮色区域--还是直接贴代码得了 package com.example.jjj.widget; imp

随机推荐