问题描述
- Android 方形图片编程圆形之后,太小了
-
从本地相册获取的一个图片,进行正方形裁剪后,在用遮罩方式变成圆形,怎么样调整生成圆形之后的圆形图片的大小。下面是主要代码
/**
* 裁剪图片方法实现
*
* @param uri
/
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/");
// 设置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", layoutPhoto.getWidth());
intent.putExtra("outputY", layoutPhoto.getHeight());
intent.putExtra("return-data", true);
startActivityForResult(intent, RESULT_REQUEST_CODE);
}/** * 保存裁剪之后的图片数据 * * @param picdata */ private void getImageToView(Intent data) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); Bitmap bitPhoto = createCirclImage(photo); Drawable drawable = new BitmapDrawable(this.getResources(), bitPhoto); iv_nikePhoto.setImageDrawable(drawable); } } /** * 绘制圆形图片 * * @return */ private Bitmap createCirclImage(Bitmap bitmap) { // // 绘制画笔 int width = layoutPhoto.getWidth(); int height = layoutPhoto.getHeight(); // 绘制圆角矩形 Bitmap roundBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(roundBitmap); int color = 0xff424242; Paint paint = new Paint(); // 设置圆形半径 int radius; if (bitmap.getWidth() > bitmap.getHeight()) { radius = bitmap.getHeight() / 2; } else { radius = bitmap.getWidth() / 2; } // 绘制圆形 paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, radius, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, 0, 0, paint); return roundBitmap; }
解决方案
public static Bitmap cut2Circular(Bitmap source, boolean recycleSource) {
int photoWidth = layoutPhoto.getWidth() - DisplayUtil.dip2px(mActivity, 10);
int photoHeight = layoutPhoto.getHeight() - DisplayUtil.dip2px(mActivity, 10);
int width = source.getWidth();
int height = source.getHeight();
float scaleWidth = (float) photoWidth / (float) width;
float scaleHeight = (float) photoHeight / (float) height;
Matrix mx = new Matrix();
mx.postScale(scaleWidth, scaleHeight);
source = Bitmap.createBitmap(source, 0, 0, width, height, mx, true);
int diameter = Math.min(photoWidth, photoHeight);
Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap result = Bitmap.createBitmap(diameter, diameter, Bitmap.Config.ARGB_8888);
if (result != null) {
Canvas canvas = new Canvas(result);
// canvas.drawCircle(diameter / 2, diameter / 2, diameter / 2,
// paint);
canvas.drawCircle(diameter / 2, diameter / 2, diameter / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
// canvas.drawBitmap(source, (diameter - photoWidth) / 2, (diameter
// - photoHeight) / 2, paint);
canvas.drawBitmap(source, (diameter - photoWidth) / 2, (diameter - photoHeight) / 2, paint);
if (recycleSource) {
source.recycle();
source = null;
}
} else {
result = source;
}
return result;
解决方案二:
首先要让圆形的直径等于正方形的边长
解决方案三:
可以看看你的效果图 和 设计图不
设计图如下
图片说明](http://img.ask.csdn.net/upload/201603/17/1458177855_662617.png)
你可以去看看 CircleImageView 开源的 那个写的很完善,包括相片的选取
解决方案七:
看能不能给存放图片的地方的布局块设置一下参数为内容自适应。
//1设置参数
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//2给图片设置参数
imageview.setLayoutParams(params);