Android 必知必会-使用 supportV4 的 RoundedBitmapDrawable 实现圆角
来源:互联网 发布:放大电路 知乎 编辑:程序博客网 时间:2024/05/23 22:47
如果移动端访问不佳,请访问 –> Github版
RoundedBitmapDrawable
是 supportV4
下的一个类,有了它,显示圆角和圆形图片的情况下就不需要额外的第三方类库了,还能和各种图片加载库配合使用。
背景
今天无意间看到一段实现圆形头像的代码:
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);drawable.setCircular(true);
RoundedBitmapDrawable
这个类顿时就引起我的好奇了,一查发现是 android.support.v4.graphics.drawable.RoundedBitmapDrawable
,点击此处 可以看到官方的介绍。这个类中的两个方法是今天的主角:
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
实现
先看效果图:
其中第一个图片是圆形,第二个和第三个图片设置了自定义大小的圆角。
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
- Android 必知必会-使用 supportV4 的 RoundedBitmapDrawable 实现圆角
- Android 必知必会-使用 supportV4 的 RoundedBitmapDrawable 实现圆角
- Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案
- Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案
- Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案
- Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案
- android RoundedBitmapDrawable最简单方式实现圆角图片(一)
- ImageView的圆角或圆形使用原生support v4中的RoundedBitmapDrawable实现
- 使用RoundedBitmapDrawable快速生成圆角和圆形图片
- 使用RoundedBitmapDrawable快速生成圆角和圆形图片
- Android Studio怎么关联supportV4的源码?
- Android Studio怎么关联supportV4的源码
- 能否修改android supportv4包的源码?
- [Material Design]使用RoundedBitmapDrawable快速生成圆角和圆形图片
- [Material Design]使用RoundedBitmapDrawable快速生成圆角和圆形图片
- 使用Android Supportv4 SwipeRefreshLayout有关ListView滑动冲突问题
- Android SupportV4 LruCache源码分析
- 如何关联supportv4的源代码
- BZOJ 2588 Spoj 10628. Count on a tree
- 实现photoshop 正常混合的shader
- Unity5中新的Shader体系简析
- Qt movie图片发布时动画效果不显示问题
- JDK里的设计模式
- Android 必知必会-使用 supportV4 的 RoundedBitmapDrawable 实现圆角
- 欢迎进入阿钟的博客导航——快速查找所有博客
- fedora安装codeblocks 亲测可用
- 从零开始学习SLAM
- 汉诺塔问题递归算法分析
- Learning Attention for Online Advertising with Recurrent Neural Network论文思路整理
- Configure the Visual Studio Tools for Apache Cordova
- Xcode 7.3.1 的framework的制作
- snake_贪吃蛇.java