android activity 渐变,关于Android的径向渐变高级编程的实现
在最近的一系列文章,對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個視圖,其中兩個在一個黑暗的背景下,兩個在一個光的背景下。相同背景的每一對都由一個使用硬件層渲染,一個使用軟件層渲染:
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"
tools:context="com.stylingandroid.radialgradient.MainActivity">
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" />
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" />
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" />
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" />
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 activity 渐变,关于Android的径向渐变高级编程的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android入门知识,android基
- 下一篇: android 屏幕禁止,Android