2014-1104 updated: 我發現 Github - vinc3m1/RoundedImageView 蠻好用的!比下面的 code 還好用的原因是那段 code 切出來的圓角好像邊邊會有一點點鋸齒。。。


要做圓角 / 圓形的 ImageView 也是有百百種方法!
如果要圓角什麼的可以參考 Github - vinc3m1/RoundedImageView,或是 How to make an ImageView with rounded corners

這裡要介紹的是一個做圓形圖片很快的方法,來自 How to make ImageView rounded corner in android?

首先創一個 RoundedImageView class

public class RoundedImageView extends ImageView {

  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);
  }

  @Override
  protected void onDraw(Canvas canvas) {
       float radius = 90.0f; // angle of round corners
       Path clipPath = new Path();
       RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
       clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
       canvas.clipPath(clipPath);

       super.onDraw(canvas);
   }
}

之後在 xml 使用只要

 <com.example.rounded_image.RoundedImageView
        android:id="@+id/imageVIew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:contentDescription="img"/>

超快!

Comments

comments powered by Disqus
Copyright © 2013 Andro Chen
Powered by Logdown and Greyshade
Favicon from The Noun Project