FrameLayout-- Neon

Inside the main function, a new thread, the control to change the background color

package com.example.framelayout

import android.support.v7.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)
    private var handler:Handler = MyHandler(WeakReference(the this )) 
    the override the onCreate Fun (savedInstanceState: the Bundle ? ) {
         Super .onCreate (savedInstanceState) 
        the setContentView (R.layout.activity_main) 

        for (I in names.indices) 
        { 
            views [I] = the findViewById (names [I]) 
        } 
        / / definition of a thread, the cycle changes the background color textView 
        the Timer () Schedule. (Object: the TimerTask () 
        { 
            the override Fun RUN () 
            { 
                // send a notification message system to change the background color 
                handler.sendEmptyMessage (0x123 ) 
            } 
        }, 0,200 ) 
    }
    class MyHandler(private val activity: WeakReference<MainActivity>):Handler()
    {
        private var cur =0
        //定义一个颜色数组
        internal var 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+cur)%colors.size])


                }
                cur++
            }
            super.handleMessage(msg)
        }
    }
}
View Code

This interface code, use the layout FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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=".MainActivity">
    <TextView
            android:id="@+id/view01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="320dp"
            android:height="320dp"
            android:background="@color/color1"
    />
    <TextView
            android:id="@+id/view02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="280dp"
            android:height="280dp"
            android:background="@color/color2"
    />
    <TextView
            android:id="@+id/view03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="240dp"
            android:height="240dp"
            android:background="@color/color3"
    />
    <TextView
            android:id="@+id/view04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="200dp"
            android:height="200dp"
            android:background="@color/color4"
    />
    <TextView
            android:id="@+id/view05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="160dp"
            android:height="160dp"
            android:background="@color/color5"
    />
    <TextView
            android:id="@+id/view06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="120dp"
            android:height="120dp"
            android:background="@color/color5"
    />
</FrameLayout>
View Code

 

Guess you like

Origin www.cnblogs.com/superxuezhazha/p/11422161.html