baidu_19694879 2016-03-16 10:48 采纳率: 50%
浏览 1789
已采纳

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 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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波