1、首先看一下实现效果

android 实现圆形头像_第1张图片

2、首先新建一个自定义view实现ImageView,详细可看注释

package com.example.a550211.xfermode;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.View;import android.widget.ImageView;/** * Created by 550211 on 2017/9/23. */public class MyImageView extends ImageView {    private Paint mPaint;//设置画笔    private Bitmap mBitmap;//获取图片资源    private int width, height;//获取控件宽高    private int mOuterRing = 0;//设置外圈大小    private int outerRingAlpha = 30;//设置外圈透明度    private int color = Color.BLUE;//设置外圈颜色    public MyImageView(Context context) {        this(context, null);    }    public MyImageView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        mPaint = new Paint();    }    public Bitmap getBitmap() {        return mBitmap;    }    public void setBitmap(Bitmap bitmap) {        this.mBitmap = bitmap;    }    public int getmOuterRing() {        return mOuterRing;    }    public void setmOuterRing(int mOuterRing) {        this.mOuterRing = mOuterRing;    }    public int getColor() {        return color;    }    public void setColor(int color) {        this.color = color;    }    public int getOuterRingAlpha() {        return outerRingAlpha;    }    public void setOuterRingAlpha(int outerRingAlpha) {        this.outerRingAlpha = outerRingAlpha;    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        //获取空间宽高        width = View.getDefaultSize(getMeasuredWidth(), widthMeasureSpec);        height = View.getDefaultSize(getMeasuredHeight(), heightMeasureSpec);    }    @Override    protected void onDraw(Canvas canvas) {        Drawable drawable = getDrawable();        Bitmap bitmap;        if (drawable != null) {            if (mBitmap != null) {                bitmap = mBitmap;            } else {                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);            }            Matrix matrix = new Matrix();            //设置图片缩放比例,            if (mOuterRing>0){                if (width > height) {                    matrix.setScale((float) (height) / bitmap.getHeight(), (float) (height) / bitmap.getHeight());                } else {                    matrix.setScale((float) (width) / bitmap.getWidth(), (float) (width) / bitmap.getWidth());                }            }else {                if (width > height) {                    matrix.setScale((float) (width) / bitmap.getWidth(), (float) (width) / bitmap.getWidth());                } else {                    matrix.setScale((float) (height) / bitmap.getHeight(), (float) (height) / bitmap.getHeight());                }            }            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);            //将图片设置为圆形            bitmap = toRoundBitmap(bitmap);            //当外圈大于0的时候要设置外圈加图片的宽度不能大于控件宽度            if (mOuterRing+bitmap.getWidth()>width){                mOuterRing = (width-bitmap.getWidth())/2;            }            //绘制外圈            Paint cPaint = new Paint();            cPaint.setStrokeWidth(2);            cPaint.setColor(color);            cPaint.setAlpha(outerRingAlpha);           // cPaint.setFilterBitmap(true);            cPaint.setAntiAlias(true);            canvas.drawCircle(bitmap.getWidth()/2+mOuterRing,bitmap.getHeight()/2+mOuterRing,bitmap.getWidth()/2+mOuterRing,cPaint);            //绘制图片            canvas.drawBitmap(bitmap, mOuterRing, mOuterRing, mPaint);        } else {            super.onDraw(canvas);        }    }    /**     * 设置图片为圆形     *     * @param bitmap     * @return     */    public Bitmap toRoundBitmap(Bitmap bitmap) {        int width = bitmap.getWidth();        int height = bitmap.getHeight();        float roundPx;        float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;        if (width <= height) {            roundPx = width / 2;            top = 0;            bottom = width;            left = 0;            right = width;            height = width;            dst_left = 0;            dst_top = 0;            dst_right = width;            dst_bottom = width;        } else {            roundPx = height / 2;            float clip = (width - height) / 2;            left = clip;            right = width - clip;            top = 0;            bottom = height;            width = height;            dst_left = 0;            dst_top = 0;            dst_right = height;            dst_bottom = height;        }        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(output);        final int color = 0xff424242;        final Paint paint = new Paint();        final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);        final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);        final RectF rectF = new RectF(dst_left, dst_top, dst_right, dst_bottom);        paint.setAntiAlias(true);        canvas.drawARGB(0, 0, 0, 0);        paint.setColor(color);        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));        canvas.drawBitmap(bitmap, src, dst, paint);        return output;    }}
2、在布局中使用
 
3、代码中使用

package com.example.a550211.xfermode;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {    private MyImageView imageView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aa);        imageView = (MyImageView) findViewById(R.id.MyImageView);        imageView.setBitmap(bitmap);        imageView.setmOuterRing(56);        imageView.setColor(Color.RED);        imageView.setOuterRingAlpha(50);    }}
这样就可以简单实现圆形头像了





更多相关文章

  1. Android 异步加载一张网络图片
  2. android 使图片显示 圆角
  3. android ImageUtils 图片处理工具类
  4. Android 获取SDCard上图片和视频的缩略图
  5. android 网络图片与网页读取
  6. Android 利用TransitionDrawable 实现两张图片渐变切换
  7. android创建数据库(SQLite)保存图片示例

随机推荐

  1. mysql查询优化之100万条数据的一张表优化
  2. MySQL索引知识的一些小妙招总结
  3. MySQL COUNT函数的使用与优化
  4. 解读MySQL的客户端和服务端协议
  5. MySQL 重写查询语句的三种策略
  6. MySQL 可扩展设计的基本原则
  7. 详解MySQL 联合查询优化机制
  8. MySQL Threads_running飙升与慢查询的相
  9. MySQL sql_mode的使用详解
  10. MySQL 数据丢失排查案例