2011-10-24 44 views
0
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { 
    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 = 12; 

    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; 
} 

我想使用此代碼來舍入位圖,但我不是什麼Mode.SRC_in和Config.ARGB_8888。我有他們的錯誤。我應該在這裏做什麼?圍繞位圖的四捨五入的邊框

+0

郵政logcat的輸出。 –

回答

2

對於PorterDuffXfermode,你必須寫import android.graphics.PorterDuffXfermode;

對於Config.ARGB_8888,你必須寫import android.graphics.Bitmap.Config;

否則直接按CTRL + SHIFT +Ø組織進口。

+1

感謝兄弟,它有助於 – user989340

+0

CTRL + O組織導入或CTRL + SHIFT + O? –

+0

Thanx Lalit糾正我。 –

2

取得了橢圓形的XML名稱它round_shape.xml

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" > 
    <stroke 
    android:width="1dp" 
    android:color="#bebebe" /> 
    </shape>  

設置此XML圖像視圖的背景像下面

<ImageView 
       android:id="@+id/img_profile" 
       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:padding="2dp" 
       android:scaleType="fitXY" 
       android:background="@drawable/round_shape"/> 

現在,你有一輪位圖並設置爲像這樣的imagebitmap資源img_profile.setImageBitmap(roundBit(selected_Pic_Bitmap));,對於以下四捨五入代碼圖像的位圖使用

公共位圖roundBit(位圖BM){

Bitmap circleBitmap = Bitmap.createBitmap(bm.getWidth(), 
      bm.getHeight(), Bitmap.Config.ARGB_8888); 

    BitmapShader shader = new BitmapShader(bm, TileMode.CLAMP, 
      TileMode.CLAMP); 
    Paint paint = new Paint(); 
    paint.setShader(shader); 
    paint.setAntiAlias(true); 
    Canvas c = new Canvas(circleBitmap); 

    c.drawCircle(bm.getWidth()/2, bm.getHeight()/2, bm.getWidth()/2, 
      paint); 

    return circleBitmap; 
} 

round_image_with_border