自定义View--圆形ImageView

本文介绍了如何使用自定义属性、BitmapShader和Xfermode来实现圆形ImageView。首先,在attrs.xml中定义了边框颜色、宽度等属性,然后在布局文件中应用这些属性。接着,通过在自定义View中获取这些属性,实现了两种实现方式:一是利用Xfermode,二是运用BitmapShader。最后展示了实现效果。
摘要由CSDN通过智能技术生成

实现圆形ImageView方法很多,常见的就是利用BitmapShader或Xfermode来实现。我们还可以用自定义属性来自定义圆形边框。

定义自定义属性

在values文件夹中新建attrs.xml,定义边框颜色、边框宽度、实现方式。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CircleImageView">
        <attr name="border_width" format="dimension" />
        <attr name="border_color" format="color" />
        <attr name="draw_style" format="enum" >
            <enum name="Xfermode" value="0"/>
            <enum name="BitmapShader" value="1"/>
        </attr>
    </declare-styleable>
</resources>

在layout文件中的自定义属性设置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context="wfz.mycircleimageview.MainActivity">

    <wfz.mycircleimageview.CircleImageView
        android:layout_marginTop="50dp"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:scaleType="fitXY"
        android:src="@drawable/butterfly_lg"
        app:draw_style="Xfermode"
        app:border_width="3dp"
        app:border_color="@color/colorPrimary"/>

    <wfz.mycircleimageview.CircleImageView
        android:layout_marginTop="50dp"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:scaleType="fitXY"
        android:src="@drawable/butterfly_lg"
        app:draw_style="BitmapShader"
        app:border_width="3dp"
        app:border_color="@color/colorPrimary"/>

</LinearLayout>
获取自定义属性

在自定义view中获取自定义属性

public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CircleImageView, defStyleAttr, 0);
    mBorderWidth = a.getDimensionPixelSize(
                R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);
    mBorderColor = a.getColor(R.styleable.CircleImageView_border_color,
                DEFAULT_BORDER_COLOR);
    drawStyle = a.getInt(R.styleable.CircleImageView_draw_style, Xfermode);
    Log.d("CircleImageView", "drawStyle-->"+drawStyle);
    a.recycle();
    mBorderPaint.setAntiAlias(true);
    mBorderPaint.setColor(mBorderColor);
    mBorderPaint.setStyle(Paint.Style.STROKE);
    mBorderPaint.setStrokeWidth(mBorderWidth);
}

绘制圆形ImageView
    protected void onDraw(Canvas canvas) {
        Bitmap bitmap = getBitmapFromDrawable(getDrawable());
        if (bitmap == null)
            return;

        if (drawStyle == Xfermode)
            drawCircleBitmapByXfermode(canvas, bitmap);
        else
            drawCircleBitmapByBitmapShader(canvas, bitmap);
        // 绘边框
        if (mBorderWidth != 0) {
            canvas.drawCircle(width / 2, height / 2, r + mBorderWidth / 2 - 1,
                    mBorderPaint);
        }
    }
Xfermode的方式实现

clear应该是透明没东西才对
这里写图片描述

    private void drawCircleBitmapByXfermode(Canvas canvas, Bitmap bitmap) {
        Log.d("CircleImageView", "drawCircleBitmapByXfermode");
        Paint cachePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        int sv = canvas.saveLayer(0, 0, width, height, null, Canvas.ALL_SAVE_FLAG);
        canvas.drawCircle(width / 2, height / 2, r, cachePaint);
        cachePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        Matrix matrix = getImageMatrix(bitmap);
        if (matrix != null)
            canvas.concat(matrix);
        canvas.drawBitmap(bitmap, 0, 0, cachePaint);
        canvas.restoreToCount(sv);
    }
BitmapShader的方式实现
    private void drawCircleBitmapByBitmapShader(Canvas canvas, Bitmap bitmap) {
        Log.d("CircleImageView", "drawCircleBitmapByBitmapShader");
        Paint paint = new Paint();
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Matrix matrix = getImageMatrix(bitmap);
        if (matrix != null)
            bitmapShader.setLocalMatrix(matrix);
        paint.setShader(bitmapShader);
        canvas.drawCircle(width / 2, height / 2, r, paint);
    }

效果

这里写图片描述


项目代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值