功能实现:点击圆形头像之后可以实现相册上传或者开启相机,然后把得到的图片经过剪裁,把剪裁过的图片设置为头像的背景图
步骤:第一步:自定义一个类,继承ImageView,重写draw方法,实现外观为圆形
第二步:在xml文件中引用该控件
第三步:实现圆形头像的点击事件,点击后显示对话框界面,询问你是打开相册还是相机(自动省略显示对话框的代码)
第四步:根据用户选择情况,打开相册或者相机
第五步:将拍摄的图片或者相册选中的图片进行剪裁,将结果保存在指定内存区域
第六步:更新头像图片
具体实现:第一步:自定义一个类,继承ImageView,重写draw方法,实现外观为圆形
//圆形头像类
public class MyRoundPhoto extends ImageView{
private Paint p;
private Bitmap bitmap;
private Context context;
private int wAndHeight[]=new int[2];
private File file;
public MyRoundPhoto(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
//获得控件长宽(px)
wAndHeight = getWidthAndHeight(context,attrs);
this.context = context;
//初始化控件
init();
}
public MyRoundPhoto(Context context) {
super(context);
// TODO Auto-generated constructor stub
//获得控件长宽(px)
wAndHeight=getWidthAndHeight(context,attrs);
this.context = context;
init();
}
public MyRoundPhoto(Context context, AttributeSet attrs) {
super(context, attrs);
//获得控件长宽(px)
wAndHeight=getWidthAndHeight(context,attrs);
// TODO Auto-generated constructor stub
this.context = context;
init();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(bitmap, new Matrix(), p);
}
private void