android 自定义圆角imageview

现在很多头像显示都是用圆形,但是我们原生态的ImageView并不能满足我们的要求,所以就要自定义一个圆形的,做法很多种,

提供下我实现的思路,

1:画一个空的画布

2:在空的画布上画一个圆和一个一张图片(这图片是正方形的)

3:取圆形和正方形的交集

4:把在空位图上绘制的内容图片 绘制到imageview上

 代码如下:

package com.example.customimageview.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CustomImageView extends ImageView {

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

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

	public CustomImageView(Context context) {
		super(context);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		Drawable drawable = getDrawable();
		if(drawable==null){
			return;
		}
		//把drawable转换成Bitmap对象,用于显示图片
		Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
		if(bitmap!=null){
			//创建一个空的位图,这里空是指位图上的内容为空,不是指对象本身
			Bitmap canvasBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
			//创建一个空的画布(也就是说画布上什么都没有)
			Canvas myCanvas = new Canvas(canvasBitmap);
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setFilterBitmap(true);
			paint.setColor(Color.GREEN);
			//在空的画布上画一个圆
			myCanvas.drawCircle(bitmap.getWidth()/2+0.1f, bitmap.getHeight()/2+0.1f, bitmap.getHeight()/2+0.1f, paint);
			//图2个图片的交集
			paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
			//在画布上画一个图片
			myCanvas.drawBitmap(bitmap, 0, 0, paint);
			//最后把在canvasBitmap上绘制的内容绘制到自定义的控件上通过系统的canvas
			canvas.drawBitmap(canvasBitmap, 0, 0,null);
		}
	}
}

使用:

package com.example.customimageview;
import android.app.Activity;
import android.os.Bundle;

import com.example.customimageview.ui.CustomImageView;
public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		CustomImageView imageview = (CustomImageView) findViewById(R.id.imageview);
		imageview.setImageResource(R.drawable.ic_launcher);
	}
}

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
    <com.example.customimageview.ui.CustomImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />
</RelativeLayout>


在这里就是这行paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));设置2张图的模式,大概有如下这么多种

16条Porter-Duff规则 
 * 1.PorterDuff.Mode.CLEAR 
 * 2.PorterDuff.Mode.SRC 
 * 3.PorterDuff.Mode.DST 
 * 4.PorterDuff.Mode.SRC_OVER 
 * 5.PorterDuff.Mode.DST_OVER 
 * 6.PorterDuff.Mode.SRC_IN 
 * 7.PorterDuff.Mode.DST_IN 
 * 8.PorterDuff.Mode.SRC_OUT 
 * 9.PorterDuff.Mode.DST_OUT 
 * 10.PorterDuff.Mode.SRC_ATOP 
 * 11.PorterDuff.Mode.DST_ATOP 
 * 12.PorterDuff.Mode.XOR 
 * 13.PorterDuff.Mode.DARKEN 
 * 14.PorterDuff.Mode.LIGHTEN 
 * 15.PorterDuff.Mode.MULTIPLY 
 * 16.PorterDuff.Mode.SCREEN  
大家可以去看看  在这不一一演示



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值