欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现图片设置圆角形式

程序员文章站 2022-06-23 12:43:21
本文实例为大家分享了android实现图片设置圆角形式的具体代码,供大家参考,具体内容如下1.自定义的图片圆角形式circleimageview类public class circleimagevie...

本文实例为大家分享了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>

如下图:

Android实现图片设置圆角形式

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);
    }
}

运行后结果:

 Android实现图片设置圆角形式

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。