欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Kotlin02.frameLayout霓虹灯效果

程序员文章站 2022-06-22 08:02:18
MainActivity.ktpackage com.example.framelayoutimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.os.Handlerimport android.os.Messageimport android.widget.TextViewimport java.lang.ref.WeakReferenceimport java.ut...

MainActivity.kt

package com.example.framelayout

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.widget.TextView
import java.lang.ref.WeakReference
import java.util.*

class MainActivity : AppCompatActivity() {

    internal val names = intArrayOf(R.id.view01, R.id.view02, R.id.view03, R.id.view04, R.id.view05, R.id.view06)
    private var views = arrayOfNulls<TextView>(names.size)

    class MyHandler(private val activity : WeakReference<MainActivity>) : Handler(){
        private var currentColor = 0;
        internal val colors = intArrayOf(R.color.color1, R.color.color2, R.color.color3, R.color.color4, R.color.color5, R.color.color6)
        override fun handleMessage(msg: Message) {
            if (msg.what == 0x123) {
                for (i in activity.get()?.names?.indices!!) {
                    activity.get()?.views!![i]?.setBackgroundResource(
                            colors[(i + currentColor) % colors.size]
                    )
                }
                currentColor++
            }
            super.handleMessage(msg)
        }
    }
    private var handler : Handler = MyHandler(WeakReference(this));
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        for (i in names.indices) {
            views[i] = findViewById(names[i])
        }
        Timer().schedule(object : TimerTask(){
            override fun run() {
                handler.sendEmptyMessage(0x123)
            }
        },0, 200)
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- 依次定义6个TextView,先定义的TextView位于底层,
        后定义的TextView位于上层 -->
    <TextView
        android:id="@+id/view01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="160pt"
        android:height="160pt"
        android:background="#f00"/>
    <TextView
        android:id="@+id/view02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="140pt"
        android:height="140pt"
        android:background="#0f0"/>
    <TextView
        android:id="@+id/view03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="120pt"
        android:height="120pt"
        android:background="#00f"/>
    <TextView
        android:id="@+id/view04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="100pt"
        android:height="100pt"
        android:background="#ff0"/>
    <TextView
        android:id="@+id/view05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="80pt"
        android:height="80pt"
        android:background="#f0f"/>
    <TextView
        android:id="@+id/view06"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="60pt"
        android:height="60pt"
        android:background="#0ff"/>
</FrameLayout>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="color1">#f00</color>
    <color name="color2">#0f0</color>
    <color name="color3">#00f</color>
    <color name="color4">#ff0</color>
    <color name="color5">#f0f</color>
    <color name="color6">#0ff</color>
</resources>

效果图

Kotlin02.frameLayout霓虹灯效果

本文地址:https://blog.csdn.net/augfun/article/details/107881839

相关标签: Kotlin