Android 实现对ImageView的边角进行任意剪裁(包含圆角和切去直角)

1.canvas方法的使用

canvas提供了clipPath,clipRect和clipRegion等方法对控件进行剪裁,通过这些方法的组合可以实现任何形状的控件,当然前提是数学够好的情况下。在本文中使用的是clipPath方法对imageView进行剪裁实现圆角和切去直角两种效果的展示。

2.圆角

2.1实现

自定义控件RoundImageVIew继承ImageView,重写onlayout函数和ondraw函数,具体实现如下所示。根据path路径的不同,可剪裁一个或者多个边角,然后angle的大小即圆的半径。Path路径其实可以看作是一个个点连成的图形,以imageview的左上角为圆点,然后依次设置不同的点,点连接起来形成的图形就是ImageView剪裁后的形状。

    int width;
    int height;
    int angle = 50;
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        path.moveTo(angle, 0);
        path.lineTo(width - angle, 0);
        path.quadTo(width, 0, width, angle);//第一个角
        path.lineTo(width, height - angle);
        path.quadTo(width, height, width - angle, height);//2
        path.lineTo(angle, height);
        path.quadTo(0, height, 0, height - angle);//3
        path.lineTo(0, angle);
        path.quadTo(0, 0, angle, 0);//4
        canvas.clipPath(path);
        super.onDraw(canvas);
    }

2.2效果图

3.切去直角

3.1代码

    int width;
    int height;
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }
    int length = 50;
    @Override
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        path.moveTo(length, 0);
        path.lineTo(width - length, 0);
        path.lineTo(width, length);
        path.lineTo(width,height-length);
        path.lineTo(width-length,height);
        path.lineTo(length,height);
        path.lineTo(0,height-length);
        path.lineTo(0,length);
        path.lineTo(length,0);
        canvas.clipPath(path);
        super.onDraw(canvas);

    }

可根据具体需求来设置path路径来实现剪裁不同的图形。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android ImageView 圆角可以通过以下两种方式实现: 1. 使用 XML 属性设置圆角ImageView 的 XML 布局文件中,可以使用以下属性设置圆角: ``` android:background="@drawable/your_image" android:scaleType="centerCrop" android:clipToOutline="true" android:outlineProvider="background" ``` 其中,`your_image` 是你要显示的图片资源。`scaleType` 属性设置图片的缩放方式,`clipToOutline` 属性设置是否裁剪视图的轮廓,`outlineProvider` 属性设置视图的轮廓提供者,这里使用 `background` 表示使用视图的背景作为轮廓。 然后,在 `res/drawable` 目录下创建一个 XML 文件,命名为 `your_image.xml`,内容如下: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="10dp" /> </shape> ``` 其中,`radius` 属性设置圆角的半径大小。 2. 使用代码设置圆角 在 Java 代码中,可以使用以下方法设置圆角: ``` ImageView imageView = findViewById(R.id.image_view); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_image); RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap); roundedBitmapDrawable.setCornerRadius(10); imageView.setImageDrawable(roundedBitmapDrawable); ``` 其中,`your_image` 是你要显示的图片资源。`RoundedBitmapDrawableFactory.create()` 方法创建一个圆角位图,`setCornerRadius()` 方法设置圆角的半径大小,`setImageDrawable()` 方法设置 ImageView 的显示内容为圆角位图。 以上两种方法都可以实现 ImageView 圆角的效果,具体使用哪种方式取决于你的需求和习惯。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值