安卓imageview实现上面两个圆角下面两个直角的效果

来源:互联网 发布:linux cp文件夹 编辑:程序博客网 时间:2024/04/28 21:52

由于产品奇葩要求要求实现下面效果,奇葩的地方就在,要求图片上面两个是圆角,下面两个是直接。

自己研究了半天找出来了下面两种解决方案

一,用自定义imageview

import android.content.Context;import android.graphics.Canvas;import android.graphics.Path;import android.graphics.RectF;import android.util.AttributeSet;import android.widget.ImageView;/**用来显示不规则图片,* 上面两个是圆角,下面两个是直角* */public class OvalImageView extends ImageView {    /*圆角的半径,依次为左上角xy半径,右上角,右下角,左下角*/    private float[] rids = {10.0f, 10.0f, 10.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f,};    public OvalImageView(Context context) {        super(context);    }    public OvalImageView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public OvalImageView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    /**     * 画图     * by Hankkin at:2015-08-30 21:15:53     *     * @param canvas     */    protected void onDraw(Canvas canvas) {        Path path = new Path();        int w = this.getWidth();        int h = this.getHeight();          /*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/        path.addRoundRect(new RectF(0, 0, w, h), rids, Path.Direction.CW);        canvas.clipPath(path);        super.onDraw(canvas);    }}  

二,借助glide(glide是一个图片加载库,很强大,大家自己了解怎么使用,使用起来也很简单)
//上面两个圆角,下面两个直角        Glide.with(this)                .load(images2)                //                .bitmapTransform(new GrayscaleTransformation(this))//带灰色蒙层                .bitmapTransform(new RoundedCornersTransformation(this, 60, 0,                        RoundedCornersTransformation.CornerType.TOP))                .into(image3);//下面两个圆角,上面两个直角        Glide.with(this)                .load(images2)                //                .bitmapTransform(new GrayscaleTransformation(this))//带灰色蒙层                .bitmapTransform(new RoundedCornersTransformation(this, 60, 0,                        RoundedCornersTransformation.CornerType.BOTTOM))                .into(image3);注:glide可以实现圆角图片,右边两个圆角左边两个直角等奇葩得效果


0 2
原创粉丝点击