最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Kotlin使用TransitionDrawable實現(xiàn)顏色漸變效果流程講解

 更新時間:2023年02月16日 14:37:16   作者:破浪會有時  
這篇文章主要介紹了Kotlin使用TransitionDrawable實現(xiàn)顏色漸變效果,這里,我們通過TransitionDrawable顯示顏色漸變效果,包括背景顏色的變化,以及圖片與圖片的漸變效果

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.pngidea2.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顏色漸變效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Android Hilt的使用以及遇到的問題

    Android Hilt的使用以及遇到的問題

    這篇文章主要介紹了Android Hilt的使用以及遇到的問題,幫助大家更好的理解和學習使用Android開發(fā),感興趣的朋友可以了解下
    2021-04-04
  • Android MotionEvent中getX()和getRawX()的區(qū)別實例詳解

    Android MotionEvent中getX()和getRawX()的區(qū)別實例詳解

    這篇文章主要介紹了Android MotionEvent中getX()和getRawX()的區(qū)別實例詳解的相關資料,需要的朋友可以參考下
    2017-03-03
  • Android 使用地圖時的權限請求方法

    Android 使用地圖時的權限請求方法

    今天小編就為大家分享一篇Android 使用地圖時的權限請求方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Flutter桌面開發(fā)windows插件開發(fā)

    Flutter桌面開發(fā)windows插件開發(fā)

    這篇文章主要為大家介紹了Flutter桌面開發(fā)windows插件開發(fā)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Android仿微信圖片點擊全屏效果

    Android仿微信圖片點擊全屏效果

    這篇文章主要為大家詳細介紹了Android仿微信圖片點擊全屏效果的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • Android視圖綁定viewBinding的使用介紹

    Android視圖綁定viewBinding的使用介紹

    最近這段時間在學習Kotlin,突然發(fā)現(xiàn)谷歌已經(jīng)把kotlin-android-extensions插件廢棄,目前推薦使用ViewBinding來進行替代,接下來通過本文給大家分享Android使用ViewBinding的詳細步驟,感興趣的朋友一起學習吧
    2023-01-01
  • Flutter UI如何使用Provide實現(xiàn)主題切換詳解

    Flutter UI如何使用Provide實現(xiàn)主題切換詳解

    這篇文章主要給大家介紹了關于Flutter UI如何使用Provide實現(xiàn)主題切換的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04
  • Android自定義View之邊框文字、閃爍發(fā)光文字

    Android自定義View之邊框文字、閃爍發(fā)光文字

    這篇文章主要為大家詳細介紹了Android自定義View之邊框文字、閃爍發(fā)光文字,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • AndroidStudio圖片壓縮工具ImgCompressPlugin使用實例

    AndroidStudio圖片壓縮工具ImgCompressPlugin使用實例

    這篇文章主要為大家介紹了AndroidStudio圖片壓縮工具ImgCompressPlugin使用實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Android Studio進行APP圖標更改的兩種方式總結

    Android Studio進行APP圖標更改的兩種方式總結

    這篇文章主要介紹了Android Studio進行APP圖標更改的兩種方式總結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06

最新評論

庄浪县| 茶陵县| 保亭| 元谋县| 云霄县| 孟州市| 旬邑县| 嘉善县| 南城县| 衡山县| 南澳县| 喜德县| 观塘区| 祁连县| 伊宁县| 特克斯县| 阜宁县| 镇巴县| 三台县| 宁南县| 丹巴县| 梧州市| 临清市| 会泽县| 会昌县| 天长市| 车致| 定结县| 永济市| 囊谦县| 连州市| 朔州市| 仁怀市| 海安县| 文化| 康定县| 兰西县| 蓝田县| 响水县| 宝兴县| 来宾市|