Android --- 自定义ImageView 实现圆形图片

自定义ImageView实现圆形图片,主要是在onDraw()方法中实现绘制圆形图片,在onMeasure()中测量圆形的半径并设置View的宽高。效果如下图

代码如下

public class CircleImageView extends ImageView {
<span style="color:rgb(128,128,128);">//画笔

private Paint mPaint;
//圆形图片的半径
private int mRadius;
//图片的宿放比例
private float mScale;

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

<span style="color:rgb(204,120,50);">public </span><span style="color:rgb(255,198,109);">CircleImageView</span>(Context context<span style="color:rgb(204,120,50);">, </span><span style="color:rgb(187,181,41);">@Nullable </span>AttributeSet attrs) {
    <span style="color:rgb(204,120,50);">super</span>(context<span style="color:rgb(204,120,50);">, </span>attrs)<span style="color:rgb(204,120,50);">;

}

<span style="color:rgb(204,120,50);">public </span><span style="color:rgb(255,198,109);">CircleImageView</span>(Context context<span style="color:rgb(204,120,50);">, </span><span style="color:rgb(187,181,41);">@Nullable </span>AttributeSet attrs<span style="color:rgb(204,120,50);">, int </span>defStyleAttr) {
    <span style="color:rgb(204,120,50);">super</span>(context<span style="color:rgb(204,120,50);">, </span>attrs<span style="color:rgb(204,120,50);">, </span>defStyleAttr)<span style="color:rgb(204,120,50);">;

}

<span style="color:rgb(187,181,41);">@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//由于是圆形,宽高应保持一致
int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
mRadius = size / 2;
setMeasuredDimension(size, size);
}

<span style="color:rgb(187,181,41);">@SuppressLint</span>(<span style="color:rgb(106,135,89);">"DrawAllocation"</span>)
<span style="color:rgb(187,181,41);">@Override

protected void onDraw(Canvas canvas) {

    <span style="color:rgb(152,118,170);">mPaint </span>= <span style="color:rgb(204,120,50);">new </span>Paint()<span style="color:rgb(204,120,50);">;


Drawable drawable = getDrawable();

if (null != drawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

//初始化BitmapShader,传入bitmap对象
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//计算缩放比例
mScale = (mRadius * 2.0f) / Math.min(bitmap.getHeight(), bitmap.getWidth());

Matrix matrix = new Matrix();
matrix.setScale(mScale, mScale);
bitmapShader.setLocalMatrix(matrix);
mPaint.setShader(bitmapShader);
//画圆形,指定好坐标,半径,画笔
canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
} else {
super.onDraw(canvas);
}
}

}

自定义好之后,就可以直接在xml布局中使用该控件

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:gravity=“center”
tools:context=“com.sun.zh.bezier.MainActivity”>
<com.sun.zh.bezier.view.CircleImageView
android:id="@+id/image"
android:layout_width=“200dp”
android:layout_height=“200dp”
android:scaleType=“centerCrop”
android:src="@drawable/ic_timg" />
</RelativeLayout>

注意必须设置src图,设置background图不会出现圆形效果

在ImageView中src与background的区别:

background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸。src是图片内容(前景),bg是背景,可以同时使用。此外scaleType只是对src起作用,bg可设置透明度。在动态加载图片中设置src可使用image.setImageResource(R.drawable.**)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值