2012-01-23 186 views
0

在我的應用程序中,我從圖庫中獲取圖像,並且該圖像的形狀位於正方形中我想將該圖像設置爲imageView,然後它應該是橢圓形。就我而言,我需要像人臉一樣剪切圖像。任何人都可以提前告訴我該怎麼做。如何將方形圖像轉換爲橢圓形

+0

的可能的複製[如何裁剪在橢圓形或Android的面罩形狀圖像?](http://stackoverflow.com/questions/15200214/how-to-crop-image-in-oval字形或 - 兼具口罩的形狀在-機器人) – bummi

回答

5

使用以下類代替圖像視圖。

RoundedCornerImageView imageView1; 
imageView1.setRadius(10); 

這將使圖像半徑增加10px,您可以給出想要的wat值並將其設置爲您想要的形狀。試試。

一切順利:)

public class RoundedCornerImageView extends ImageView { 
    private int radius = 10; 

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

    protected void onDraw(Canvas canvas) { 
     Path clipPath = new Path(); 
     int w = this.getWidth(); 
     int h = this.getHeight(); 
     clipPath.addRoundRect(new RectF(0, 0, w, h), radius, radius, Path.Direction.CW); 
     canvas.clipPath(clipPath); 
     super.onDraw(canvas); 
    } 

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

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

    public void setRadius(int radius){ 
     this.radius = radius; 
     this.invalidate(); 
    } 
} 
4

您可以使用此

public Drawable getRoundedCornerImage(Drawable bitmapDrawable) { 
     Bitmap bitmap = ((BitmapDrawable)bitmapDrawable).getBitmap(); 
     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 = 100; 

     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); 
     Drawable image = new BitmapDrawable(output); 
     return image; 

    } 

希望這有助於你

-1

enter image description here

您也可以使用此或Download demo code example

public class Shape { 

    private Bitmap bmp; 
    private ImageView img; 
    public Shape(Bitmap bmp, ImageView img) { 

     this.bmp=bmp; 
     this.img=img; 
     onDraw(); 
    } 


    private void onDraw(){ 

     Canvas canvas=new Canvas(); 
     if (bmp.getWidth() == 0 || bmp.getHeight() == 0) { 
      return; 
     } 

     int w = bmp.getWidth(), h = bmp.getHeight(); 

     Bitmap roundBitmap = getOvalCroppedBitmap(bmp, w); 

     img.setImageBitmap(roundBitmap); 

    } 


    public static Bitmap getOvalCroppedBitmap(Bitmap bitmap, int radius) { 
     Bitmap finalBitmap; 
     if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) 
      finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, 
        false); 
     else 
      finalBitmap = bitmap; 
     Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(), 
       finalBitmap.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), 
       finalBitmap.getHeight()); 

     paint.setAntiAlias(true); 
     paint.setFilterBitmap(true); 
     paint.setDither(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(Color.parseColor("#BAB399")); 
     RectF oval = new RectF(0, 0, 130, 150); 
     canvas.drawOval(oval, paint); 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
     canvas.drawBitmap(finalBitmap, rect, oval, paint); 

     return output; 
    } 

最後,在您的主要活動中,實例化您的類Shape並將兩個參數傳遞給您的類。要裁剪成橢圓形的圖像以及將設置最終圖像的圖像視圖。

Download demo code example