您的位置:首页 > 移动开发 > Android开发

android圆形图片,圆形头像实现方法(可直接运用到工程中)

2015-03-03 15:10 615 查看
  hello,大家好,如何将一个图片变成圆形图片相信大家或多或少都遇到过,本文通过自定义类继承ImageView重写了ondraw()方法来实现将一个图片转化为圆形图片,在使用时极其方便快捷,可直接复制到工程中使用,使用方法如下:

使用时只需将CircleImage.java复制到你的工程中,在需要使用圆形图片的地方调用该类即可。

效果图如下:



CircleImage.java:

package com.amos.testcircleimage.common;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/*
* 重写CircleImage,继承ImageView
*/
public class CircleImage extends ImageView {
public CircleImage(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

private int borderWidth = 4;
private int viewWidth;
private int viewHeight;
private Bitmap image;
private Paint paint;
private Paint paintBorder;
private BitmapShader shader;

public CircleImage(Context context, AttributeSet attrs) {
super(context, attrs);
setup();
}

public CircleImage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setup();
}

@SuppressLint("NewApi")
private void setup() {
// init paint
paint = new Paint();
paint.setAntiAlias(true);

paintBorder = new Paint();
setBorderColor(Color.parseColor("#E0EEEE"));
paintBorder.setAntiAlias(true);
this.setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.WHITE);
}

public void setBorderWidth(int borderWidth) {
this.borderWidth = borderWidth;
this.invalidate();
}

public void setBorderColor(int borderColor) {
if (paintBorder != null)
paintBorder.setColor(borderColor);

this.invalidate();
}

private void loadBitmap() {
BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();

if (bitmapDrawable != null)
image = bitmapDrawable.getBitmap();
}

@SuppressLint("DrawAllocation")
@Override
public void onDraw(Canvas canvas) {
// load the bitmap
loadBitmap();

// init shader
if (image != null) {
shader = new BitmapShader(Bitmap.createScaledBitmap(image,
canvas.getWidth(), canvas.getHeight(), false),
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
int circleCenter = viewWidth / 2;

// circleCenter is the x or y of the view's center
// radius is the radius in pixels of the cirle to be drawn
// paint contains the shader that will texture the shape
canvas.drawCircle(circleCenter + borderWidth, circleCenter
+ borderWidth, circleCenter + borderWidth - 4.0f,
paintBorder);
canvas.drawCircle(circleCenter + borderWidth, circleCenter
+ borderWidth, circleCenter - 4.0f, paint);
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec, widthMeasureSpec);

viewWidth = width - (borderWidth * 2);
viewHeight = height - (borderWidth * 2);

setMeasuredDimension(width, height);
}

private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text
result = viewWidth;
}

return result;
}

private int measureHeight(int measureSpecHeight, int measureSpecWidth) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpecHeight);
int specSize = MeasureSpec.getSize(measureSpecHeight);

if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = viewHeight;
}

return (result + 2);
}

}


activity_main.xml 

测试的xml文件只有一个圆形图片:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.amos.testcircleimage.common.CircleImage
android:id="@+id/main_img"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_launcher"/>

</LinearLayout>
特别注意的是此处的width和height不可使用wrap_content。喜欢能够帮到大家,谢谢!

demo下载地址:http://download.csdn.net/detail/a332802774/8469161
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