Android实现图片设置圆角形式
作者:彬sir哥 时间:2023-07-29 16:37:32
本文实例为大家分享了Android实现图片设置圆角形式的具体代码,供大家参考,具体内容如下
1.自定义的图片圆角形式CircleImageView类
public class CircleImageView extends ImageView {
private static final Xfermode MASK_XFERMODE;
private Bitmap mask;
private Paint paint;
private int mBorderWidth = 10;
private int mBorderColor = Color.parseColor("#f2f2f2");
private boolean useDefaultStyle = false;
static {
PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN;
MASK_XFERMODE = new PorterDuffXfermode(localMode);
}
public CircleImageView(Context context) {
super(context);
}
public CircleImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircularImage);
mBorderColor = a.getColor(R.styleable.CircularImage_border_color, mBorderColor);
final int def = (int) (2 * context.getResources().getDisplayMetrics().density + 0.5f);
mBorderWidth = a.getDimensionPixelOffset(R.styleable.CircularImage_border_width, def);
a.recycle();
}
private void useDefaultStyle(boolean useDefaultStyle) {
this.useDefaultStyle = useDefaultStyle;
}
@Override
protected void onDraw(Canvas canvas) {
if (useDefaultStyle) {
super.onDraw(canvas);
return;
}
final Drawable localDraw = getDrawable();
if (localDraw == null) {
return;
}
if (localDraw instanceof NinePatchDrawable) {
return;
}
if (this.paint == null) {
final Paint localPaint = new Paint();
localPaint.setFilterBitmap(false);
localPaint.setAntiAlias(true);
localPaint.setXfermode(MASK_XFERMODE);
this.paint = localPaint;
}
final int width = getWidth();
final int height = getHeight();
/** 保存layer */
int layer = canvas.saveLayer(0.0F, 0.0F, width, height, null, 31);
/** 设置drawable的大小 */
localDraw.setBounds(0, 0, width, height);
/** 将drawable绑定到bitmap(this.mask)上面(drawable只能通过bitmap显示出来) */
localDraw.draw(canvas);
if ((this.mask == null) || (this.mask.isRecycled())) {
this.mask = createOvalBitmap(width, height);
}
/** 将bitmap画到canvas上面 */
canvas.drawBitmap(this.mask, 0.0F, 0.0F, this.paint);
/** 将画布复制到layer上 */
canvas.restoreToCount(layer);
drawBorder(canvas, width, height);
}
/**
* 绘制圆形边框
*/
private void drawBorder(Canvas canvas, final int width, final int height) {
if (mBorderWidth == 0) {
return;
}
final Paint mBorderPaint = new Paint();
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setAntiAlias(true);
mBorderPaint.setColor(mBorderColor);
mBorderPaint.setStrokeWidth(mBorderWidth);
canvas.drawCircle(width / 2, height / 2, (width - mBorderWidth) / 2, mBorderPaint);
canvas = null;
}
public Bitmap createOvalBitmap(final int width, final int height) {
Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;
Bitmap localBitmap = Bitmap.createBitmap(width, height, localConfig);
Canvas localCanvas = new Canvas(localBitmap);
Paint localPaint = new Paint();
final int padding = (mBorderWidth - 3) > 0 ? mBorderWidth - 3 : 1;
/**
* 设置椭圆的大小(因为椭圆的最外边会和border的最外边重合的,如果图片最外边的颜色很深,有看出有棱边的效果,所以为了让体验更加好,
* 让其缩进padding px)
*/
RectF localRectF = new RectF(padding, padding, width - padding, height - padding);
localCanvas.drawOval(localRectF, localPaint);
return localBitmap;
}
}
1.1 在values目录下创建一个circle_attr.xml,文件内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircularImage">
<attr name="border_width" format="dimension" />
<attr name="border_color" format="color" />
</declare-styleable>
</resources>
如下图:
2.activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_sp" />
<com.demo.test.bitmap.CircleImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="50dp"
android:src="@drawable/ic_sp" />
</LinearLayout>
</LinearLayout>
3.MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
运行后结果:
来源:https://blog.csdn.net/qq_35091074/article/details/121517918
标签:Android,图片,圆角
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android自定义环形对比图效果
2023-06-15 19:49:19
![](https://img.aspxhome.com/file/2023/4/82404_0s.jpg)
Android RecyclerView使用ListAdapter高效刷新数据的操作方法
2023-06-24 22:22:09
![](https://img.aspxhome.com/file/2023/0/83550_0s.gif)
一文梳理Java 8后的新功能
2023-07-13 07:41:43
基于C语言实现静态通讯录的示例代码
2023-07-02 22:07:38
![](https://img.aspxhome.com/file/2023/2/103102_0s.gif)
Unity实现场景漫游相机
2023-06-15 19:08:57
Flutter WillPopScope拦截返回事件原理示例详解
2023-07-19 12:53:35
浅谈c++性能测试工具之计算时间复杂度
2023-07-09 12:40:05
![](https://img.aspxhome.com/file/2023/4/79524_0s.jpg)
Android编程基于自定义View实现绚丽的圆形进度条功能示例
2023-07-24 17:13:26
![](https://img.aspxhome.com/file/2023/7/84737_0s.png)
Android ViewPager实现图片轮翻效果
2023-08-01 00:30:57
基于C语言实现井字棋游戏
2023-06-28 21:23:18
Android异常 java.lang.IllegalStateException解决方法
2023-07-28 10:26:36
Android使用URL读取网络资源的方法
2023-07-27 10:56:39
![](https://img.aspxhome.com/file/2023/6/63836_0s.png)
java多线程通过CompletableFuture组装异步计算单元
2023-07-19 10:15:42
![](https://img.aspxhome.com/file/2023/0/57620_0s.jpg)
Android 如何获取手机总内存和可用内存等信息
2023-07-27 13:11:42
六款值得推荐的android(安卓)开源框架简介
2023-06-24 01:46:54
java 实现MD5加密算法的简单实例
2023-07-19 21:53:56
C#超市收银系统设计
2023-06-21 00:00:09
![](https://img.aspxhome.com/file/2023/9/66579_0s.png)
Android SharedPreferences实现记住密码和自动登录界面
2023-06-15 20:07:00
![](https://img.aspxhome.com/file/2023/9/83529_0s.png)
彻底掌握C语言strcpy函数的用法
2023-07-03 07:21:18
![](https://img.aspxhome.com/file/2023/4/103104_0s.png)
一文带你了解C#中抽象方法与虚方法的区别
2023-07-23 00:14:32