Android学习 - 圆形ImageView

先看下效果图:

public class RoundedImageView extends ImageView {

	private int borderThickness;
	private int borderColor;
	private Bitmap image;
	private Context context;
	private int width = 0;
	private int height = 0;

	public RoundedImageView(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public RoundedImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		TypedArray typedArray = context.obtainStyledAttributes(attrs,
				R.styleable.roundedimageview);
		borderThickness = typedArray.getDimensionPixelSize(
				R.styleable.roundedimageview_border_thickness, 10);
		borderColor = typedArray.getColor(
				R.styleable.roundedimageview_border_color, 0xFFFFFFFF);
		typedArray.recycle();
		init();
	}

	public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		init();
	}

	public void init() {
		image = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.main_portrait);
	}

	public void setImage(String path) {
		if (path == null || "".equals(path)) {
			return;
		}
		File file = new File(path);
		if ((!file.exists()) || (!file.isFile())) {
			return;
		}
		try {
			this.image = BitmapFactory.decodeStream(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			this.image = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.ic_launcher);
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (width == 0) {
			width = getWidth();
		}
		if (height == 0) {
			height = getHeight();
		}
		if (width == 0 || height == 0) {
			return;
		}
		if (image == null) {
			return;
		}
		this.measure(0, 0);
		int radius = (width < height ? width : height) / 2 - borderThickness;
		Bitmap bitmap = image.copy(Bitmap.Config.ARGB_8888, true);
		drawCircleBorder(canvas, radius + borderThickness / 2);
		Bitmap roundBitmap = getCroppedRoundBitmap(bitmap, radius);
		canvas.drawBitmap(roundBitmap, width / 2 - radius, height / 2 - radius,
				null);
	}

	public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
		Bitmap scaledSrcBmp;
		int diameter = radius * 2;

		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		int squareWidth = 0, squareHeight = 0;
		int x = 0, y = 0;
		Bitmap squareBitmap;
		if (bmpHeight > bmpWidth) {
			squareWidth = squareHeight = bmpWidth;
			x = 0;
			y = (bmpHeight - bmpWidth) / 2;
			squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,
					squareHeight);
		} else if (bmpHeight < bmpWidth) {
			squareWidth = squareHeight = bmpHeight;
			x = (bmpWidth - bmpHeight) / 2;
			y = 0;
			squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,
					squareHeight);
		} else {
			squareBitmap = bmp;
		}

		if (squareBitmap.getWidth() != diameter
				|| squareBitmap.getHeight() != diameter) {
			scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter,
					diameter, true);

		} else {
			scaledSrcBmp = squareBitmap;
		}
		Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(),
				scaledSrcBmp.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		Paint paint = new Paint();
		Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(),
				scaledSrcBmp.getHeight());

		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setDither(true);
		canvas.drawARGB(0, 0, 0, 0);
		canvas.drawCircle(scaledSrcBmp.getWidth() / 2,
				scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2,
				paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
		bmp.recycle();
		squareBitmap.recycle();
		scaledSrcBmp.recycle();
		bmp = null;
		squareBitmap = null;
		scaledSrcBmp = null;
		return output;
	}

	private void drawCircleBorder(Canvas canvas, int radius) {
		Paint myPaint = new Paint();
		myPaint.setAntiAlias(true);
		myPaint.setFilterBitmap(true);
		myPaint.setDither(true);
		myPaint.setColor(borderColor);
		myPaint.setStyle(Paint.Style.STROKE);
		myPaint.setStrokeWidth(borderThickness);
		canvas.drawCircle(width / 2, height / 2, radius, myPaint);
	}
}

attrs.xml

<!-- 主界面圆形头像roundedimageview -->
<declare-styleable name="roundedimageview">
    <attr name="border_thickness" format="dimension" />
    <attr name="border_color" format="color" />
</declare-styleable>

使用方式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:imagecontrol="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ehr_backgroud"
    android:orientation="vertical" >
    <com.android.wolflz.RoundedImageView
        android:id="@+id/index_photo_imageview"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_centerInParent="true"
        android:layout_marginTop="50dp"
        android:contentDescription="@string/contentDescription"
        imagecontrol:border_color="#ffffff"
        imagecontrol:border_thickness="4dp" />
</LinearLayout>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值