程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> android-Android 方形圖片編程圓形之後,太小了

android-Android 方形圖片編程圓形之後,太小了

編輯:編程解疑
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;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved