2 baidu 19694879 baidu_19694879 于 2016.03.16 18:48 提问

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;

}

7个回答

baidu_19694879
baidu_19694879   2016.03.22 15:06
已采纳

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;
dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.03.17 08:32

首先要让圆形的直径等于正方形的边长

lxk_1993
lxk_1993   Rxr 2016.03.17 08:37

可以看看你的效果图 和 设计图不

baidu_19694879
baidu_19694879   2016.03.17 09:25
baidu_19694879
baidu_19694879   2016.03.17 09:26

图片说明这是效果图

Connery_thunder
Connery_thunder   2016.03.17 14:06

图片说明

你可以去看看 CircleImageView 开源的 那个写的很完善,包括相片的选取

m_zero_ichelle
m_zero_ichelle   2016.03.18 11:12

看能不能给存放图片的地方的布局块设置一下参数为内容自适应。
//1设置参数
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

//2给图片设置参数
imageview.setLayoutParams(params);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!