2013-06-20 30 views
6

Tôi đang sử dụng lớp rất phổ biến này để làm tròn góc:làm tròn góc Chỉ ONE Image - không cả bốn

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap 
       .getHeight(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final int color = 0xff424242; 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     final RectF rectF = new RectF(rect); 
     final float roundPx = pixels; 

     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(color); 
     canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 

     return output; 
    } 

Tôi muốn sửa đổi nó để chỉ góc trên bên trái được làm tròn. Tôi không thể tìm thấy tham số trong mã thực hiện điều này? Ai đó có thể hỗ trợ?

+0

Điều này có thể hữu ích - http://developer.android.com/reference/android/graphics/drawable/shapes/RoundRectShape.html – Varun

+0

@Varun Tôi cần thêm một chút thông tin sau đó? Các tài liệu của dev không phải luôn luôn thân thiện khi bạn muốn một cái gì đó cụ thể này. – KickingLettuce

Trả lời

5

Đây có lẽ không phải là cách hiệu quả nhất để thực hiện việc này, nhưng bạn có thể điền vào các góc được làm tròn bằng cách vẽ lên trên mặt nạ hiện tại của bạn. Bạn có thể bắt đầu với mã hiện tại sau đó sử dụng canvas.drawRect (ngay sau khi gọi tới canvas.drawRoundRect) trên các khu vực thích hợp (các góc). Tôi tưởng tượng một cái gì đó như thế này sẽ chỉ tròn góc trên cùng bên trái:

public static Bitmap getRoundedTopLeftCornerBitmap(Bitmap bitmap, int pixels) { 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap 
      .getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int color = 0xff424242; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    final RectF rectF = new RectF(rect); 
    final float roundPx = pixels; 
    final Rect topRightRect = new Rect(bitmap.getWidth()/2, 0, bitmap.getWidth(), bitmap.getHeight()/2); 
    final Rect bottomRect = new Rect(0, bitmap.getHeight()/2, bitmap.getWidth(), bitmap.getHeight()); 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
    // Fill in upper right corner 
    canvas.drawRect(topRightRect, paint); 
    // Fill in bottom corners 
    canvas.drawRect(bottomRect, paint); 

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 

    return output; 
} 

Có một chút tối ưu bạn có thể làm ở đây nếu bạn cảm thấy thích, nhưng tôi nghĩ nó sẽ hoạt động. Ý tưởng chung chắc chắn nên. Tôi đã không thử hoặc thử nghiệm mã này mặc dù và nó sẽ không nhìn đúng nếu pixels > bitmap.getWidth()/2 hoặc pixels > bitmap.getHeight()/2. Sau đó, một lần nữa, đó có thể là sự thật trước khi quá.

+0

Điều này đã làm điều đó, cảm ơn! Bạn có nghĩ rằng điều này sẽ có hiệu lực hiệu suất trên một hình ảnh trong mỗi 'listView'? Đánh dấu bạn cũng chính xác. – KickingLettuce

+1

Tôi sẽ không tưởng tượng điều này có vấn đề hiệu suất nghiêm trọng, mặc dù nếu bạn có nghĩa là nó trong mỗi mục 'ListView' sau đó nó có thể thêm lên chỉ đủ để làm cho mọi thứ không hoàn toàn trơn tru khi di chuyển. Nếu điều này xảy ra, bạn sẽ phải thực hiện các thủ thuật thông thường với tải không đồng bộ. Chỉ cần thử xem và cảm nhận nó như thế nào trước khi bạn cố gắng hết sức để tối ưu hóa. – kabuko

+0

Tôi làm cách nào để chụp một hình vuông, làm tròn nó, sau đó giải nén phần trên bên phải 1/4 của hình ảnh? – MCR

1

Đây là câu trả lời mang tính khái niệm hơn, nhưng bạn có thể vẽ hình chữ nhật được làm tròn sau đó lớp hai hình chữ nhật bình thường trên đầu góc mà bạn muốn làm tròn?

Visual example

+0

Đây là bản chất những gì tôi đã làm trong câu trả lời ở trên của tôi mặc dù với kích thước hơi khác nhau cho các hình dạng. – kabuko

4
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int topLeftX, int topLeftY, int topRightX, int topRightY, int bottomRightX, int bottomRightY, int bottomLeftX, int bottomLeftY) { 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 
     // the float array passed to this function defines the x/y values of the corners 
     // it starts top-left and works clockwise 
     // so top-left-x, top-left-y, top-right-x etc 
     RoundRectShape rrs = new RoundRectShape(new float[]{topLeftX, topLeftY, topRightX, topRightY, bottomRightX, bottomRightY, bottomLeftX, bottomLeftY}, null, null); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setAntiAlias(true); 
     paint.setColor(0xFF000000); 
     rrs.resize(bitmap.getWidth(), bitmap.getHeight()); 
     rrs.draw(canvas, paint); 
     paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 
     return output; 
    } 

