2014-02-25 31 views
1

我開發了一個rss應用程序。我希望包含標題和圖像的ListView具有帶圓角的圖像。我在線提供了示例代碼,但問題是圖像仍然是矩形。奇怪的部分是我有一個滑動菜單,當它切換時會將rs ListView推開,而它被推動的圖像有圓角!當他們停止推動動畫時,再次變成長方形。對我來說這是一個非常奇怪的問題,所以幫助? 四捨五入的圖像類:帶有圓角的ListView中的圖像

public class RoundedImageView extends ImageView 
{ 
    private float radius = 20.0f; 

    public RoundedImageView(Context context) 
    { 
     super(context); 
    } 

    public RoundedImageView(Context context,AttributeSet attrs) 
    { 
     super(context,attrs); 
    } 

    public RoundedImageView(Context context,AttributeSet attrs,int defStyle) 
    { 
     super(context,attrs,defStyle); 
    } 

    @SuppressLint("DrawAllocation") 
    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     Path clipPath = new Path(); 
     RectF rect = new RectF(0,0,getWidth(), getHeight()); 
     clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW); 
     canvas.clipPath(clipPath); 
     super.onDraw(canvas); 
    } 
} 
+4

爲什麼ü這樣做?創建一個圓角的XML並設置爲您的imageview的背景。 –

+1

https://github.com/vinc3m1/RoundedImageView – naidu

回答

3

試試這個方法:創建rounded_corner.xml文件到drawable\rounded_corner.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 

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

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

<corners android:radius="15dp" /> 

,並設置爲BackgroundImageView

android:background="@drawable\rounded_corner" 

,還可以設置你的RSS圖像0​​爲Src或者像Bitmap一樣:

imageview.setBitmap(yourrssimage); 
+0

我試過它仍然是不圓角的角落。奇怪的是,使用我公佈的代碼的代碼是模擬器上的圓形,但是在我的筆記II中,它們不是。 – nick28

+0

更改XML文件中的半徑並嘗試。或發佈您的所有佈局和代碼。你可能做錯了什麼。 –

+0

好吧我只注意到一些rss項目沒有圖像源,默認圖像有圓角,但有圖像源的是矩形。我使用setImageURI來設置圖像源。如何將uri更改爲位圖以使用setBitmap方法? – nick28

0
使用

圓角圖像或創建xml和設置爲上ImageView的背景圖像。

0

在您的可繪製文件夾中創建一個如下所示的XML。

button_back.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:state_enabled="false" 
    android:drawable="@color/grey"> 

    <shape> 
     <solid 
      android:color="#ef4444" /> 
     <stroke 
      android:width="1dp" 
      android:color="#992f2f" /> 
     <corners 
      android:radius="16dp" /> 
    </shape> 
</item> 

,然後應用到你的按鈕,像這樣。

<Button 
     android:text="@string/press_me" 
     android:layout_margin="12dp" 
     android:layout_width="fill_parent" 
     android:layout_height="30dp" 

     android:background="@drawable/button_back" 
/> 
0

創建一個行程,並列出它作爲你imageview的背景

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="@android:color/transparent"/> 
    <stroke android:width="5dip" android:color="@android:color/transparent" /> 
    <corners android:radius="5dip"/> 
    <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" /> 
</shape> 

並在您的ImageView XML

android:background="@drawable/your_stroke_xml" 
+0

它適用於單個圖像,而不是ListView中的圖像。此外,現在,當我上下滾動時,屏幕頂部出現一個空白區域。 – nick28

+0

@ nick28我有類似的問題。你找到解決方案了嗎? – Bresiu

+0

@Bresiu我發佈在我的問題上的課程與我完全一致。問題是我使用調用MIUI的自定義ROM,我已經返回到CM ROM,並且類按預期工作:) – nick28