开发者社区> 问答> 正文

有关于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;

}

展开
收起
爵霸 2016-03-10 11:00:16 2321 0
1 条回答
写回答
取消 提交回答
  • 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;
    
    
    2019-07-17 18:57:10
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载