android 自定义圆角头像以及使用declare-styleable进行配置属性解析

博客介绍了如何在Android项目中实现圆角头像,特别是四角带弧度的圆角头像。通过声明自定义属性declare-styleable在attrs.xml文件中,然后在布局文件中配置相关属性,最后在自定义控件中读取这些属性,实现头像的定制。这种方法提高了代码的复用性。
摘要由CSDN通过智能技术生成

       由于最新项目中正在检查UI是否与效果图匹配,结果关于联系人模块给的默认图片是四角稍带弧度的圆角,而我们截取的图片是正方形的,现在要给应用统一替换。应用中既用到大圆角头像(即整个头像是圆的)又用到四角稍带弧度的圆角头像,封装一下以便重用。以下直接见代码

package com.test.demo;
import com.test.demo.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ImageView;

/**
 * 圆角imageview
 */
public class RoundImageView extends ImageView {

    private static final String TAG = "RoundImageView";
    /**
     * 图片的类型,圆形or圆角
     */
    private int type;
    public static final int TYPE_CIRCLE = 0;
    public static final int TYPE_ROUND = 1;
    /**
     * 圆角大小的默认值
     */
    private static final int CORNER_RADIUS_DEFAULT = 10;
    /**
     * 圆角的大小
     */
    private int mCornerRadius;

    /**
     * 绘图的Paint
     */
    private Paint mBitmapPaint;

    // 按下状态颜色
    private Paint mPressedColorPaint;
    private int pressedColor;

    /**
     * 圆角的半径
     */
    private int mRadius;
    /**
     * 3x3 矩阵,主要用于缩小放大
     */
    private Matrix mMatrix;
    /**
     * 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);

        pressedColor = a.getColor(R.styleable.RoundImageView_pressed_color, -1);
        if (pressedColor != -1) {
            mPressedColorPaint = new Paint();
            mPressedColorPaint.setAntiAlias(true);
            mPressedColorPaint.setColor(pressedColor);
        }

        mCornerRadius = a.getDimensionPixelSize(
                R.styleable.RoundImageView_corner_radius, (int) TypedValue
                        .applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                CORNER_RADIUS_DEFAULT, getResources()
                                        .getDisplayMetrics()));// 默认为10dp
        type = a.getInt(R.styleable.RoundImageView_type, TYPE_CIRCLE);// 默认为Circle

        a.recycle();
    }

    public RoundImageView(Context context) {
        this(context, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        /**
         * 如果类型是圆形,则强制改变view的宽高一致,以小值为准
         */
        if (type == TYPE_CIRCLE) {
            mWidth = Math.min(MeasureSpec.getSize(widthMeasureSpec),
                    MeasureSpec.getSize(heightMeasureSpec));
            mRadius = mWidth / 2;
        }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值