Android布局--每0.2秒变换颜色的霓虹灯

FrameLayout是帧布局,与其他布局不同的是,帧布局会为每个组件创造一个空白的区域,可以一帧一帧地展示组件(或每帧重叠的方式),而不会被遮挡,更适合用来做卡包之类的效果。

今天照着Android疯狂讲义里利用FrameLayout做了一个闪烁的霓虹灯效果。
静态效果如下:
这里写图片描述
代码如下,如果将xml文件里的FrameLayout改为LinearLayout,则不会将其叠在一起,而是线性排布在一个布局里
这里写图片描述

<?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="com.example.luoge.uisample2.MainActivity">

    <TextView
        android:id="@+id/View0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="320px"
        android:height="320px"
        android:background="@android:color/holo_blue_bright" />

    <TextView
        android:id="@+id/View2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="280px"
        android:height="280px"
        android:background="@android:color/holo_blue_light" />

    <TextView
        android:id="@+id/View1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="240px"
        android:height="240px"
        android:background="@android:color/holo_blue_dark" />

    <TextView
        android:id="@+id/View3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="200px"
        android:height="200px"
        android:background="@android:color/holo_green_light"
         />

    <TextView
        android:id="@+id/View4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="160px"
        android:height="160px"
        android:background="@android:color/holo_orange_light"
         />

    <TextView
        android:id="@+id/View5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="120px"
        android:height="120px"
        android:background="@android:color/holo_red_light" />




</FrameLayout>

MainActivity文件,设置一个Timer,周期性的通知hander变换布局颜色。通过Handler机制发送消息进行变换,为什么不直接在Timer中的run方法里进行变换呢?

Android 疯狂讲义中给出的解释是,因为Android中的View和UI组件不是线程安全的(假设有两个线程都对其进行修改,那么就会有冲突,所以不是线程安全),所以要设置Handler对其进行处理。

MainActivity文件如下:

package com.example.luoge.uisample2;

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

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

public class MainActivity extends AppCompatActivity {

    private int currentColor = 0 ;

    final int[] color = new int[]{
            R.color.color1 ,
            R.color.color2 ,
            R.color.color3 ,
            R.color.color4 ,
            R.color.color5 ,
            R.color.color6 ,
    };

    final  int[] view = new int[]{
            R.id.View0 ,
            R.id.View1 ,
            R.id.View2 ,
            R.id.View3 ,
            R.id.View4 ,
            R.id.View5 ,
    };

    TextView[] textview = new TextView[view.length];

    Handler handler=new Handler()
    {   //利用数组变换颜色
        @Override
        public void handleMessage(Message msg)
        {
            if(msg.what == 0x101)
            {
                for(int i=0;i<view.length;i++)
                {
                    textview[i].setBackgroundResource( color[(i+currentColor ) % view.length]);
                }
                currentColor++;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化TextView
        for(int i=0;i<view.length;i++)
        {
            textview[i] = (TextView)findViewById(view[i]);
        }
        //设置Timer进行触发
        new Timer().schedule(new TimerTask()
                {
                    @Override
                    public void run()
                    {
                        handler.sendEmptyMessage(0x101);
                    }

                },0,300);

    }


}

图中的颜色序号需要在资源文件配置,即在res/values/color.xml进行配置。
res/values/color.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">@android:color/holo_blue_bright</color>
    <color name="color2">@android:color/holo_blue_light</color>
    <color name="color3">@android:color/holo_blue_dark</color>
    <color name="color4">@android:color/holo_green_light</color>
    <color name="color5">@android:color/holo_orange_light</color>
    <color name="color6">@android:color/holo_red_light</color>

谢谢大家!如有不足请指出,疑问请留言。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值