Android 必知必会-使用 supportV4 的 RoundedBitmapDrawable 实现圆角

如果移动端访问不佳,请访问 –> Github版

RoundedBitmapDrawablesupportV4 下的一个类,有了它,显示圆角和圆形图片的情况下就不需要额外的第三方类库了,还能和各种图片加载库配合使用。

背景

今天无意间看到一段实现圆形头像的代码:

RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
drawable.setCircular(true);

RoundedBitmapDrawable 这个类顿时就引起我的好奇了,一查发现是 android.support.v4.graphics.drawable.RoundedBitmapDrawable点击此处 可以看到官方的介绍。这个类中的两个方法是今天的主角:

returnmethod
voidsetCircular(boolean circular) : Sets the image shape to circular.
voidsetCornerRadius(float cornerRadius) : Sets the corner radius to be applied when drawing the bitmap.

setCircular(boolean circular) : 把图片的形状设为圆形;

setCornerRadius(float cornerRadius) : 设置图片的圆角半径。

这里贴一下源码,更能清晰的知道它的实现:

    /**
     * Sets the image shape to circular.
     * <p>This overwrites any calls made to {@link #setCornerRadius(float)} so far.</p>
     */
    public void setCircular(boolean circular) {
        mIsCircular = circular;
        mApplyGravity = true;
        if (circular) {
            updateCircularCornerRadius();
            mPaint.setShader(mBitmapShader);
            invalidateSelf();
        } else {
            setCornerRadius(0);
        }
    }

    private void updateCircularCornerRadius() {
        final int minCircularSize = Math.min(mBitmapHeight, mBitmapWidth);
        mCornerRadius = minCircularSize / 2;
    }

    /**
     * Sets the corner radius to be applied when drawing the bitmap.
     */
    public void setCornerRadius(float cornerRadius) {
        if (mCornerRadius == cornerRadius) return;

        mIsCircular = false;
        if (isGreaterThanZero(cornerRadius)) {
            mPaint.setShader(mBitmapShader);
        } else {
            mPaint.setShader(null);
        }

        mCornerRadius = cornerRadius;
        invalidateSelf();
    }

至于具体的实现,阅读源码发现官方使用了 BitmapShader 来实现的圆角。

如果你有兴趣研究,更多资料:

实现

先看效果图:

2016-08-25_08104B752A742F5F71AE7F16661CBA2A.jpg

其中第一个图片是圆形,第二个和第三个图片设置了自定义大小的圆角。

xxActivity

private void init() {
        RoundedBitmapDrawable drawableA = RoundedBitmapDrawableFactory.create(getResources(), id2Bitmap(this, R.drawable.icon_avatar));
        drawableA.setCircular(true);
        rbA.setImageDrawable(drawableA);

        RoundedBitmapDrawable drawableB = RoundedBitmapDrawableFactory.create(getResources(), id2Bitmap(this, R.drawable.icon_avatar));
        drawableB.setCornerRadius(30L);
        rbB.setImageDrawable(drawableB);

        RoundedBitmapDrawable drawableC = RoundedBitmapDrawableFactory.create(getResources(), id2Bitmap(this, R.drawable.icon_avatar));
        drawableC.setCornerRadius(60L);
        rbC.setImageDrawable(drawableC);
    }

    public static Bitmap id2Bitmap(Context context, int id) {
        return BitmapFactory.decodeResource(context.getResources(), id);
    }

activity_xx.xml

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

    <ImageView
        android:id="@+id/rb_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <ImageView
        android:id="@+id/rb_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        />

    <ImageView
        android:id="@+id/rb_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        />

</LinearLayout>

代码比较简单,有什么问题欢迎随时和我联系。

总结

一个小小的发现,让我又少使用了一个第三方库,心情大好。另外在此过程中发现的 BitmapShader 是学习自定义 View 的一个不错的切入点,需要再研究研究。

PS:你可以通过下面的方式和我联系

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值