安卓案例-霓虹灯效果

本例子中布局使用的是 帧布局 。帧布局容器为每个加入其中的组件穿件一个空白的区域(称为一帧)。每个子组件占据一帧,这些帧都会根据 gravity 属性执行自动对齐。

下面示范了帧布局的用法,可以看到6个TextView 叠加在一起,上面的TextView遮住下面的 TextView。

效果图如下

image

布局文件代码如下

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="pub.weber.bym.framelayout.MainActivity">

    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="320dp"
        android:height="320dp"
        android:background="#f00"/>
    <TextView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="270dp"
        android:height="270dp"
        android:background="#0f0"/>
    <TextView
        android:id="@+id/view3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="220dp"
        android:height="220dp"
        android:background="#00f"/>
    <TextView
        android:id="@+id/view4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="170dp"
        android:height="170dp"
        android:background="#ff0"/>
    <TextView
        android:id="@+id/view5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="120dp"
        android:height="120dp"
        android:background="#f0f"/>
    <TextView
        android:id="@+id/view6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="70dp"
        android:height="70dp"
        android:background="#0ff"/>
</FrameLayout>

color 文件配置

<?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">#ff4040</color>
    <color name="color2">#fff240</color>
    <color name="color3">#43ff40</color>
    <color name="color4">#4053ff</color>
    <color name="color5">#f540ff</color>
    <color name="color6">#40fff5</color>
</resources>

MainActivity 代码如下

public class MainActivity extends AppCompatActivity {
    private int currentColor = 0;
    final int [] colors = new int[]{
            R.color.color1,
            R.color.color2,
            R.color.color3,
            R.color.color4,
            R.color.color5,
            R.color.color6
    };
    final int[] names = new int[]{
            R.id.view1,
            R.id.view2,
            R.id.view3,
            R.id.view4,
            R.id.view5,
            R.id.view6
    };
    TextView [] mViews = new TextView[names.length];
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            // 表明消息来自本程序所发送的
            if (msg.what == 0x123){
                for (int i=0;i<names.length;i++){
                    mViews[i].setBackgroundResource(colors[(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++){
            mViews[i] = (TextView) findViewById(names[i]);
        }
        // 定义一个线程周期性地改变 currentColor 变量值
        new Timer().schedule(new TimerTask(){
            @Override
            public void run(){
                // 发送一条空消息通知系统改变 6 个TextView 组件的背景色
                mHandler.sendEmptyMessage(0x123);
            }
        },0,200);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TPC-ZK霓虹灯控制系统实验是一种基于Zigbee无线通信技术的霓虹灯控制系统的实验。该实验旨在通过无线通信方式实现对霓虹灯的远程控制和管理。 该实验首先会搭建一个基于Zigbee无线通信的霓虹灯控制系统。系统中包括了一个主控节点和多个从节点,主控节点负责发送控制指令,而从节点则负责接收指令并控制相应的霓虹灯。 实验中,我们可以通过PC电脑或者手机等终端设备连接到主控节点,通过用户界面选择相应的控制模式和参数。主控节点会将这些指令通过无线通信发送给从节点。从节点收到指令后,根据指令的内容控制相应的霓虹灯进行亮灭、变色等操作。主控节点还可以接收从节点返回的状态信息,如灯的亮度、颜色等,从而实现对灯光状态的监控。 TPC-ZK霓虹灯控制系统实验具有以下特点:首先,采用无线通信技术,实现了对霓虹灯的远程控制,方便快捷。其次,通过主控节点可以方便地对多个从节点进行控制,实现了对多个灯的统一管理。此外,主控节点能够实时接收从节点的状态信息,可以对灯光的亮度、色彩等进行实时监控和调整。 通过TPC-ZK霓虹灯控制系统实验,我们可以深入了解无线通信技术在霓虹灯控制领域的应用,并通过实际操作体验到其便捷性和灵活性。这对于相关领域的研究和应用具有重要的参考价值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值