在上一篇文章http://blog.csdn.net/yanglei0917/article/details/52180544里,写了一下关于上传头像的问题,但是上传头像默认的都比较丑,现在圆形头像很受欢迎,所以这里学习一下圆形头像的制作,其实圆形头像制作很简单;
我们只需要在图片转成Bitmap的时候做一下相应的处理即可:
/**
* 展示头像
* @param data
*/
private void showResizeImage(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
// Drawable drawable = new BitmapDrawable(photo);
// iv.setImageDrawable(drawable);
//这里我们发现和上篇文章相比,这里做了相应的改动,这就是把默认的矩形图片转化为圆形的方法
photo=toRoundBitmap(photo);
iv.setImageBitmap(photo);
}
}
在toRoundBitmap的方法里我们就是做了一下简单的绘制,把矩形绘制成了圆形这么简单而已
/**
* 把bitmap转成圆形
* */
public Bitmap toRoundBitmap(Bitmap bitmap){
int width=bitmap.getWidth();
int height=bitmap.getHeight();
int r=0;
//取最短边做边长
if(width<height){
r=width;
}else{
r=height;
}
//构建一个bitmap
Bitmap backgroundBm=Bitmap.createBitmap(width,height,Config.ARGB_8888);
//new一个Canvas,在backgroundBmp上画图
Canvas canvas=new Canvas(backgroundBm);
Paint p=new Paint();
//设置边缘光滑,去掉锯齿
p.setAntiAlias(true);
RectF rect=new RectF(0, 0, r, r);
//通过制定的rect画一个圆角矩形,当圆角X轴方向的半径等于Y轴方向的半径时,
//且都等于r/2时,画出来的圆角矩形就是圆形
canvas.drawRoundRect(rect, r/2, r/2, p);
//设置当两个图形相交时的模式,SRC_IN为取SRC图形相交的部分,多余的将被去掉
p.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//canvas将bitmap画在backgroundBmp上
canvas.drawBitmap(bitmap, null, rect, p);
return backgroundBm;
}
//完整代码:
public class MainActivity extends Activity implements OnClickListener {
private static final int IMAGE_REQUEST_CODE = 0;
private static final int CAMERA_REQUEST_CODE = 1;
private static final int RESIZE_REQUEST_CODE = 2;
private static final String IMAGE_FILE_NAME = "header.jpg";
private CircleView iv;
private Button bt_select;
private Button bt_cam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
iv = (CircleView) findViewById(R.id.iv);
bt_cam = (Button) findViewById(R.id.bt_cam);
bt_select = (Button) findViewById(R.id.bt_select);
bt_select.setOnClickListener(this);
bt_cam.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 从图库中选择
case R.id.bt_select:
Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
intent1.addCategory(Intent.CATEGORY_OPENABLE);
intent1.setType("image/*");
startActivityForResult(intent1, IMAGE_REQUEST_CODE);
break;
// 从相机拍照
case R.id.bt_cam:
if (isSdcardExisting()) {
Intent cameraIntent = new Intent(
"android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
} else {
Toast.makeText(v.getContext(), "请插入sd卡", Toast.LENGTH_LONG)
.show();
}
break;
default:
break;
}
}
private Uri getImageUri() {
return Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
IMAGE_FILE_NAME));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK){
switch (requestCode) {
case IMAGE_REQUEST_CODE:
resizeImage(data.getData());
break;
case CAMERA_REQUEST_CODE:
if(isSdcardExisting()){
resizeImage(getImageUri());
}else{
Toast.makeText(MainActivity.this, "未找到存储卡,无法存储照片!",
Toast.LENGTH_LONG).show();
}
break;
case RESIZE_REQUEST_CODE:
if (data != null) {
showResizeImage(data);
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 展示头像
* @param data
*/
private void showResizeImage(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
// Drawable drawable = new BitmapDrawable(photo);
// iv.setImageDrawable(drawable);
photo=toRoundBitmap(photo);
iv.setImageBitmap(photo);
}
}
/**
* 把bitmap转成圆形
* */
public Bitmap toRoundBitmap(Bitmap bitmap){
int width=bitmap.getWidth();
int height=bitmap.getHeight();
int r=0;
//取最短边做边长
if(width<height){
r=width;
}else{
r=height;
}
//构建一个bitmap
Bitmap backgroundBm=Bitmap.createBitmap(width,height,Config.ARGB_8888);
//new一个Canvas,在backgroundBmp上画图
Canvas canvas=new Canvas(backgroundBm);
Paint p=new Paint();
//设置边缘光滑,去掉锯齿
p.setAntiAlias(true);
RectF rect=new RectF(0, 0, r, r);
//通过制定的rect画一个圆角矩形,当圆角X轴方向的半径等于Y轴方向的半径时,
//且都等于r/2时,画出来的圆角矩形就是圆形
canvas.drawRoundRect(rect, r/2, r/2, p);
//设置当两个图形相交时的模式,SRC_IN为取SRC图形相交的部分,多余的将被去掉
p.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//canvas将bitmap画在backgroundBmp上
canvas.drawBitmap(bitmap, null, rect, p);
return backgroundBm;
}
/**
* 对图片进行处理
* @param uri
*/
private void resizeImage(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, RESIZE_REQUEST_CODE);
}
/**
* 判断sd卡是否存在
*
* @return
*/
private boolean isSdcardExisting() {
final String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
}