Android超级简单的实现圆形,矩形和圆角

使用 View.setClipToOutline() 方法或 android:clipToOutline 属性将视图裁剪至其轮廓区域。由 Outline.canClip() 方法所决定,仅有矩形、圆形和圆角矩形轮廓支持裁剪。

如果要将视图裁剪至可绘制对象的形状,请将可绘制对象设置为视图背景(如上所示)并调用 View.setClipToOutline() 方法。

比如说布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:background="@color/colorPrimary"
        android:orientation="vertical"
        android:scaleType="center"
        android:src="@mipmap/timg"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

可以是其他View,如imageview

imageView.outlineProvider = object : ViewOutlineProvider() {
            override fun getOutline(view: View, outline: Outline) {
                //矩形
                outline.setRect(Rect(0,0,view.width,view.width))
                //圆形
                //outline.setOval(0, 0, view.width, view.width)
                //圆角
                //outline.setRoundRect(0, 0, view.width, view.height, view.width / 2f)
            }
        }
//将视图裁剪至其轮廓区域
imageView.clipToOutline = true

最近开发中发现google新增了ShapeableImageView,实现圆角更加简单了

<com.google.android.material.imageview.ShapeableImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ddd"
        android:layout_margin="@dimen/dp_10"
        app:shapeAppearanceOverlay="@style/cornerStyle" />

样式:

//圆角
 <style name="cornerStyle">
        <item name="cornerFamilyBottomLeft">rounded</item>
        <item name="cornerSizeBottomLeft">@dimen/dp_10</item>
        <item name="cornerFamilyBottomRight">rounded</item>
        <item name="cornerSizeBottomRight">@dimen/dp_10</item>
    </style>

//圆形
<style name="circleStyle">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">50%</item>
    </style>

最近发现Google官方推荐Android使用一个coil的图片加载库,这个库主要是使用kotlin的协程实现的

//图片加载库
implementation("io.coil-kt:coil:1.1.1")

实现方式

//圆角 左上 右上
imageView.load(R.mipmap.ic_ming) {
            placeholder(R.mipmap.ic_launcher)
            crossfade(true)
            transformations(RoundedCornersTransformation(20f,20f))
        }
//圆形
imageView.load(R.mipmap.ic_ming) {
            placeholder(R.mipmap.ic_launcher)
            crossfade(true)
            transformations(CircleCropTransformation())
        }

在开发中经常用到

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值