Kotlin:旋转ImageVector

huangapple go评论80阅读模式
英文:

Kotlin: Rotate ImageVector

问题

我正在将material icon存储在一个数据类中,像这样:

import androidx.compose.ui.graphics.vector.ImageVector

data class Item(
    val icon: ImageVector
)

val item = Item(Icons.Filled.Send)

然后将该项传递给一个可组合函数,其中使用VectorPainter绘制它。

如何将ImageVector旋转90度?理想情况下,这将生成一个仍然可以存储在数据类中的ImageVector。

英文:

I'm storing a material icon in a data class like so:

import androidx.compose.ui.graphics.vector.ImageVector

data class Item(
    val icon: ImageVector
)

val item = Item(Icons.Filled.Send)

The item is later passed to a composable where it's drawn using a VectorPainter

How do I rotate the ImageVector 90 degrees? Ideally this would result in an ImageVector that I can still store in the data class.

答案1

得分: 2

你可以使用 rotate 修饰符:

类似这样:

Icon(
    Icons.Filled.ArrowDropDown,
    null,
    Modifier.rotate(90f)
)

你可以添加条件以实现旋转和非旋转的图标。

类似这样:

@Composable
fun TrailingIcon(expanded: Boolean) {
    Icon(
        Icons.Filled.ArrowDropDown,
        null,
        Modifier.rotate(if (expanded) 90f else 0f)
    )
}
英文:

You can use the rotate Modifier:

Something like:

Icon(
    Icons.Filled.ArrowDropDown,
    null,
    Modifier.rotate(90f)
)

Kotlin:旋转ImageVector

You can add a condition to achieve both rotated and non-rotated icons.

Something like:

@Composable
fun TrailingIcon(expanded: Boolean) {
    Icon(
        Icons.Filled.ArrowDropDown,
        null,
        Modifier.rotate(if (expanded) 90f else 0f)
    )
}

答案2

得分: 1

根据评论,您的Composable没有modifier参数,这不是一个好的设计。即使您不立即使用它,每个Composable都应该始终具有modifier参数。

链接:https://chrisbanes.me/posts/always-provide-a-modifier/

在过去的一年左右,我看到了许多Composable看起来很棒,但它们有一个致命缺陷:它们在签名中没有暴露modifier参数。

如果您不想阅读整篇文章,这篇博客文章的TL;DR是:

您编写的任何发出布局的Composable(即使是一个简单的Box)都应该有一个modifier:Modifier参数,然后在布局中使用它。

即使Composable没有modifier参数,您也可以首先向Item数据类添加rotation参数:

data class Item(
    val icon: ImageVector,
    val rotation: Float
)

然后,将不具有Modifier参数的Composable包装在以下方式中:

Box(modifier = Modifier.rotate(item.rotation) {
  // 在这里添加您的Composable
}

imageVector.root.rotationrotation作为不可更改的参数公开,您无法更改它,也无法公开IconVector的Path,如果这是原因,可以通过矩阵进行旋转。

public val Icons.Filled.Send: ImageVector
    get() {
        if (_send != null) {
            return _send!!
        }
        _send = materialIcon(name = "Filled.Send") {
            materialPath {
                moveTo(2.01f, 21.0f)
                lineTo(23.0f, 12.0f)
                lineTo(2.01f, 3.0f)
                lineTo(2.0f, 10.0f)
                lineToRelative(15.0f, 2.0f)
                lineToRelative(-15.0f, 2.0f)
                close()
            }
        }
        return _send!!
    }

private var _send: ImageVector? = null
英文:

As i get from comments your Composable does not have a modifier param which is not a good design. Every composable even if you won't use it right away, should always have a modifier param.

https://chrisbanes.me/posts/always-provide-a-modifier/

> Over the past year or so, I’ve seen lots of composables which look
> great but they have one fatal flaw: they don’t expose a modifier:
> Modifier parameter in their signature.
>
> If you don’t want to read the whole post, the TL;DR of this blog post
> is:
>
> Any composable you write which emits layout (even a simple Box),
> should have a modifier: Modifier parameter, which is then used in the
> layout.

Even if the Composable doesn't have a modifier param you can first add rotation param to Item data class

data class Item(
    val icon: ImageVector,
    val rotation: Float
)

Then wrap your Composable that don't have Modifier param with

Box(modifier = Modifier.rotate(item.rotation) {
  // Your Composable here
}

imageVector.root.rotation exposes rotation as immutable param which you cannot change nor exposes Path of IconVector, if that was the cause it would be possible to rotate via matrix.

public val Icons.Filled.Send: ImageVector
    get() {
        if (_send != null) {
            return _send!!
        }
        _send = materialIcon(name = "Filled.Send") {
            materialPath {
                moveTo(2.01f, 21.0f)
                lineTo(23.0f, 12.0f)
                lineTo(2.01f, 3.0f)
                lineTo(2.0f, 10.0f)
                lineToRelative(15.0f, 2.0f)
                lineToRelative(-15.0f, 2.0f)
                close()
            }
        }
        return _send!!
    }

private var _send: ImageVector? = null

答案3

得分: -2

你可以使用“ImageVector”类提供的“rotate”方法。例如:

data class Item(
    val icon: ImageVector
)
val item = Item(Icons.Filled.Send.rotate(90f))
英文:

You can use the “rotate” method provided by the “ImageVector” class. E.g:

data class Item(
    val icon: ImageVector
)
val item = Item(Icons.Filled.Send.rotate(90f))

huangapple
  • 本文由 发表于 2023年6月8日 22:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定