Android实现圆形头像

来源:互联网 发布:网络机漏洞检测软件 编辑:程序博客网 时间:2024/05/19 13:22

想要写成和博客上大神们的例子是不可能的,我只能去了解下用法和原理。

我知道最基础的原理是 在图片上先用canvas画出一个圆,然后在根据圆的坐标和半径再画出一个半径相同圆形的bitmap,就是和之前的canvas画的圆重叠了,圆在下面,Bitmap在上面。然后我们要获取两者相交的部分,去掉他们外面的部分。图形参考的可以去看看这篇文章:http://blog.csdn.net/zhangjm_123/article/details/42025991

先上效果图:


这个是有圆环的圆形头像

当然圆形头像是要继承ImageView的,也就是自定义view

1.在values文件夹里新建attrs.xml文件

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="RoundImageView">        <attr name="border_width" format="dimension" />        <attr name="border_color" format="color" />    </declare-styleable></resources>

两个参数是圆环的宽度和颜色

然后在activity_main.xml里面加入控件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res/com.sdf.roundimage"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#fffffdfa"    android:gravity="center"    android:orientation="vertical" >    <com.sdf.roundimage.RoundImageView        android:id="@+id/roundImage"        android:layout_width="160dp"        android:layout_height="160dp"        android:src="@drawable/girl"        app:border_color="#CC0000"        app:border_width="2dp" /></LinearLayout>

这里注意一定要加上 xmlns:app="http://schemas.android.com/apk/res/com.sdf.roundimage"这句话,这是引用自定义控件必须加的 

接下来就是圆形头像的代码,是继承ImageView的代码:

public class RoundImageView extends ImageView {private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;private static final int COLORDRAWABLE_DIMENSION = 1;private static final int DEFAULT_BORDER_WIDTH = 0;private static final int DEFAULT_BORDER_COLOR = Color.BLACK;private final RectF mDrawableRect = new RectF();private final RectF mBorderRect = new RectF();private final Matrix mShaderMatrix = new Matrix();private final Paint mBitmapPaint = new Paint();private final Paint mBorderPaint = new Paint();private int mBorderColor = DEFAULT_BORDER_COLOR;private int mBorderWidth = DEFAULT_BORDER_WIDTH;private Bitmap mBitmap;private BitmapShader mBitmapShader;private int mBitmapWidth;private int mBitmapHeight;private float mDrawableRadius;private float mBorderRadius;private boolean mReady;private boolean mSetupPending;public RoundImageView(Context context) {super(context);}public RoundImageView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RoundImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);super.setScaleType(SCALE_TYPE);TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.RoundImageView, defStyle, 0);mBorderWidth = a.getDimensionPixelSize(R.styleable.RoundImageView_border_width, DEFAULT_BORDER_WIDTH);mBorderColor = a.getColor(R.styleable.RoundImageView_border_color,DEFAULT_BORDER_COLOR);a.recycle();mReady = true;if (mSetupPending) {setup();mSetupPending = false;}}@Overridepublic ScaleType getScaleType() {return SCALE_TYPE;}@Overridepublic void setScaleType(ScaleType scaleType) {if (scaleType != SCALE_TYPE) {throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));}}@Overrideprotected void onDraw(Canvas canvas) {if (getDrawable() == null) {return;}canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius,mBitmapPaint);canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius,mBorderPaint);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);setup();}public int getBorderColor() {return mBorderColor;}public void setBorderColor(int borderColor) {if (borderColor == mBorderColor) {return;}mBorderColor = borderColor;mBorderPaint.setColor(mBorderColor);invalidate();}public int getBorderWidth() {return mBorderWidth;}public void setBorderWidth(int borderWidth) {if (borderWidth == mBorderWidth) {return;}mBorderWidth = borderWidth;setup();}@Overridepublic void setImageBitmap(Bitmap bm) {super.setImageBitmap(bm);mBitmap = bm;setup();}@Overridepublic void setImageDrawable(Drawable drawable) {super.setImageDrawable(drawable);mBitmap = getBitmapFromDrawable(drawable);setup();}@Overridepublic void setImageResource(int resId) {super.setImageResource(resId);mBitmap = getBitmapFromDrawable(getDrawable());setup();}private Bitmap getBitmapFromDrawable(Drawable drawable) {if (drawable == null) {return null;}if (drawable instanceof BitmapDrawable) {return ((BitmapDrawable) drawable).getBitmap();}try {Bitmap bitmap;if (drawable instanceof ColorDrawable) {bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION,COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);} else {bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(), BITMAP_CONFIG);}Canvas canvas = new Canvas(bitmap);drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());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);mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width()- mBorderWidth, mBorderRect.height() - mBorderWidth);mDrawableRadius = Math.min(mDrawableRect.height() / 2,mDrawableRect.width() / 2);updateShaderMatrix();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;dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;} else {scale = mDrawableRect.width() / (float) mBitmapWidth;dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;}mShaderMatrix.setScale(scale, scale);mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth,(int) (dy + 0.5f) + mBorderWidth);mBitmapShader.setLocalMatrix(mShaderMatrix);}}

这个是拿来别人的代码的==,待会在贴一段自己理解后写的简化的代码。

MainActivity.class

public class MainActivity extends Activity {ImageView imageView = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView = (ImageView) findViewById(R.id.roundImage);}

以上这些就是一个完整的显示圆形头像的过程



后来我自己写了一个很简单的代码,也是实现圆形头像的,但是无圆环

直接就是一个ImageLoader.class

public class ImageLoader {public static Bitmap getRoundBitmap(Bitmap bitmap) {// 获取传入进来的Bitmap宽度和高度int width = bitmap.getWidth();int height = bitmap.getHeight();int r = 0;// 园的半径if (width < height) {r = width;} else {r = height;}// 新建一个bitmap,相当于传入进来的bitmap的copyBitmap backgroundBitmap = Bitmap.createBitmap(width, height,Config.ARGB_8888);// 建立画布Canvas canvas = new Canvas(backgroundBitmap);// 建立画笔Paint paint = new Paint();// 无锯齿paint.setAntiAlias(true);// 矩形RectF rect = new RectF(0, 0, r, r);// 画圆角矩形,当它的宽和高一样时,就是一个圆了canvas.drawRoundRect(rect, r / 2, r / 2, paint);// 取画布和bitmap相交的部分,即展示的圆paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 画出圆形头像canvas.drawBitmap(bitmap, null, rect, paint);return backgroundBitmap;}}

这个例子不需要再自定义View,直接在xml里写ImageView就行了

activity_main.xml

 <ImageView        android:id="@+id/roundimage"        android:layout_width="400dp"        android:layout_height="400dp"        />


MainActivity.class

public class MainActivity extends Activity {private ImageView image;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);image = (ImageView) findViewById(R.id.roundimage);Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.girl);Bitmap bm = ImageLoader.getRoundBitmap(bitmap);image.setImageBitmap(bm);}

这个例子也上个效果图:


圆形头像的学习算是暂时结束了,其实学的一点也不深,只是能够借用别人的源码来使用,这个其实一点也不算进步,以后学的更深以后,再去深研究里面的原理以及怎么写代码。


0 0
原创粉丝点击