美文网首页工作生活
七、Android帧布局FrameLayout和霓虹灯效果

七、Android帧布局FrameLayout和霓虹灯效果

作者: 清梦星河哈 | 来源:发表于2019-07-01 14:38 被阅读0次

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

FrameLayout的常用XML属性和相关方法

XML属性 相关方法 说  明
android:foreground setForeground(Drawable) 设置该帧布局容器的前景图像
android:foregroundGravity setForegroundGravity(int) 定义绘制前景图像的gravity属性

下面的布局界面定义使用FrameLayout布局,并向该布局容器中添加了7个TextView,这7个TextView的高度完全相同,而宽度逐渐减少——这保证最先添加的TextView不会被完全遮挡;而且设置了7个TextView的背景色渐变。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/View01"
        android:layout_width="210dp"
        android:layout_height="200dp"
        android:background="#ff0000"/>
    <TextView
        android:id="@+id/View02"
        android:layout_width="180dp"
        android:layout_height="200dp"
        android:background="#dd0000"/>
    <TextView
        android:id="@+id/View03"
        android:layout_width="150dp"
        android:layout_height="200dp"
        android:background="#bb0000"/>
    <TextView
        android:id="@+id/View04"
        android:layout_width="120dp"
        android:layout_height="200dp"
        android:background="#990000"/>
    <TextView
        android:id="@+id/View05"
        android:layout_width="90dp"
        android:layout_height="200dp"
        android:background="#770000"/>
    <TextView
        android:id="@+id/View06"
        android:layout_width="60dp"
        android:layout_height="200dp"
        android:background="#550000"/>
    <TextView
        android:id="@+id/View07"
        android:layout_width="30dp"
        android:layout_height="200dp"
        android:background="#330000"/>
</FrameLayout>

效果如图:


颜色渐变.jpg
霓虹灯效果

如果考虑轮换上面帧布局中的7个TextView的背景色,就会看到上面的颜色渐变条不断地变化,就像大街上的霓虹灯。

    private int currentColor = 0;
    //定义一个颜色数组
    final int[] colors = new int[]{
            R.color.color7,
            R.color.color6,
            R.color.color5,
            R.color.color4,
            R.color.color3,
            R.color.color2,
            R.color.color1
    };

    final int[] names = new int[]{
            R.id.View01,
            R.id.View02,
            R.id.View03,
            R.id.View04,
            R.id.View05,
            R.id.View06,
            R.id.View07,
    };

    TextView[] views = new TextView[7];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity3);
        for (int i = 0; i < 7; i++) {
            views[i] = findViewById(names[i]);
        }
        @SuppressLint("HandlerLeak")
        final Handler handler = new Handler() {

            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 0x1122) {
                    for (int i = 0; i < 7 - currentColor; i++) {
                        views[i].setBackgroundResource(colors[i + currentColor]);
                    }
                    for (int i = 7 - currentColor, j = 0; i < 7; i++, j++) {
                        views[i].setBackgroundResource(colors[j]);
                    }
                }
                super.handleMessage(msg);
            }
        };
        //定义一个线程周期性地改变currentColor变量值
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                currentColor++;
                if (currentColor >= 6) {
                    currentColor = 0;
                }
                Message m = new Message();
                m.what = 0x1122;
                handler.sendMessage(m);
            }
        }, 0, 500);
    }

相关文章

网友评论

    本文标题:七、Android帧布局FrameLayout和霓虹灯效果

    本文链接:https://www.haomeiwen.com/subject/eyhgcctx.html