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

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

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

一,用自定义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可以实现圆角图片,右边两个圆角左边两个直角等奇葩得效果


  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值