2013-04-07 54 views
4

我在電梯視圖中的每一行都有一個用戶圖標。我想要一個圓角。以下是我已經和它什麼都不做:在ListView行ImageView上的圓角

<ImageView 
     android:background="@drawable/profile_widget_selector" 
     android:id="@+id/ivTopUserPP" 
     android:layout_width="65dp" 
     android:layout_height="65dp" 
     android:baselineAlignBottom="@drawable/profile_pic_shape" 
     android:src="@drawable/usericon" /> 

profile_pic_shape.xml

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

    <solid android:color="#bbbbbb" /> 

    <stroke 
     android:width="1dp" 
     android:color="#bbb" /> 

    <padding 
     android:bottom="1dp" 
     android:left="1dp" 
     android:right="1dp" 
     android:top="1dp" /> 

    <corners android:topLeftRadius="40dp" /> 

</shape> 

回答

13

您可以使用通用圖像裝載機。

https://github.com/nostra13/Android-Universal-Image-Loader

可用於顯示帶圓角的位圖。檢查上面的鏈接。

在您的自定義適配器構造

File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder"); 

// Get singletone instance of ImageLoader 
imageLoader = ImageLoader.getInstance(); 
// Create configuration for ImageLoader (all options are optional) 
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a) 
     // You can pass your own memory cache implementation 
    .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation 
    .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) 
    .enableLogging() 
    .build(); 
    // Initialize ImageLoader with created configuration. Do it once. 
    imageLoader.init(config); 
    options = new DisplayImageOptions.Builder() 
.showStubImage(R.drawable.stub_id)//display stub image 
.cacheInMemory() 
.cacheOnDisc() 
.displayer(new RoundedBitmapDisplayer(20)) //rounded corner bitmap 
.build(); 

在你getView()

ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options 

你可以用其他選項配置,以滿足您的需求。帶圓角

羅曼球員圖像@http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

實測值以下@How to make an ImageView with rounded corners?

http://ruibm.com/?p=184

public class ImageHelper { 
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; 
} 
} 
+0

getRoundedCornerBitmap()方法是我一直在尋找 – 2013-09-24 07:53:43

+1

嗨,你能告訴我怎麼做圖像的右側頂部和右側BUTTOM圓而左側? – 2013-10-04 06:43:17

+0

@SampathKumar,你應該問一個新問題。此外,請檢查是否可以幫助您:http://stackoverflow.com/questions/15962745/draw-a-semicircle-in-the-background-of-a-view,或 http://developer.android.com/參考/機器人/圖形/ Canvas.html – dwbrito 2014-10-20 13:07:20