Kotlin使用TransitionDrawable實現(xiàn)顏色漸變效果流程講解
1 導入需要漸變的圖片
如果需要實現(xiàn)圖片之間的漸變效果,我們需要兩張照片,這樣才能實現(xiàn)照片1到照片2的漸變。在路徑 /res/values/ 下,我們新建一個 arrays.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="icons">
<item>@drawable/idea1</item>
<item>@drawable/idea2</item>
</array>
</resources>
這個文件包含了兩個 item:@drawable/idea1 以及 @drawable/idea2,把它們寫在一個 array 里面。這里,我們導入的兩張圖片的名字分別是 idea1.png 和 idea2.png,存放于 res/drawable/ 路徑下。


從上面兩張照片我們可以看到,我們希望通過 TransitionDrawable 呈現(xiàn)出燈泡的開關效果。
2 activity_main.xml
這里例子涉及到的前端由三部分組成,一個 TextView,一個 ImageView,以及一個 Switch,當我們點擊了 Switch 按鈕,圖片的燈光就可以實現(xiàn)亮暗之間的變化,以及字體背景的漸變。
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="案例2:燈泡顏色漸變"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pushButton" />
<ImageView
android:id="@+id/iv_light"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/idea"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="0.218" />
<Switch
android:id="@+id/switchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="20dp"
android:showText="true"
android:textOff="關"
android:textOn="開"
app:layout_constraintTop_toBottomOf="@+id/iv_light"
tools:ignore="UseSwitchCompatOrMaterialXml" />3 MainActivity.kt
@SuppressLint("ClickableViewAccessibility", "ResourceType")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val resources: Resources = resources
val icons: TypedArray = resources.obtainTypedArray(R.array.icons)
val drawable = icons.getDrawable(0) // ending image
val drawableTwo = icons.getDrawable(1) // starting image
val iconDrawables = arrayOf(drawable,drawableTwo)
var transitionDrawableIcon = TransitionDrawable(iconDrawables);
val colorDrawables = arrayOf(ColorDrawable(Color.RED),ColorDrawable(Color.GREEN) )
var transitionDrawable = TransitionDrawable(colorDrawables)
switchView.setOnCheckedChangeListener { buttonView, isChecked ->
iv_light.setImageDrawable(transitionDrawableIcon)
transitionDrawableIcon.reverseTransition(
2000
)
transitionDrawable.isCrossFadeEnabled = false
val transitionDrawableTextView = TransitionDrawable(colorDrawables)
textView2.background = transitionDrawableTextView
transitionDrawableTextView.startTransition(1000)
}
}
我們先導入這兩張圖片,然后這個array作為輸入給到 TransitionDrawable 函數(shù):var transitionDrawableIcon = TransitionDrawable(iconDrawables);。對于文字背景也是一個道理,我們需要把需要漸變的顏色先放到一個array里面:val colorDrawables = arrayOf(ColorDrawable(Color.RED),ColorDrawable(Color.GREEN) ),然后再作為輸入給到 TransitionDrawable 函數(shù):var transitionDrawable = TransitionDrawable(colorDrawables)。當我們點擊 Switch 按鈕后,燈泡會變亮(實際上就是兩張燈泡之間的顏色漸變),字體背景也會從紅色變化到綠色。
到此這篇關于Kotlin使用TransitionDrawable實現(xiàn)顏色漸變效果流程講解的文章就介紹到這了,更多相關Kotlin顏色漸變效果內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android MotionEvent中getX()和getRawX()的區(qū)別實例詳解
這篇文章主要介紹了Android MotionEvent中getX()和getRawX()的區(qū)別實例詳解的相關資料,需要的朋友可以參考下2017-03-03
Flutter桌面開發(fā)windows插件開發(fā)
這篇文章主要為大家介紹了Flutter桌面開發(fā)windows插件開發(fā)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Flutter UI如何使用Provide實現(xiàn)主題切換詳解
這篇文章主要給大家介紹了關于Flutter UI如何使用Provide實現(xiàn)主題切換的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04
Android自定義View之邊框文字、閃爍發(fā)光文字
這篇文章主要為大家詳細介紹了Android自定義View之邊框文字、閃爍發(fā)光文字,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
AndroidStudio圖片壓縮工具ImgCompressPlugin使用實例
這篇文章主要為大家介紹了AndroidStudio圖片壓縮工具ImgCompressPlugin使用實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
Android Studio進行APP圖標更改的兩種方式總結
這篇文章主要介紹了Android Studio進行APP圖標更改的兩種方式總結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06

