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

来源:互联网 发布:放大电路 知乎 编辑:程序博客网 时间:2024/05/23 22:47

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

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

背景

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

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

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

return method void setCircular(boolean circular) : Sets the image shape to circular. void setCornerRadius(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 来实现的圆角。

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

  • Android BitmapShader 实战 实现圆形、圆角图片
  • 自定义控件其实很简单1/3

实现

先看效果图:

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:你可以通过下面的方式和我联系

  • 微博:cafeting
  • Github: likfe
  • CSDN:他叫自己Mr.张
2 0
原创粉丝点击