Android圖片圓角控件
繪製圓角圖片控件
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import com.android.volley.toolbox.NetworkImageView;
public class RoundCornerImageView extends NetworkImageView {
public RoundCornerImageView(Context context) {
super(context);
}
public RoundCornerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundCornerImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
if (getDrawable() == null) {
return;
}
Path clipPath = new Path();
int w = this.getWidth();
int h = this.getHeight();
float rx=15.0f;
float ry=15.0f;
clipPath.addRoundRect(new RectF(0, 0, w, h), rx, ry, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
}
在佈局文件中使用:
<com.swissabl.utils.RoundCornerImageView
android:id="@+id/id_image_kaquan_name_pic"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerCrop"
android:padding="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
在Java代碼裏綁定圖片:
public class ImageActivity{
private RequestQueue queue;
private ImageLoader imageLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
queue = Volley.newRequestQueue(this);
imageLoader = new ImageLoader(queue, new BitmapCache());
RoundCornerImageView id_image_kaquan_name_pic= (RoundCornerImageView) findViewById(R.id.id_image_kaquan_name_pic);
String url="http://image.xinmin.cn/2012/11/01/20121101105203854339.jpg";
id_image_kaquan_name_pic.setImageUrl(url,imageLoader);
}
}