Android开发6:布局管理器3(帧布局FrameLayout-案例霓虹灯效果用到的知识点)

来源:互联网 发布:web portal认证软件 编辑:程序博客网 时间:2024/05/17 04:08
版本:Android4.3 API18  学习整理:liuxinming 

概念

  FramLayout直接继承了ViewGroup组件

API参考地址:http://android.toolib.net/reference/android/widget/FrameLayout.

理解:帧布局容器为每个加入其中的组件创建一个空白的区域(称为一帧),每个子组件占据一帧,这些帧会根据gravity属性执行自动对齐。

FrameLayout常用XML属性及方法

XML属性相关方法说明android:foregroundsetForeground(Drawable)设置该帧布局容器的前景图像android:foregroundGravitysetForegroundGravity(int)定义绘制前景图像的gravity属性
FrameLayout包含的子元素也受FrameLayout.LayoutParams控制,因此它所包含的子元素也可以指定android:layout_gravity 属性

XML布局代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <!-- 依次定义6个TextView,先定义的TextView位于底层 -->    <TextView android:id="@+id/text01"        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/text02"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="280dp"        android:height="280dp"        android:background="#0f0"        />    <TextView android:id="@+id/text03"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="240dp"        android:height="240dp"        android:background="#00f"        />    <TextView android:id="@+id/text04"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="200dp"        android:height="200dp"        android:background="#ff0"        />    <TextView android:id="@+id/text05"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="160dp"        android:height="160dp"        android:background="#f0f"        />    <TextView android:id="@+id/text06"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="120dp"        android:height="120dp"        android:background="#00f"        /></FrameLayout>

我们简单来理解下上面代码干的事情
我们使用了FrameLayout布局,并向该布局容器中添加了6个TextView,这6个TextView高度完全相同,而宽度则逐渐减少
这样可以保证最先添加的TextView不会被完全遮挡
而且我们设置了6个不同颜色

调试效果图



霓虹灯效果

package com.example.framelayout;import java.util.Timer;import java.util.TimerTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.graphics.Color;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {private int currentColor = 0;//定义一个颜色数组final int[] colors = new int[] {Color.MAGENTA,Color.BLUE,Color.GREEN,Color.GRAY,Color.YELLOW,Color.RED};//颜色显示组件,view为TextView控件final int[] names = new int[] {R.id.text01,R.id.text02,R.id.text03,R.id.text04,R.id.text05,R.id.text06};/** * 在Activity中创建TextView控件数组 */    TextView[] views = new TextView[names.length];    /**     * Handler 主要接受子线程发送的数据, 并用此数据配合主线程更新UI     */    Handler handler = new Handler(){    @Override    public void handleMessage(Message msg){    // 消息来自本程序所发送    if(msg.what == 0x123){    //循环设置TextView背景颜色    for (int i=0;i<names.length;i++){    views[i].setBackgroundColor(colors[(i+currentColor)%names.length]);    }        currentColor++;    }    super.handleMessage(msg);    }        };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);for (int i=0;i<names.length;i++){//通过findViewById()来获取布局中的SlidingDrawer对象//返回一个View对象views[i] = (TextView) findViewById(names[i]);}//定义一个线程周期性地改变currentColor变量值new Timer().schedule(new TimerTask(){public void run(){//发送一条空消息通知系统改变6个TextView组件的背景色handler.sendEmptyMessage(0x123);}},0,200);//200毫秒执行一次}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

结果:

上面Activity实现了每隔200毫秒更新6个TextView的背景颜色

用到的知识点

1、Handler():主要受子线程发送的数据,并用此数据配合主线程更新UI

      解释: 当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发, 比如说, 你要是点击一个 Button, Android会分发事件到Button上,来响应你的操作。  如果此时需要一个耗时的操作,例如: 联网读取数据,或者读取本地较大的一个文件的时候,你不能把这些操作放在主线程中,如果你放在主线程中的话,界面会出现假死现象, 如果5秒钟还没有完成的话,会收到Android系统的一个错误提示  "强制关闭".  这个时候我们需要把这些耗时的操作,放在一个子线程中,因为子线程涉及到UI更新,Android主线程是线程不安全的,也就是说,更新UI只能在主线程中更新,子线程中操作是危险的. 这个时候,Handler就出现了来解决这个复杂的问题,由于Handler运行在主线程中(UI线程中),它与子线程可以通过Message对象来传递数据,这个时候,Handler就承担着接受子线程传过来的(子线程用sedMessage()方法传弟)Message对象,(里面包含数据)  , 把这些消息放入主线程队列中,配合主线程进行更新UI。

2、Timer()计时器

      ①创建Timer()定时器:timer = new Timer()
      ②创建一个TimerTask,实现了Runnable接口,所以TimerTask就是一个子线程
     ==========定义计划任务,根据参数的不同可以完成一下种类的工作=========
     ① .schedule(TimerTask task, Date when) 在固定时间执行某任务
     ② .schedule(TimerTask task, Date when, long period) 在固定时间开始重复执行某任务,重复时间间隔可控
     ③ .schedule(TimerTask task, long delay) 在延迟多久后执行某任务
     ④ .schedule(TimerTask task, long delay, long period) 在延迟多久后重复执行某任务,重复时间间隔可控
    ==========华丽分割线============================
    timer.schedule(new TimerTask(),3000,1000); //3秒后开始倒计时,倒计时间隔为1秒

调试效果




欢迎Android , php 同学加群 QQ :224784042 交流
学习的结果,依靠于每天的持之以恒!!不是特忙,特加班情况,会每天更新一篇Android学习笔记,欢迎大家互相交流,学习,共同进步。
偶菜鸟一枚!!!!!!
晚安!