展开全部
方法1:自定义view,使用BitmapShader
首先自定义一些62616964757a686964616fe78988e69d8331333337626138view的属性
下面定义了一个枚举和一个圆角的大小borderRadius。<?xml version="1.0" encoding="utf-8"?>
自定义View代码如下public class RoundImageView extends ImageView
{
/** 图片的类型,圆形or圆角 */
private int type;
private static final int TYPE_CIRCLE = 0;
private static final int TYPE_ROUND = 1;
/** 圆角大小的默认值 */
private static final int BODER_RADIUS_DEFAULT = 10;
/** * 圆角的大小 */
private int mBorderRadius;
/** * 绘图的Paint */
private Paint mBitmapPaint;
/** * 圆角的半径 */
private int mRadius;
/** * 3x3 矩阵,主要用于缩小放大 */
private Matrix mMatrix;
/** * 渲染图像,使用图像为绘制图形着色 */
private BitmapShader mBitmapShader;
/** * view的宽度 */
private int mWidth;
private RectF mRoundRect;
public RoundImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
mMatrix = new Matrix();
mBitmapPaint = new Paint();
mBitmapPaint.setAntiAlias(true);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.RoundImageView);
mBorderRadius = a.getDimensionPixelSize(
R.styleable.RoundImageView_borderRadius, (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
BODER_RADIUS_DEFAULT, getResources()
.getDisplayMetrics()));// 默认为10dp
type = a.getInt(R.styleable.RoundImageView_type, TYPE_CIRCLE);// 默认为Circle
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSp