Android实现头像圆角

古人已用三冬足,年少今开万卷余。这篇文章主要讲述Android实现头像圆角相关的知识,希望能为你提供帮助。
先看一下效果图

Android实现头像圆角

文章图片

 
首先在res-> values下新建一个xml命名为attrs
< ?xml version="1.0" encoding="utf-8"?> < resources> < declare-styleable name="RoundImageView"> < attr name="type" format="enum"> < enum name="circle" value="https://www.songbingjia.com/android/1"/> < enum name="round" value="https://www.songbingjia.com/android/2"/> < /attr> < attr name="radius" format="dimension"/> < /declare-styleable> < /resources>

 
然后建一个java类,命名  RoundImageView
Android实现头像圆角

文章图片
Android实现头像圆角

文章图片
1 package com.example.logintest.circleImage; 2 3 import android.annotation.SuppressLint; 4 import android.content.Context; 5 import android.content.res.TypedArray; 6 import android.graphics.Bitmap; 7 import android.graphics.BitmapShader; 8 import android.graphics.Canvas; 9 import android.graphics.Matrix; 10 import android.graphics.Paint; 11 import android.graphics.RectF; 12 import android.graphics.Shader; 13 import android.graphics.drawable.Drawable; 14 import android.os.Build; 15 import android.util.AttributeSet; 16 import android.util.TypedValue; 17 import android.widget.ImageView; 18 import com.example.logintest.R; 19 20 21 /** 22* Created by leo on 17/3/14. 23*/ 24 25 public class RoundImageView extends androidx.appcompat.widget.AppCompatImageView { 26/** 27* 圆形模式 28*/ 29private static final int MODE_CIRCLE = 1; 30/** 31* 普通模式 32*/ 33private static final int MODE_NONE = 0; 34/** 35* 圆角模式 36*/ 37private static final int MODE_ROUND = 2; 38private Paint mPaint; 39private int currMode = 0; 40/** 41* 圆角半径 42*/ 43private int currRound = dp2px(10); 44 45public RoundImageView(Context context) { 46super(context); 47initViews(); 48} 49 50public RoundImageView(Context context, AttributeSet attrs) { 51this(context, attrs, 0); 52} 53 54public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) { 55super(context, attrs, defStyleAttr); 56obtainStyledAttrs(context, attrs, defStyleAttr); 57initViews(); 58} 59 60private void obtainStyledAttrs(Context context, AttributeSet attrs, int defStyleAttr) { 61TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView, defStyleAttr, 0); 62currMode = a.hasValue(R.styleable.RoundImageView_type) ? a.getInt(R.styleable.RoundImageView_type, MODE_NONE) : MODE_NONE; 63currRound = a.hasValue(R.styleable.RoundImageView_radius) ? a.getDimensionPixelSize(R.styleable.RoundImageView_radius, currRound) : currRound; 64a.recycle(); 65} 66 67private void initViews() { 68mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); 69} 70 71@Override 72protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 73/** 74* 当模式为圆形模式的时候,我们强制让宽高一致 75*/ 76if (currMode == MODE_CIRCLE) { 77super.onMeasure(widthMeasureSpec, heightMeasureSpec); 78int result = Math.min(getMeasuredHeight(), getMeasuredWidth()); 79setMeasuredDimension(result, result); 80} else { 81super.onMeasure(widthMeasureSpec, heightMeasureSpec); 82} 83} 84 85@SuppressLint("DrawAllocation") 86@Override 87protected void onDraw(Canvas canvas) { 88Drawable mDrawable = getDrawable(); 89Matrix mDrawMatrix = getImageMatrix(); 90if (mDrawable == null) { 91return; // couldn\'t resolve the URI 92} 93 94if (mDrawable.getIntrinsicWidth() == 0 || mDrawable.getIntrinsicHeight() == 0) { 95return; // nothing to draw (empty bounds) 96} 97 98if (mDrawMatrix == null & & getPaddingTop() == 0 & & getPaddingLeft() == 0) { 99mDrawable.draw(canvas); 100} else { 101final int saveCount = canvas.getSaveCount(); 102canvas.save(); 103 104if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.JELLY_BEAN) { 105if (getCropToPadding()) { 106final int scrollX = getScrollX(); 107final int scrollY = getScrollY(); 108canvas.clipRect(scrollX + getPaddingLeft(), scrollY + getPaddingTop(), 109scrollX + getRight() - getLeft() - getPaddingRight(), 110scrollY + getBottom() - getTop() - getPaddingBottom()); 111} 112} 113canvas.translate(getPaddingLeft(), getPaddingTop()); 114if (currMode == MODE_CIRCLE) {//当为圆形模式的时候 115Bitmap bitmap = drawable2Bitmap(mDrawable); 116mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); 117canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, mPaint); 118} else if (currMode == MODE_ROUND) {//当为圆角模式的时候 119Bitmap bitmap = drawable2Bitmap(mDrawable); 120mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); 121canvas.drawRoundRect(new RectF(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(), getHeight() - getPaddingBottom()), 122currRound, currRound, mPaint); 123} else { 124if (mDrawMatrix != null) { 125canvas.concat(mDrawMatrix); 126} 127mDrawable.draw(canvas); 128} 129canvas.restoreToCount(saveCount); 130} 131} 132 133/** 134* drawable转换成bitmap 135*/ 136private Bitmap drawable2Bitmap(Drawable drawable) { 137if (drawable == null) { 138return null; 139} 140Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); 141Canvas canvas = new Canvas(bitmap); 142//根据传递的scaletype获取matrix对象,设置给bitmap 143Matrix matrix = getImageMatrix(); 144if (matrix != null) { 145canvas.concat(matrix); 146} 147drawable.draw(canvas); 148return bitmap; 149} 150 151private int dp2px(float value) { 152return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics()); 153} 154 }

View Code这是一个自定义的ImageView
 
然后就是页面布居中的引用了
< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@color/threefragment_color" android:id="@+id/person"> < com.example.logintest.circleImage.RoundImageView android:id="@+id/img_tx" android:layout_width="100dp" android:layout_height="100dp" app:type="circle" android:src="https://www.songbingjia.com/android/@drawable/touxiang" android:layout_marginTop="30dp" android:layout_centerHorizontal="true" /> < /RelativeLayout>

 
【Android实现头像圆角】参考博客链接 

    推荐阅读