在Android API 28上,使用Kotlin,我试图让SupportMapFragment圆形,这样我就可以让它周围有动画,然而,当我试图将clipToOutline变成True时,它仍然是一个正方形.我怎么才能把它画成一个圆呢?谢谢!

// Preview of Screenshot

enter image description here

// XML

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/customerMapView"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_marginTop="16dp"
    android:background="@drawable/circle_mask"
    android:clipToOutline="true"
    android:contentDescription="@string/customer_map_view"
    app:layout_constraintEnd_toEndOf="@id/pulsatingCircle"
    app:layout_constraintStart_toStartOf="@id/pulsatingCircle"
    app:layout_constraintTop_toTopOf="@id/pulsatingCircle" />

// circle_mask

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/transparent" />
    <item>
        <shape android:shape="oval">
            <size
                android:width="300dp"
                android:height="300dp" />
        </shape>
    </item>
</layer-list>

// Kotlin Code

private fun initMap() {
    val mapView = supportFragmentManager.findFragmentById(R.id.customerMapView) as SupportMapFragment

    mapView.view?.outlineProvider = ViewOutlineProvider.BACKGROUND
    mapView.view?.clipToOutline = true
}

我try 在XML和Kotlin文件中将clipToOutline设置为True,但在XML中,我需要使用API32.

推荐答案

您可以使用CardView来执行此操作.

因为你有固定的宽度和高度,你可以通过一个特定的角半径,使它看起来像一个圆,你就完成了!

<com.google.android.material.card.MaterialCardView
        android:id="@+id/mapCardLayout"
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:cardBackgroundColor="@android:color/transparent"
        app:cardCornerRadius="100dp"
        app:cardElevation="0dp">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/customerMapView"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.google.android.material.card.MaterialCardView>

If you don't have a specific width or height, then you can go with the custom view with the same width height for proper circle like this,

import android.content.Context
import android.util.AttributeSet
import com.google.android.material.card.MaterialCardView

class CircleCardView : MaterialCardView {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec)
    }
}

For circle appearance, make a custom style attribute and use it with the cardview like this,

style.xml名中,

<style name="CustomCardCornerStyle" parent="@style/Widget.MaterialComponents.CardView">
        <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay_card_corners</item>
    </style>
    
    <style name="ShapeAppearanceOverlay_card_corners" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">50%</item>
    </style>

在你layout.xml岁的时候,

<com.demo.motiondemo.CircleCardView
        android:id="@+id/mapCardLayout"
        style="@style/CustomCardCornerStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@android:color/transparent"
        app:cardElevation="0dp">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/customerMapView"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.demo.motiondemo.CircleCardView>

Android相关问答推荐

如何使禁用状态下的material 3按钮与启用状态下的 colored颜色 相同?

使用Kotlin的SD卡

Android 14无法删除已配置的文件

如何在Jetpack composeH中创建具有弯曲末端的六边形形状

strings.xml中字符串数组中的占位符

原始mp3文件不显示与proguard

如何用帆布在喷气背包中画一个圆环?

可以';t将数据插入房间数据库

仅当先前输入为 yes 时,Android 才会要求下一个输入

Andorid Studio编译器如何自动为变量editText生成mutableStateOf("")的方法名?

如何在 Android 的 ViewModel 中使用 LiveData

在移动设备上看到时如何增加 PasswordField 文本?

在 Kotlin 中打开新片段时如何对当前片段应用更改?

Android活动系统导航栏 colored颜色 ?

如何将文本组合放在行中,一个具有可变宽度的组合

这是 let 函数的正确用法吗?

Jetpack 组合和片段

基线配置文件 x R8/Proguard

自定义布局忽略可组合的大小

在 Android Studio 中保存数据