2013-04-01 201 views
16

我正在搜索過去的一天,但我沒有成功。將方形圖像裁剪成圓形 - 以編程方式

我從API獲取圖像,並使用下面的代碼將它下載到位圖文件中。

private Bitmap DownloadImage(String URL) 
    { 
     Bitmap bitmap = null; 
     InputStream in = null; 
     try 
     { 
      in = OpenHttpConnection(URL); 
      bitmap = BitmapFactory.decodeStream(in); 
      in.close(); 
     } 
     catch (IOException e1) 
     { 
      e1.printStackTrace(); 
     } 
     return bitmap; 
    } 

    private InputStream OpenHttpConnection(String urlString) throws IOException 
    { 
     InputStream in = null; 
     int response = -1; 

     URL url = new URL(urlString); 
     URLConnection conn = url.openConnection(); 

     if (!(conn instanceof HttpURLConnection)) 
      throw new IOException("Not an HTTP connection"); 

     try 
     { 
      HttpURLConnection httpConn = (HttpURLConnection) conn; 
      httpConn.setAllowUserInteraction(false); 
      httpConn.setInstanceFollowRedirects(true); 
      httpConn.setRequestMethod("GET"); 
      httpConn.connect(); 

      response = httpConn.getResponseCode(); 
      if (response == HttpURLConnection.HTTP_OK) 
      { 
       in = httpConn.getInputStream(); 
      } 
     } 
     catch (Exception ex) 
     { 
      throw new IOException("Error connecting"); 
     } 
     return in; 
    } 

而我得到的圖像作爲一個正方形,我想裁剪四個角落,並使其成爲圓形圖像。有沒有什麼可能的辦法來實現?

歡迎任何相關答案。提前致謝 。

+0

不確定關閉我的頭頂,但我想作爲替代方法可以創建一個alpha圖像,並在原始圖像頂部切出大小不一的洞。與使用圓圈類並進行適當的編輯相比,這並不理想,但如果您找不到所需的內容並需要快速解決方法,則可以選擇其他方法。 –

回答

19
public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     DrawingView dv = new DrawingView(this); 
     setContentView(dv); 
    } 

    class DrawingView extends View { 
     Bitmap bitmap; 

     public DrawingView(Context context) { 
      super(context); 
      bitmap = BitmapFactory.decodeResource(context.getResources(), 
        R.drawable.glossy_overlay); 

     } 

     @Override 
     public void onDraw(Canvas canvas) { 
      Paint paint = new Paint(); 
      // paint.setColor(Color.CYAN); 
      canvas.drawBitmap(getclip(), 30, 20, paint); 
     } 

     public Bitmap getclip() { 
      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()); 

      paint.setAntiAlias(true); 
      canvas.drawARGB(0, 0, 0, 0); 
      // paint.setColor(color); 
      canvas.drawCircle(bitmap.getWidth()/2, 
        bitmap.getHeight()/2, bitmap.getWidth()/2, paint); 
      paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
      canvas.drawBitmap(bitmap, rect, rect, paint); 
      return output; 
     } 
    } 
} 
+2

每次調用onDraw()時,此代碼都會重新創建剪切的位圖。只有當源位圖改變並繪製時,才能更好地創建對剪裁的drawable的引用。實際上,您也可以失去對源位圖的引用。在繪圖循環之外分配Paint也更好。 – greg7gkb

+0

@採取的措施將改善後當我有時間 – Raghunandan

0

這可以用XML來簡單地完成,請在這裏看到我的回答: https://stackoverflow.com/a/18287979/665930

<RelativeLayout 
      android:id="@+id/icon_layout" 
      android:layout_width="@dimen/icon_mask" 
      android:layout_height="@dimen/icon_mask" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" > 

      <ImageView 
       android:id="@+id/icon" 
       android:layout_width="@dimen/icon" 
       android:layout_height="@dimen/icon" 
       android:layout_centerInParent="true" 
       android:scaleType="fitXY" > 
      </ImageView> 

      <ImageView 
       android:id="@+id/icon_mask" 
       android:layout_width="@dimen/icon_mask" 
       android:layout_height="@dimen/icon_mask" 
       android:layout_centerInParent="true" 
       android:background="@drawable/circle" 
       android:scaleType="fitXY" > 
      </ImageView> 


