2014-01-08 154 views
1

我使用這個代碼來獲取頂級四捨五入位圓形頂角

public static void setTopRounded(Bitmap workingBitmap , int w, int h, ImageView v,Context context) 
{ 
    Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(bmp); 
    Shader shader = new BitmapShader(workingBitmap, Shader.TileMode.MIRROR, 
      Shader.TileMode.MIRROR); 

    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG); 
    paint.setAntiAlias(true); 
    paint.setShader(shader); 
    RectF rec = new RectF(0, 0, w, h - 20); 
    c.drawRect(new RectF(0, 20, w, h), paint); 
    c.drawRoundRect(rec, 10, 10, paint); 
    v.setBackgroundDrawable(new BitmapDrawable(context.getResources(), bmp)); 
} 

的角落和圖像查看我的XML代碼

<ImageView 
    android:layout_height="140dp" 
    android:layout_width="match_parent" 
    android:id="@+id/profileIV" 
    android:visibility="gone" 
    android:scaleType="centerCrop" /> 

現在的問題是通過設置

v.setBackgroundDrawable(new BitmapDrawable(context.getResources(), bmp))android:scaleType="centerCrop"

財產沒有工作,如果我使用

v.setImageDrawable(new BitmapDrawable(context.getResources(), bmp))

然後規模類型的作品,但圖像沒有得到圓。 我做錯了什麼?

+0

你爲什麼要同時使用 - 的drawRect()和drawRoundRect()? –

+0

setBackgroundDrawable()已棄用。相反,使用setBackground() –

回答

0

我找到了答案。我沒有改變任何事情。對頂部圓角的圖像使用相同的功能,並在圖像視圖中使用位圖作爲背景。但現在做CenterCrop代碼爲ImageView的使用ThumbnailUtils

首先我用下面的函數來獲得縮放CenterCroped圖像

Bitmap resizedProfileBitmap = ThumbnailUtils.extractThumbnail(profileBitmap, voucherTitleRLT.getWidth(), height); 

,然後用頂四捨五入函數來獲得機頂盒的圓形中心croped形象imageview的背景像下面

setTopRounded(resizedProfileBitmap, resizedProfileBitmap.getWidth(), resizedProfileBitmap.getHeight(), profileIV, thisContext); 
+0

setTopRounded屬於哪個類? –

0

嘗試v.setImageBitmap(bmp)代替

+1

這樣做,但同樣的問題。圖像沒有變圓。 – FIXI

0
/** 
    * This method used for bitmap compress and rounded corner 
    * @param Bitmap object 
    * @param pixels 
    * @return nothing 
    */ 
    public static Bitmap getRoundedRectBitmap(final Bitmap bitmap,final int pixels) 
    { 
     int color; 
     float roundPx; 
     final Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); 
     final Canvas canvas = new Canvas(result); 
     color = 0xff424242; 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     final RectF rectF = new RectF(rect); 
     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 result; 
    } 

//調用上述方法這樣

profileImage.setImageBitmap(getRoundedRectBitmap(位圖,50));

+0

只是發送你的位圖到這個方法,它會返回舍入的image.profileImage.setImageBitmap(getRoundedRectBitmap(bitmap,50)); – Ruban

+0

我發送了50作爲參數,因爲我需要小圖片,您可以通過增加尺寸來獲得大圖片 – Ruban

0

您可以通過創建一個Shape Drawable並將其設置爲您的ImageView的背景在XML做到這一點。本文介紹瞭如何:http://www.techrepublic.com/article/pro-tip-round-corners-on-an-android-imageview-with-this-hack/#

你的形狀繪製可以是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 

<solid android:color="#00ffffff" /> 
<padding 
    android:left="1dp" 
    android:top="1dp" 
    android:right="1dp" 
    android:bottom="1dp" /> 
<corners 
    android:topLeftRadius="6dp" 
    android:topRightRadius="6dp" 
    android:bottomLeftRadius="0.1dp" 
    android:bottomRightRadius="0.1dp" /> 
<stroke 
    android:width="6dp" 
    android:color="#ffffffff" />