android的帧布局,七、Android帧布局FrameLayout和霓虹灯效果
幀布局容器為每個(gè)加入其中的組件創(chuàng)建一個(gè)空白的區(qū)域(稱(chēng)為一幀),所有每個(gè)子組件占據(jù)一幀,這些幀都會(huì)根據(jù)gravity屬性執(zhí)行自動(dòng)對(duì)齊。
FrameLayout的常用XML屬性和相關(guān)方法
XML屬性
相關(guān)方法
說(shuō)??明
android:foreground
setForeground(Drawable)
設(shè)置該幀布局容器的前景圖像
android:foregroundGravity
setForegroundGravity(int)
定義繪制前景圖像的gravity屬性
下面的布局界面定義使用FrameLayout布局,并向該布局容器中添加了7個(gè)TextView,這7個(gè)TextView的高度完全相同,而寬度逐漸減少——這保證最先添加的TextView不會(huì)被完全遮擋;而且設(shè)置了7個(gè)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個(gè)TextView的背景色,就會(huì)看到上面的顏色漸變條不斷地變化,就像大街上的霓虹燈。
private int currentColor = 0;
//定義一個(gè)顏色數(shù)組
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);
}
};
//定義一個(gè)線程周期性地改變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);
}
總結(jié)
以上是生活随笔為你收集整理的android的帧布局,七、Android帧布局FrameLayout和霓虹灯效果的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jmeter监控服务资源
- 下一篇: JAVA开源商城系统