2012-06-13 38 views
17

我用下面的方法制作了一個帶圓角的位圖。現在我想圍繞位圖畫一條線。在Android中使用圓角的位圖邊框

private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) { 

     Bitmap bitmap = scaledBitmap.getBitmap(); 

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

     color = 0xff424242; 
     paint = new Paint(); 
     rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     rectF = new RectF(rect); 
     roundPx = i; 
     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(Color.BLUE); 
     canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 
     BitmapDrawable finalresult = new BitmapDrawable(result); 
     return finalresult; 
    } 

我得到了下面的圖像,但我的實際需要是我必須在圖像周圍繪製邊框。

+1

http://stackoverflow.com/questions/3263611/border-for-an-image- view-in-android – Andy

回答

40

將其設置爲背景我放在一起爲自己以下。

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int color, int cornerDips, int borderDips, Context context) { 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 
      Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips, 
      context.getResources().getDisplayMetrics()); 
    final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) cornerDips, 
      context.getResources().getDisplayMetrics()); 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    final RectF rectF = new RectF(rect); 

    // prepare canvas for transfer 
    paint.setAntiAlias(true); 
    paint.setColor(0xFFFFFFFF); 
    paint.setStyle(Paint.Style.FILL); 
    canvas.drawARGB(0, 0, 0, 0); 
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint); 

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

    // draw border 
    paint.setColor(color); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeWidth((float) borderSizePx); 
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint); 

    return output; 
} 

學分,當然要http://ruibm.com/?p=184

0

遺憾的是沒有好的,整齊的「邊界」參數中的Android,但要做到這一點是包住另一個佈局中,父佈局的背景設置爲您的邊框顏色/繪製的最簡單方法然後在其上設置填充。填充將出現在您的BitmapDrawable周圍。

4

如何準備9修補圖像象下面這樣,通過使用android:background

enter image description here

+1

我喜歡這個解決方案。 –

+0

@ChristopherPerry :) –

2

我用BitmapShaderdrawRoundRect做到這一點,它爲我工作,看截圖

enter image description here

RectF roundRect; // the Rect you have to draw into 
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 

// draw the border at bottom 
mPaint.setStyle(Paint.Style.FILL); 
mPaint.setColor(mBorderColor); 
canvas.drawRoundRect(roundRect, mRadius, mRadius, mPaint); 

// ------------------ draw scheme bitmap 
roundRect.set(itemRect.left + mBorderSize, itemRect.top + mBorderSize, itemRect.right - mBorderSize, itemRect.bottom - mBorderSize); 
Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
mPaint.setShader(shader); 
canvas.drawRoundRect(roundRect, mRadius, mRadius, mPaint); 
mPaint.setShader(null); 
0

我做了很多搜索來實現這種效果t之前,我找到了另一種方式,但它不夠完美,你可以看到每個角落的鋸齒,我設置Paint.setAntiAlias(true),Paint.setDither(true),Paint.setFilterBitmap(true),但它doesn沒有用,所以我希望有人能幫助我。 enter image description here

RectF roundRect = new RectF(itemRect); 

Bitmap bitmap = scheme.getSchemeBitmap(getResources()); 

mPaint.setStyle(Paint.Style.FILL); 
mPaint.setColor(mSchemeSelectedColor); 
canvas.drawRoundRect(roundRect, mSchemeCornerRadius, mSchemeCornerRadius, mPaint); 

roundRect.set(itemRect.left + mBorderSize, itemRect.top + mBorderSize, itemRect.right - mBorderSize, itemRect.bottom - mBorderSize); 
Path clipPath = new Path(); 
clipPath.addRoundRect(roundRect, mSchemeCornerRadius, mSchemeCornerRadius, Path.Direction.CW); 
canvas.save(Canvas.CLIP_SAVE_FLAG); 
canvas.clipPath(clipPath); 
canvas.drawBitmap(bitmap, null, roundRect, mPaint); 
canvas.restore(); 
0

我的方式:

public static Bitmap getRoundedCornerBitmap1(Bitmap bitmap, int color, int cornerDips, int borderDips) { 


      Bitmap output = Bitmap.createBitmap(bitmap.getWidth()+2*borderDips, 
        bitmap.getHeight()+2*borderDips, 
        Bitmap.Config.ARGB_8888); 

      Canvas canvas = new Canvas(output); 

      canvas.drawColor(Color.TRANSPARENT); 

      final RectF rectF = new RectF(0, 0, output.getWidth(), output.getHeight()); 
      final Paint paint = new Paint(); 
      // prepare canvas for transfer 
      paint.setAntiAlias(true); 
      paint.setStrokeWidth((float) borderDips); 
      paint.setColor(Color.WHITE); 
      paint.setStyle(Paint.Style.FILL); 

      canvas.drawRoundRect(rectF, borderDips, borderDips, paint); 

      canvas.drawBitmap(bitmap, borderDips, borderDips, null); 
      bitmap.recycle(); 
      return output; 
     } 

結果

enter image description here