FrameLayout(层布局或者帧布局)的使用

FrameLayout(层布局或者帧布局)类似于photoshop的图层,这里做了一个例子以供以后参考:
闪烁的霓虹灯:
这里写图片描述

这里写图片描述

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="240dp"
        android:layout_height="240dp"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center" />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:contentDescription="@string/imgInfo"
        android:src="@drawable/ic_launcher" />

</FrameLayout>

MainActivity.java

package com.example.framelayouttest;

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

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends Activity {
    private int[] textIds = new int[] { R.id.textView1, R.id.textView2,
            R.id.textView3, R.id.textView3, R.id.textView4, R.id.textView5 };// 定义数组存放布局里的textView的id
    private int[] colorIds = new int[] { Color.LTGRAY, Color.GRAY, Color.GREEN,
            Color.WHITE, Color.YELLOW };// 定义颜色数组
    private TextView[] textView = new TextView[textIds.length];// 定义一个数组,数组元素由TextView组成,长度由textId决定
    private Handler handler;
    private int current = 0;// 当前cong 哪个颜色开始,这里从第一个开始

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        setContentView(R.layout.activity_main);
        for (int i = 0; i < textIds.length; i++) {
            textView[i] = (TextView) findViewById(textIds[i]);// 绑定
        }
        // 创建Handler用于接受和发送消息
        handler = new Handler() {
            // 重写此方法来处理消息
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x11) {// 判断消息是否为指定的消息
                    // 循环设置textView颜色
                    for (int i = 0; i < textView.length; i++) {
                        textView[i].setBackgroundColor(colorIds[(i + current)
                                % colorIds.length]);
                    }
                    current = (current + 1) % colorIds.length;
                }
            }
        };
        // 创建定时器对象
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                handler.sendEmptyMessage(0x11);
            }
        }, 0, 3000);// 没3秒发送一次消息

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值