Hoặc bạn có thể xem mã nguồn RoundRects.java - ví dụ cho thấy làm thế nào để tạo ra các góc tròn, có sẵn trong các mẫu SDK: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.3_r2.1/com/example/android/apis/graphics/RoundRects.java/

+0

Tôi không hiểu mã của bạn. Nếu tôi muốn làm tròn một hình ảnh, sau đó cắt xuống phần trên cùng bên phải 1/4, tôi có thể làm điều đó với mã này không? – MCR

2

này là dành cho chọn góc:

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap, float roundDip, boolean roundTL, boolean roundTR, boolean roundBL, boolean roundBR) 
{ 
    try 
    { 

     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final int color = 0xff424242; 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     final RectF rectF = new RectF(rect); 
     final float roundPx = convertDipToPixel(roundDip, context); 

     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(color); 
     canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// draw round 
                   // 4Corner 

     if (!roundTL) 
     { 
      Rect rectTL = new Rect(0, 0, bitmap.getWidth()/2, bitmap.getHeight()/2); 
      canvas.drawRect(rectTL, paint); 
     } 
     if (!roundTR) 
     { 
      Rect rectTR = new Rect(bitmap.getWidth()/2, 0, bitmap.getWidth(), bitmap.getHeight()/2); 
      canvas.drawRect(rectTR, paint); 
     } 
     if (!roundBR) 
     { 
      Rect rectBR = new Rect(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth(), bitmap.getHeight()); 
      canvas.drawRect(rectBR, paint); 
     } 
     if (!roundBL) 
     { 
      Rect rectBL = new Rect(0, bitmap.getHeight()/2, bitmap.getWidth()/2, bitmap.getHeight()); 
      canvas.drawRect(rectBL, paint); 
     } 

     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 

     return output; 
    } catch (Exception e) 
    { 
    } 
    return bitmap; 
} 
+0

không hoạt động đối với tôi – JMRboosties

0

Trong trường hợp bạn cần vẽ (trên canvas) một vòng tròn với bán kính khác nhau cho các góc khác nhau, bạn có thể sử dụng điều này:

private void drawAsymmetricRoundRect(Canvas canvas, RectF rectF, float[] radii, Paint paint) { 
    float topLeftX = rectF.left + radii[0]; 
    float topLeftY = rectF.top + radii[0]; 
    float topRightX = rectF.right - radii[1]; 
    float topRightY = rectF.top + radii[1]; 
    float bottomRightX = rectF.right - radii[2]; 
    float bottomRightY = rectF.bottom - radii[2]; 
    float bottomLeftY = rectF.bottom - radii[3]; 
    float bottomLeftX = rectF.left + radii[3]; 
    RectF topLeftCorner = new RectF(rectF.left, rectF.top, topLeftX + radii[0], topLeftY + radii[0]); 
    RectF topRightCorner = new RectF(topRightX - radii[1], rectF.top, rectF.right, topRightY + radii[1]); 
    RectF bottomRightCorner = new RectF(bottomRightX - radii[2], bottomRightY - radii[2], rectF.right, rectF.bottom); 
    RectF bottomLeftCorner = new RectF(rectF.left, bottomLeftY - radii[3], bottomLeftX + radii[3], rectF.bottom); 

    canvas.drawArc(topLeftCorner, 180, 90, true, paint); 
    canvas.drawArc(topRightCorner, 270, 90, true, paint); 
    canvas.drawArc(bottomRightCorner, 0, 90, true, paint); 
    canvas.drawArc(bottomLeftCorner, 90, 90, true, paint); 
    canvas.drawRect(topLeftX, rectF.top, topRightX, bottomLeftY < bottomRightY ? bottomLeftY : bottomRightY, paint); //top rect 
    canvas.drawRect(topLeftX > bottomLeftX ? topLeftX : bottomLeftX, topRightY, rectF.right, bottomRightY, paint); //right rect 
    canvas.drawRect(bottomLeftX, topLeftY > topRightY ? topLeftY : topRightY, bottomRightX, rectF.bottom, paint); //bottom rect 
    canvas.drawRect(rectF.left, topLeftY, bottomRightX < topRightX ? bottomRightX : topRightX, bottomLeftY, paint); //left rect 
} 

float[] radii là một mảng float (chiều dài = 4), mà các cửa hàng kích thước của bán kính góc của bạn (chiều kim đồng hồ, bắt đầu từ góc trên bên trái =>{topLeft, topRight, bottomRight, bottomLeft}).

Về cơ bản, cách tiếp cận này vẽ 4 vòng cung (góc) và lấp đầy mọi thứ ở giữa các góc đó với 4 thước.

LƯU Ý QUAN TRỌNG: Tôi đã đặt khởi tạo các góc RectFs trong phương pháp này để giảm độ phức tạp của mã được đăng. Do thực tế bạn có thể gọi phương thức này từ phương thức onDraw() của mình, bạn nên trích xuất phần này của mã và đặt nó ở nơi bạn bắt đầu khác Rects (miễn là bạn cũng không khởi tạo chúng trong onDraw(): P) .