在 kotlin 中从 GradientDrawable 获取颜色
GetColor from GradientDrawable in kotlin
我已经像这样以编程方式设置了一个 GradientDrawable 背景,并且正在尝试测试它的颜色。主要问题似乎是在测试时从形状 (GradientDrawable) 获取颜色。
这个片段来自一个更大的绑定适配器..
color = ContextCompat.getColor(
textView.context, R.color.ColorRatingHigh
)
val shape = GradientDrawable()
shape.shape = GradientDrawable.OVAL
shape.setColor(color)
shape.setStroke(2, Color.BLACK)
textView.setBackground(shape)
测试是这样设置的..
@Test
fun MovieDetailRatingColorTest() {
...
onView(withId(R.id.movie_vote_average))
.check(matches(withBackgroundColor(R.color.ColorRatingHigh)))
}
...
fun withBackgroundColor(expectedColor: Int): Matcher<View?>? {
Checks.checkNotNull(expectedColor)
return object : BoundedMatcher<View?, TextView>(TextView::class.java) {
override fun matchesSafely(textView: TextView): Boolean {
val actualColor = (textView.getBackground() as ColorDrawable).color
return expectedColor == actualColor
}
override fun describeTo(description: Description) {
description.appendText("with background color: ")
}
}
}
不幸的是,我收到以下 ClassCastException
android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ColorDrawable
我在网站上看到了几个 post 与此类似的问题,
但 none 似乎在工作并且大多数最终都会遇到相同的问题.. 例如
或有看起来过时或遭受 java 到 kotlin 转换的答案.. 例如 how to get the color from GradientDrawable
想出了一个解决方法,使用片段方案来访问 'ContextCompat'。
这使我能够检索 'R.color' 目录。
getColor 检索最初传入的 colorId 的 2 的补码...恰好与此处检索到的 id 相匹配:
val actualColor = (textView.getBackground() as ColorDrawable).color.defaultColor
lateinit var scenario: FragmentScenario<MovieDetailFragment>
...
@Test
fun MovieDetailRatingColorTest() {
var expectedColor: Int? = 0
scenario.onFragment { fragment ->
expectedColor =
fragment.context?.let {
ContextCompat.getColor(it, R.color.ColorRatingHigh )
}
}
onView(withId(R.id.movie_vote_average))
.check(matches(withBackgroundColor(expectedColor)))
}
然后我编辑了 withBackground() 函数以匹配新的输入
fun withBackgroundColor(expectedColor: Int?): Matcher<View?>? {
Checks.checkNotNull(expectedColor)
return object : BoundedMatcher<View?, TextView>(TextView::class.java) {
override fun matchesSafely(textView: TextView): Boolean {
val actualColor = (textView.getBackground() as GradientDrawable).color?.defaultColor
return expectedColor == actualColor
}
override fun describeTo(description: Description) {
description.appendText("with background color: $expectedColor")
}
}
}
我已经像这样以编程方式设置了一个 GradientDrawable 背景,并且正在尝试测试它的颜色。主要问题似乎是在测试时从形状 (GradientDrawable) 获取颜色。
这个片段来自一个更大的绑定适配器..
color = ContextCompat.getColor(
textView.context, R.color.ColorRatingHigh
)
val shape = GradientDrawable()
shape.shape = GradientDrawable.OVAL
shape.setColor(color)
shape.setStroke(2, Color.BLACK)
textView.setBackground(shape)
测试是这样设置的..
@Test
fun MovieDetailRatingColorTest() {
...
onView(withId(R.id.movie_vote_average))
.check(matches(withBackgroundColor(R.color.ColorRatingHigh)))
}
...
fun withBackgroundColor(expectedColor: Int): Matcher<View?>? {
Checks.checkNotNull(expectedColor)
return object : BoundedMatcher<View?, TextView>(TextView::class.java) {
override fun matchesSafely(textView: TextView): Boolean {
val actualColor = (textView.getBackground() as ColorDrawable).color
return expectedColor == actualColor
}
override fun describeTo(description: Description) {
description.appendText("with background color: ")
}
}
}
不幸的是,我收到以下 ClassCastException
android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ColorDrawable
我在网站上看到了几个 post 与此类似的问题,
但 none 似乎在工作并且大多数最终都会遇到相同的问题.. 例如
或有看起来过时或遭受 java 到 kotlin 转换的答案.. 例如 how to get the color from GradientDrawable
想出了一个解决方法,使用片段方案来访问 'ContextCompat'。
这使我能够检索 'R.color' 目录。
getColor 检索最初传入的 colorId 的 2 的补码...恰好与此处检索到的 id 相匹配:
val actualColor = (textView.getBackground() as ColorDrawable).color.defaultColor
lateinit var scenario: FragmentScenario<MovieDetailFragment>
...
@Test
fun MovieDetailRatingColorTest() {
var expectedColor: Int? = 0
scenario.onFragment { fragment ->
expectedColor =
fragment.context?.let {
ContextCompat.getColor(it, R.color.ColorRatingHigh )
}
}
onView(withId(R.id.movie_vote_average))
.check(matches(withBackgroundColor(expectedColor)))
}
然后我编辑了 withBackground() 函数以匹配新的输入
fun withBackgroundColor(expectedColor: Int?): Matcher<View?>? {
Checks.checkNotNull(expectedColor)
return object : BoundedMatcher<View?, TextView>(TextView::class.java) {
override fun matchesSafely(textView: TextView): Boolean {
val actualColor = (textView.getBackground() as GradientDrawable).color?.defaultColor
return expectedColor == actualColor
}
override fun describeTo(description: Description) {
description.appendText("with background color: $expectedColor")
}
}
}