2015-04-30 78 views
-2

我試圖做位圖圈工作,但高分辨率圖像無法正常工作。它像圖像視圖中的小圓圈一樣工作。位圖圈不正確

我已經嘗試此代碼,我試過很多其他的代碼,卻沒有正常工作

public static Bitmap circleShape(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()); 

     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(color); 
     // canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
     canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, 
       bitmap.getWidth()/2, paint); 
     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 
     //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false); 
     //return _bmp; 
     return output; 
} 
+1

檢查我的答案http://stackoverflow.com/questions/18378741/how-to-make-an-imageview-in-circular-形狀/ 18378873#18378873 – Piyush

+0

其非常模糊,我已測試 –

+0

張貼您的輸出。前面評論中的代碼將圖像下采樣到50 * 50,'targetWidth'和'targetHeight'指定如何對原始圖像進行採樣。 – tom91136

回答

2

我已經嘗試了這麼多的代碼,並且沒有完美的工作。我已搜索並找到此代碼。它工作真棒,驚人的..希望有人樂於助人的這段代碼..謝謝

public static Bitmap circleShape(Bitmap source) { 
    int size = Math.min(source.getWidth(), source.getHeight()); 

    int x = (source.getWidth() - size)/2; 
    int y = (source.getHeight() - size)/2; 

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); 
    if (squaredBitmap != source) { 
     source.recycle(); 
    } 

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); 

    Canvas canvas = new Canvas(bitmap); 
    Paint paint = new Paint(); 
    BitmapShader shader = new BitmapShader(squaredBitmap, 
      BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); 
    paint.setShader(shader); 
    paint.setAntiAlias(true); 

    float r = size/2f; 
    canvas.drawCircle(r, r, r, paint); 

    squaredBitmap.recycle(); 
    return bitmap; 

}