</RelativeLayout> 


<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > 
    <gradient android:startColor="#00FFFFFF" android:endColor="#00FFFFFF" 
     android:angle="270"/> 
    <stroke android:width="10dp" android:color="#FFAAAAAA"/> 
13

使用功能的打擊繪製位圖圓圈,然後圓圈位設置爲imageview的

public static Bitmap getclip(Bitmap bitmap) { 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 
      bitmap.getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

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

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, 
      bitmap.getWidth()/2, paint); 
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 
    return output; 
} 
+0

大功能,但是這行greg7gkb建議做: paint.setXfermode(新PorterDuffXfermode(Mode.SRC_IN)); – bebosh

+0

@bebosh設置了位圖的繪圖模式(請參閱http://ssp.impulsetrain.com/porterduff.html)...或者您問爲什麼需要調用此方法? – greg7gkb

+0

此自定義方法可以正常工作,但不會產生錯誤,但會生成一個水平橢圓形的圖像。使用RoundedBitmapDrawableFactory產生一個完美的圓圈,只是FYI。 – JamisonMan111

2

Roman Nurik提出了一個非常直接的着色器來使用自定義drawable來做類似的事情。

我改變了一下代碼來做一個橢圓形的圖像並測試自己。效果和性能都非常好:

public class StreamDrawable extends Drawable { 
private static final boolean USE_VIGNETTE = true; 

private final RectF mRect = new RectF(); 
private final BitmapShader mBitmapShader; 
private final Paint mPaint; 
private final int mMargin; 

public StreamDrawable(Bitmap bitmap, int margin) { 

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

    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setShader(mBitmapShader); 

    mMargin = margin; 
} 

@Override 
protected void onBoundsChange(Rect bounds) { 
    super.onBoundsChange(bounds); 
    mRect.set(mMargin, mMargin, bounds.width() - mMargin, bounds.height() - mMargin); 

    if (USE_VIGNETTE) { 
     RadialGradient vignette = new RadialGradient(
       mRect.centerX(), mRect.centerY() * 1.0f/0.7f, mRect.centerX() * 1.3f, 
       new int[] { 0, 0, 0x7f000000 }, new float[] { 0.0f, 0.7f, 1.0f }, 
       Shader.TileMode.CLAMP); 

     Matrix oval = new Matrix(); 
     oval.setScale(1.0f, 0.7f); 
     vignette.setLocalMatrix(oval); 

     mPaint.setShader(
       new ComposeShader(mBitmapShader, vignette, PorterDuff.Mode.SRC_OVER)); 
    } 
} 

@Override 
public void draw(Canvas canvas) { 
    canvas.drawOval(mRect, mPaint); 
} 

@Override 
public int getOpacity() { 
    return PixelFormat.TRANSLUCENT; 
} 

@Override 
public void setAlpha(int alpha) { 
    mPaint.setAlpha(alpha); 
} 

@Override 
public void setColorFilter(ColorFilter cf) { 
    mPaint.setColorFilter(cf); 
} 
} 
14

一旦位圖被檢索RoundedBitmapDrawableFactory可以用來產生從v4 Support Library一個RoundedBitmapDrawableDrawable然後可以應用於ImageView或直接繪製到Canvas

// Create the RoundedBitmapDrawable. 
RoundedBitmapDrawable roundDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap); 
roundDrawable.setCircular(true); 

// Apply it to an ImageView. 
ImageView imageView = (ImageView)findViewById(R.id.imageView); 
imageView.setImageDrawable(roundDrawable); 

// Alternatively, draw it to an canvas (e.g. in onDraw where a Canvas is available). 
// setBounds since there's no View handling size and positioning. 
roundDrawable.setBounds(left, top, right, bottom); 
roundDrawable.draw(canvas); 
+2

非常感謝Godfrey,這個答案是最簡單和最好的答案。這應該是被接受的答案! – JamisonMan111

+0

非常感謝你,這是非常有益的@Godfrey公爵 – Shruti