欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

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

发布时间:2023/12/10 67 豆豆
生活随笔 收集整理的这篇文章主要介绍了 android的帧布局,七、Android帧布局FrameLayout和霓虹灯效果 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

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

XML属性

相关方法

说  明

android:foreground

setForeground(Drawable)

设置该帧布局容器的前景图像

android:foregroundGravity

setForegroundGravity(int)

定义绘制前景图像的gravity属性

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/View01"

android:layout_width="210dp"

android:layout_height="200dp"

android:background="#ff0000"/>

android:id="@+id/View02"

android:layout_width="180dp"

android:layout_height="200dp"

android:background="#dd0000"/>

android:id="@+id/View03"

android:layout_width="150dp"

android:layout_height="200dp"

android:background="#bb0000"/>

android:id="@+id/View04"

android:layout_width="120dp"

android:layout_height="200dp"

android:background="#990000"/>

android:id="@+id/View05"

android:layout_width="90dp"

android:layout_height="200dp"

android:background="#770000"/>

android:id="@+id/View06"

android:layout_width="60dp"

android:layout_height="200dp"

android:background="#550000"/>

android:id="@+id/View07"

android:layout_width="30dp"

android:layout_height="200dp"

android:background="#330000"/>

效果如图:

颜色渐变.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的帧布局,七、Android帧布局FrameLayout和霓虹灯效果的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。