框架布局

1,布局(FrameLayout)

android:layout_gravity="center":设置居中
TextView:先设置的在最底层(宽高最大),后设置会覆盖前面的
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.hello.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:text="第一个"
        android:layout_gravity="center"
        android:background="@color/color1"
        android:layout_width="400dp"
        android:layout_height="400dp"
       />

    <TextView
        android:id="@+id/tv2"
        android:text="第一个"
        android:layout_gravity="center"
        android:background="@color/colorPrimary"
        android:layout_width="300dp"
        android:layout_height="300dp"
         />

    <TextView
        android:id="@+id/tv3"
        android:text="第一个"
        android:layout_gravity="center"
        android:background="@color/colorPrimaryDark"
        android:layout_width="200dp"
        android:layout_height="200dp"
         />

    <TextView
        android:id="@+id/tv4"
        android:text="第一个"
        android:background="@color/color1"
        android:layout_width="100dp"
        android:layout_gravity="center"
        android:layout_height="100dp"
     />


</FrameLayout>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color1">#f4f409</color>
    <color name="colorPrimary">#b53f4b</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

2.逻辑

package com.example.administrator.hello;


import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends AppCompatActivity {
    private int currentcolor=0;
    private int[] colors={R.color.color1,R.color.colorAccent,R.color.colorPrimary,R.color.colorPrimaryDark};
     // private int[] colors=new int[]{}
    private int[] names=new int[]{R.id.tv1,R.id.tv2,R.id.tv3,R.id.tv4};
    private TextView[] tvs=new TextView[names.length];
    Handler hander= new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0x123)
            {
               for(int i=0;i<names.length;i++)
                {
                    tvs[i].setBackgroundResource(colors[(i+currentcolor)%names.length]);
                    Log.d("tag", (i+currentcolor)%names.length+"");
                }
                currentcolor++;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<names.length;i++)
        {
            tvs[i]=(TextView) findViewById(names[i]);
        }
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                hander.sendEmptyMessage(0x123);
            }
        },0,300);
    }
}

 3,逻辑步骤

1,首先定义两个数组,存放color,textView(names存放TextView的id)

 private int[] colors={R.color.color1,R.color.colorAccent,R.color.colorPrimary,R.color.colorPrimaryDark};
 private int[] names=new int[]{R.id.tv1,R.id.tv2,R.id.tv3,R.id.tv4};
 private TextView[] tvs=new TextView[names.length];

在onCreated()里面初始化TextView

 for(int i=0;i<names.length;i++)
        {
            tvs[i]=(TextView) findViewById(names[i]);
        }

2,设置一个定时器Timer ,调用它的时刻表的方法,

第一个参数:时间任务

第二个参数:延迟时间

第三个参数:任务周期

 Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                hander.sendEmptyMessage(0x123);
            }
        },0,300);

3.在上面的run方法中发送信息sendEmptyMessage(0x123)

0x123:是自定义的一个标示

发送信息是用hander对象执行的

所以要在最上面定义一个Hander对象

 import android.os.Handler;

Handler hander= new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0x123)
            {
               for(int i=0;i<names.length;i++)
                {
                    tvs[i].setBackgroundResource(colors[(i+currentcolor)%names.length]);
                    Log.d("tag", (i+currentcolor)%names.length+"");
                }
                currentcolor++;
            }
            super.handleMessage(msg);
        }

4.在上述对象创建过程中需要重载 handleMessage(Message msg) 方法-->用于处理接受到的信息

参数:msg信息对象

msg.what用于获取信息的标示

为了保证每次颜色不同,设置为(i+currentcolor)%names.length

猜你喜欢

转载自www.cnblogs.com/excellencesy/p/9051441.html