自定义圆角图片、圆形图片(适用于显示用户图标)

自定义圆角图形


1、自定义view 类

package com.tan.roundangleimageview.view;

import com.tan.roundangleimageview.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundAngleImageView extends ImageView {

	private Paint paint;
	private int roundWidth = 5;
	private int roundHeight = 5;
	private Paint paint2;

	public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs);
	}

	public RoundAngleImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public RoundAngleImageView(Context context) {
		super(context);
		init(context, null);
	}
	
	public RoundAngleImageView(Context context,int roundWidth,int roundHeight ) {
		super(context);
		
		this.roundHeight=roundHeight;
		this.roundWidth=roundWidth;
		init(context, null);
	}
	
	private void init(Context context, AttributeSet attrs) {

		//从attrs 获取自定义属性、注意xml中引用自定义属性时,要增加
		//xmlns:app1="http://schemas.android.com/apk/res/com.tan.roundangleimageview"
		//app1 为名称, com.tan.roundangleimageview 为包名,从manifest文件中获取
		if (attrs != null) {
			TypedArray a = context.obtainStyledAttributes(attrs,
					R.styleable.RoundAngleImageView);
			roundWidth = a.getDimensionPixelSize(
					R.styleable.RoundAngleImageView_roundWidth, roundWidth);
			roundHeight = a.getDimensionPixelSize(
					R.styleable.RoundAngleImageView_roundHeight, roundHeight);
			// 注意回收
			a.recycle();
		} else {
			//如果没有获取到自定义属性值。就获取屏幕像素密度值,然后乘以一个默认的值作为默认属性值。
			float density = context.getResources().getDisplayMetrics().density;
			roundWidth = (int) (roundWidth * density);
			roundHeight = (int) (roundHeight * density);
		}
		// paint 设置模式PorterDuffXfermode DST_OUT 
		paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setAntiAlias(true);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

		paint2 = new Paint();
		paint2.setXfermode(null);
	}
	// activity中调用设置宽度
	public void setRoundWidth(int roundWidth) {
		this.roundWidth = roundWidth;
	}

	public void setRoundHeight(int roundHeight) {
		this.roundHeight = roundHeight;
	}

	@Override
	public void draw(Canvas canvas) {
		Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
				Config.ARGB_8888);
		Canvas canvas2 = new Canvas(bitmap);
		super.draw(canvas2);
		drawLeftUp(canvas2);
		drawRightUp(canvas2);
		drawLeftDown(canvas2);
		drawRightDown(canvas2);
		canvas.drawBitmap(bitmap, 0, 0, paint2);
		if(!bitmap.isRecycled()){
			//bitmap.recycle();
		}
	}

	// 绘制左上角 外圆弧形
	private void drawLeftUp(Canvas canvas) {
		Path path = new Path();
		path.moveTo(0, roundHeight);
		path.lineTo(0, 0);
		path.lineTo(roundWidth, 0);
		path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90);
		path.close();
		canvas.drawPath(path, paint);
	}

	private void drawLeftDown(Canvas canvas) {
		Path path = new Path();
		path.moveTo(0, getHeight() - roundHeight);
		path.lineTo(0, getHeight());
		path.lineTo(roundWidth, getHeight());
		path.arcTo(new RectF(0, getHeight() - roundHeight * 2,
				0 + roundWidth * 2, getWidth()), 90, 90);
		path.close();
		canvas.drawPath(path, paint);
	}

	private void drawRightDown(Canvas canvas) {
		Path path = new Path();
		path.moveTo(getWidth() - roundWidth, getHeight());
		path.lineTo(getWidth(), getHeight());
		path.lineTo(getWidth(), getHeight() - roundHeight);
		path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight()
				- roundHeight * 2, getWidth(), getHeight()), 0, 90);
		path.close();
		canvas.drawPath(path, paint);
	}

	private void drawRightUp(Canvas canvas) {
		Path path = new Path();
		path.moveTo(getWidth(), roundHeight);
		path.lineTo(getWidth(), 0);
		path.lineTo(getWidth() - roundWidth, 0);
		path.arcTo(new RectF(getWidth() - roundWidth * 2, 0, getWidth(),
				0 + roundHeight * 2), -90, 90);
		path.close();
		canvas.drawPath(path, paint);
	}

}

a、如果项目利用 volley 加载图片。此View还可以继承 NetworkImageView

即:public class RoundAngleImageView extends NetworkImageView

这样利于加载图片。加载图片格式如下:

	Glide.with(mContext).load(HttpHelper.OSS_IMAGE_HOST+faceImg)// url地址
	.centerCrop()
	.placeholder(R.drawable.ic_me)
	.error(R.drawable.ic_me)
	.into(vRoundImg); //vRoundImg 自定义view


 
 

b、

// 绘制左上角 外圆弧形
	private void drawLeftUp(Canvas canvas) {
		Path path = new Path();
		path.moveTo(0, roundHeight);
		path.lineTo(0, 0);
		path.lineTo(roundWidth, 0);
		path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90);
		path.close();
		canvas.drawPath(path, paint);
	}
绘制左上角。草图如下

自定义圆角图片、圆形图片(适用于显示用户图标)_第1张图片

c 、关于Path 绘制图形的解析参考:

http://blog.csdn.net/legend12300/article/details/51122314


d 、关于 PorterDuffXfermode 的解析参考:

http://blog.csdn.net/legend12300/article/details/51122642

2、 value ——attrs.xml 

<resources>
    
    <declare-styleable name="RoundAngleImageView">
        <attr name="roundWidth" format="dimension" />
        <attr name="roundHeight" format="dimension" />
    </declare-styleable>
</resources>

自定义属性值,基本用法

TypedArray a = context.obtainStyledAttributes(attrs,
					R.styleable.RoundAngleImageView);
			roundWidth = a.getDimensionPixelSize(
					R.styleable.RoundAngleImageView_roundWidth, roundWidth);
			roundHeight = a.getDimensionPixelSize(
					R.styleable.RoundAngleImageView_roundHeight, roundHeight);
			// 注意回收
			a.recycle();



3、layout 布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app1="http://schemas.android.com/apk/res/com.tan.roundangleimageview"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#AACF52">

    <com.tan.roundangleimageview.view.RoundAngleImageView
        android:id="@+id/roundangle_imageview"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_android"
        app1:roundHeight="32dp"
        app1:roundWidth="32dp" 
        android:layout_centerInParent="true"/>

</RelativeLayout>

 

注意: 增加命名空间  

 xmlns:app1="http://schemas.android.com/apk/res/com.tan.roundangleimageview"
名称为 app1   res 后加包名 。 



源码下载地址

http://download.csdn.net/detail/legend12300/9487575



你可能感兴趣的:(图形)