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,图片,圆角
0
投稿
猜你喜欢
浅谈Java如何实现一个基于LRU时间复杂度为O(1)的缓存
2022-02-02 08:35:36
关于访问后端接口报404错误问题的解决方法(全网最细!)
2022-02-13 23:18:40
Java快速排序QuickSort(实例)
2021-12-22 21:47:42
使用C# Winform应用程序获取网页源文件的解决方法
2023-02-01 10:56:28
一文搞懂Android RecyclerView点击展开、折叠效果的实现代码
2023-02-17 14:52:24
Android SurfaceView拍照录像实现方法
2022-06-18 17:07:20
Android studio中生成引用.aar和.jar的方法详解
2023-09-29 07:48:33
C#创建安全的字典(Dictionary)存储结构
2023-03-17 22:11:51
Kotlin 基础教程之数组容器
2021-09-09 17:22:40
SpringBoot 集成 activiti的示例代码
2023-01-22 10:22:03
C#使用windows服务开启应用程序的方法
2022-08-14 04:37:14
C#实现XSL转换的方法
2023-02-02 03:36:41
Android快速实现无预览拍照功能
2023-12-23 12:23:38
Android中Intent传递对象的两种方法Serializable,Parcelable
2021-05-24 03:11:58
详解App保活实现原理
2022-01-20 17:59:10
Java 数据结构与算法系列精讲之队列
2023-09-26 21:10:31
使用IntelliJ IDEA搭建SSM框架的图文教程
2022-06-14 00:56:19
C#接口(Interface)用法分析
2022-10-18 10:59:17
java类中生成jfreechart,返回图表的url地址 代码分享
2023-09-08 00:54:07
Java获取时间年、月、日的方法
2022-02-10 16:58:36