2012-12-19 10 views
6

Ho un ImageView ed e sto cercando di creare angoli arrotondati, ha provato tutte le soluzioni di questo post: How to make an ImageView with rounded corners? Ma niente ha funzionato .. Ecco la mia XMLAndroid cercando di arrotondare gli angoli bitmap

<RelativeLayout 
    android:id="@+id/RL_ImageHolder" 
    android:layout_width="150dp" 
    android:layout_height="180dp" 
    android:layout_alignBottom="@+id/relativeLayout2" 
    android:layout_alignParentLeft="true" 
    android:layout_alignTop="@+id/relativeLayout2" 
    android:layout_marginLeft="10dp" > 

    <ImageView 
     android:id="@+id/imgPreview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:contentDescription="Preview" 

     /> 
</RelativeLayout> 

ed è così che ho impostato l'ImageView utilizzando il metodo accennato nel post che ho linkato sopra:

l'immagine è impostato correttamente, ma mantiene un rettangolo .. Tutte le idee perché non funziona?

MODIFICA: appena scoperto che non funziona solo quando il layout che contiene l'immagine ha una larghezza/altezza fissa .. Devi pensare a come gestirlo. Grazie ragazzi

+0

controllare: [Android Ricetta n. 1, immagine con angoli arrotondati] (http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners) di [Romain Guy] (http://stackoverflow.com/users/298575/romain-guy) –

risposta

7

prova con sottostante Codice Snippet:

public static Bitmap GetCurveImage(Bitmap bitmap) { 
     // Bitmap myCoolBitmap = ... ; // <-- Your bitmap you 
     // want rounded 
     int w = bitmap.getWidth(), h = bitmap.getHeight(); 

     // We have to make sure our rounded corners have an 
     // alpha channel in most cases 
     Bitmap rounder = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(rounder); 

     // We're going to apply this paint eventually using a 
     // porter-duff xfer mode. 
     // This will allow us to only overwrite certain pixels. 
     // RED is arbitrary. This 
     // could be any color that was fully opaque (alpha = 
     // 255) 
     Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     xferPaint.setColor(Color.RED); 

     // We're just reusing xferPaint to paint a normal 
     // looking rounded box, the 20.f 
     // is the amount we're rounding by. 
     canvas.drawRoundRect(new RectF(0, 0, w, h), 5.0f, 5.0f, xferPaint); 

     // Now we apply the 'magic sauce' to the paint 
     xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 

     Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), 
       bitmap.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas resultCanvas = new Canvas(result); 
     resultCanvas.drawBitmap(bitmap, 0, 0, null); 
     resultCanvas.drawBitmap(rounder, 0, 0, xferPaint); 

     return result; 
    } 

spero che vi aiuterà.

+4

Può essere ottenuto con meno codice e in modo più efficiente chiamando Canvas.drawRect() e impostando un BitmapShader sul Paint. –

+0

@RomainGuy: Sì, hai ragione. Scusa se non ne ero a conoscenza fino ad ora. I tuoi suggerimenti sono davvero belli e Straight Forward. Applicherò il tuo codice quando ho bisogno di applicare un'immagine arrotondata nel mio progetto. Grazie. :) –

Problemi correlati