2012-10-17 14 views
7

Czy ktoś wie, jak przyciąć obraz \ bitmap do okręgu? nie mogę znaleźć żadnego rozwiązania, przepraszam ..Crop Image as circle in Android

+0

@ user1281750 czytać dokładnie szczegółów pytanie! To nie jest duplikat jak! –

+0

możliwy duplikat [Przycinanie obszaru okrągłego z bitmapy w systemie Android] (http://stackoverflow.com/questions/11932805/cropping-circular-area-from-bitmap-in-android) – Adinia

Odpowiedz

1

Spróbuj z kodem poniżej:

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) { 
    // TODO Auto-generated method stub 
    int targetWidth = 50; 
    int targetHeight = 50; 
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
          targetHeight,Bitmap.Config.ARGB_8888); 

       Canvas canvas = new Canvas(targetBitmap); 
    Path path = new Path(); 
    path.addCircle(((float) targetWidth - 1)/2, 
    ((float) targetHeight - 1)/2, 
    (Math.min(((float) targetWidth), 
       ((float) targetHeight))/2), 
      Path.Direction.CCW); 

       canvas.clipPath(path); 
    Bitmap sourceBitmap = scaleBitmapImage; 
    canvas.drawBitmap(sourceBitmap, 
           new Rect(0, 0, sourceBitmap.getWidth(), 
    sourceBitmap.getHeight()), 
           new Rect(0, 0, targetWidth, 
    targetHeight), null); 
    return targetBitmap; 
} 
+0

Nie działa. Proszę, zasugeruj mi. – Arun

2

Klasa:

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) { 

    int targetWidth = 50; 
    int targetHeight = 50; 
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
         targetHeight,Bitmap.Config.ARGB_8888); 
         canvas = new Canvas(targetBitmap); 
    Path path = new Path(); 
    path.addCircle(((float) targetWidth - 1)/2, 
    ((float) targetHeight - 1)/2, 
    (Math.min(((float) targetWidth), 
      ((float) targetHeight))/2), 
     Path.Direction.CCW); 

    canvas.clipPath(path); 
    Bitmap sourceBitmap = scaleBitmapImage; 
    canvas.drawBitmap(sourceBitmap, 
          new Rect(0, 0, sourceBitmap.getWidth(), 
     sourceBitmap.getHeight()), 
          new Rect(0, 0, targetWidth, 
     targetHeight), null); 
    return targetBitmap; 
    } 

Widok:

<ImageView 
     android:id="@+id/imgView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btnEdit" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="40dp" 
     android:background="@drawable/rounded" 
     android:adjustViewBounds="true" 
     android:gravity="center" 
     android:src="@drawable/happy"/> 

Dodatkowe style:

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

<solid android:color="@android:color/white" /> 

<stroke 
    android:width="3dip" 
    android:color="#FF0000" /> 

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

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

2

Romain Guy, dawniej inżynier na Androida zespół Google, pisał doskonały artykuł na drawing images with rounded corners. Pomysł ten można łatwo rozszerzyć na okrąg, na przykład zmieniając zaokrąglony promień prostokąta, aby utworzyć pełny okrąg.

Z artykułu:

Aby wygenerować zaokrąglone obrazy po prostu napisał niestandardową Drawable że rysuje zaokrąglonego prostokąta przy użyciu Canvas.drawRoundRect(). Sztuką jest , aby użyć Paint z BitmapShader do wypełnienia zaokrąglonego prostokąta z teksturą zamiast prostego koloru. Oto co kod wygląda następująco:

BitmapShader shader; shader = new BitmapShader(bitmap, 
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 

Paint paint = new Paint(); paint.setAntiAlias(true); 
paint.setShader(shader); 

RectF rect = new RectF(0.0f, 0.0f, width, height); 

// rect contains the bounds of the shape 
// radius is the radius in pixels of the rounded corners 
// paint contains the shader that will texture the shape 
canvas.drawRoundRect(rect, radius, radius, paint); 
+1

Canvas wyświetla również metodę [drawCircle] (http://developer.android.com/reference/android/graphics/Canvas.html), która może być bardziej odpowiednia w tym przypadku. – greg7gkb

0
finalBitmapShader shader = newBitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
mPaint.setShader(shader); 
mBitmapWidth=mBitmap.getWidth(); 
mBitmapHeight=mBitmap.getHeight(); 
} 
@Override 
public void draw(Canvas canvas{ 
    canvas.drawOval(mRectF,mPaint); 
} 
@Override 
protected void onBoundsChange(Rect bounds) { 
    super.onBoundsChange(bounds); 
    mRectF.set(bounds); 
} 

tutaj znalazłem tutorial próbki nim na http://androidgreeve.blogspot.in/2014/09/facebook-messanger-like-profile-image.html?m=1

1

Wiseman Designs, mają open source Circular ImageView gotowy do użycia

https://github.com/wisemandesigns/CircularImageView

Używa XML w twoich układach, które ułatwia życie. Możesz ustawić źródło w XML lub przy niewielkich modyfikacjach możesz z łatwością użyć Bitmapy.

Disclaimer: pracować dla Wiseman Designs

1

Dla posiadające zaokrąglone narożniki dla ImageView, konwertować obraz na bitmapę, a następnie spróbuj poniższy kod:

 private Bitmap getRoundedCroppedBitmap(Bitmap bitmap) { 
     int widthLight = bitmap.getWidth(); 
     int heightLight = bitmap.getHeight(); 

     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),Config.ARGB_8888); 

     Canvas canvas = new Canvas(output); 
     Paint paintColor = new Paint(); 
     paintColor.setFlags(Paint.ANTI_ALIAS_FLAG); 

     RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight)); 

     canvas.drawRoundRect(rectF, widthLight/2 ,heightLight/2,paintColor); 

     Paint paintImage = new Paint(); 
     paintImage.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP)); 
     canvas.drawBitmap(bitmap, 0, 0, paintImage); 

     return output; 
    }