baidu_19694879
baidu_19694879
采纳率50%
2016-03-16 10:48 阅读 1.8k

Android 方形图片编程圆形之后,太小了

10

从本地相册获取的一个图片,进行正方形裁剪后,在用遮罩方式变成圆形,怎么样调整生成圆形之后的圆形图片的大小。

下面是主要代码

/**
* 裁剪图片方法实现
*
* @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 07: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 东风玖哥 2016-03-17 00:32

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

    点赞 评论 复制链接分享
  • lxk_1993 103style 2016-03-17 00:37

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

    点赞 评论 复制链接分享
  • baidu_19694879 baidu_19694879 2016-03-17 01:25
    点赞 评论 复制链接分享
  • baidu_19694879 baidu_19694879 2016-03-17 01:26

    图片说明这是效果图

    点赞 评论 复制链接分享
  • Connery_thunder 浅浅无奈 2016-03-17 06:06

    图片说明

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

    点赞 评论 复制链接分享
  • m_zero_ichelle 空中有朵雨做的云1789 2016-03-18 03:12

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

    //2给图片设置参数
    imageview.setLayoutParams(params);
    
    点赞 评论 复制链接分享

相关推荐