关于Android的径向渐变高级编程的实现

发布时间:2019-06-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了关于Android的径向渐变高级编程的实现脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

原文地址:http://www.apkbus.com/blog-91...

在最近的一系列文章,对midipad APP,有一个关于一个radialgradiant渲染每个padview利用的探讨,对审美的原因,这是一个软件层,而不是一个硬件层。在这个简短的系列中,我们首先看看差异是什么,然后探索一种方法来调整硬件层。

在midipad的文章,我说我喜欢如何呈现的一个软件层的径向渐变,所以让我们开始比较同一径向出现时所采用的硬件和软件层。让我们先定义一个简单的自定义视图是viewpad中midipad文章大大简化版。

它使用相同的技–我们创建一个新的径向尺寸变化时的观点,但径向本身是在PadView使用的使用非常相似。shaderfactory的使用是一种机制,我们将使用后在不同的径向厂替代。
class GradientView @Jvmoverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0,
    PRivate val bounds: RectF = RectF()

) : View(context, attrs, defStyleAttr, defStyleRes) {

private val defaultColour: Int by lazyFast {
    context.theme.getColour(R.attr.colorAccent)
}

private val paint: Paint =
    Paint().apply {
        isAntiAlias = true
        style = Paint.Style.FILL
    }

VAR shaderFactory: (width: Float, height: Float) -> Shader = { viewWidth, viewHeight ->
    RadialGradient(
            viewWidth / 2f,
            viewHeight / 2f,
            Math.min(viewWidth, viewHeight) / 2f,
            defaultColour,
            Color.TRANSPARENT,
            Shader.TileMode.CLAMP
    )
}

override fun onSizeChanged(newWidth: Int, newHeight: Int, oldWidth: Int, oldHeight: Int) =
        super.onSizeChanged(newWidth, newHeight, oldWidth, oldHeight).run {
            adjustBounds(newWidth.toFloat(), newHeight.toFloat())
        }

private fun adjustBounds(width: Float, height: Float) {
    bounds.set(0f, 0f, width, height)
    paint.shader = shaderFactory(width, height)
}

override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    canvas?.drawRect(bounds, paint)
}

}

我们现在可以创建一个布局,其中包含4个视图,其中两个在一个黑暗的背景下,两个在一个光的背景下。相同背景的每一对都由一个使用硬件层渲染,一个使用软件层渲染:
<?XMl version="1.0" encoding="utf-8"?>
    <andROId.support.constraint.ConstraintLayout >"http://schemas.android.COM/apk/res/android"
  >"http://schemas.android.com/apk/res-auto"
  >"http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.stylingandroid.radialgradient.MainActivity">

  <View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@android:color/black"
    app:layout_constraintBottom_toBottomOf="@+id/dark_hardware"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

  <com.stylingandroid.radialgradient.GradientView
    android:id="@+id/dark_hardware"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="8dp"
    android:layerType="hardware"
    app:layout_constraintBottom_toTopOf="@+id/light_hardware"
    app:layout_constraintEnd_toStartOf="@+id/dark_Software"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

  <com.stylingandroid.radialgradient.GradientView
    android:id="@+id/dark_software"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="8dp"
    android:layerType="software"
    app:layout_constraintBottom_toTopOf="@+id/light_software"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/dark_hardware"
    app:layout_constraintTop_toTopOf="parent" />

  <com.stylingandroid.radialgradient.GradientView
    android:id="@+id/light_hardware"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="8dp"
    android:layerType="hardware"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/light_software"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/dark_hardware" />

  <com.stylingandroid.radialgradient.GradientView
    android:id="@+id/light_software"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="8dp"
    android:layerType="software"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/light_hardware"
    app:layout_constraintTop_toBottomOf="@+id/dark_software" />

</android.support.constraint.ConstraintLayout>
如果我们现在在黑暗背景下比较这两个,我们可以看到它们的出现有很大的不同:

@H_733_406@关于Android的径向渐变高级编程的实现

使用软件层呈现的是右边,而这一个看起来更适合我想要达到的目标。它褪得稍微快一点,看起来更漂亮。但是,如果我选择使用一个光的背景,我肯定我会选择使用硬件层渲染的那个:

关于Android的径向渐变高级编程的实现

再一次,使用软件层呈现的是右边的一个,现在有一个暗晕,看起来不太好。

为了理解为什么会出现这种差异,让我们先退一步,了解一下硬件和软件层实际上做了什么。

脚本宝典总结

以上是脚本宝典为你收集整理的关于Android的径向渐变高级编程的实现全部内容,希望文章能够帮你解决关于Android的径向渐变高级编程的实现所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。