Android开发之用帧布局和handler实现霓虹灯效果
2014-03-20 21:26
453 查看
在帧布局中放上若干TextView,利用handler,可以实现霓虹灯效果。
页面布局文件如下:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/View01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="210px"
android:width="50px"
android:background="#ff0000"
/>
<TextView
android:id="@+id/View02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="180px"
android:width="50px"
android:background="#dd0000"
/>
<TextView
android:id="@+id/View03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="150px"
android:width="50px"
android:background="#bb0000"
/>
<TextView
android:id="@+id/View04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="120px"
android:width="50px"
android:background="#990000"
/>
<TextView
android:id="@+id/View05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="90px"
android:width="50px"
android:background="#770000"
/>
</FrameLayout>
colors.xml如下:
<?xml
version="1.0"
encoding="utf-8"?>
<resources>
<color
name="color1">#ff0000</color>
<color
name="color2">#dd0000</color>
<color
name="color3">#bb0000</color>
<color
name="color4">#990000</color>
<color
name="color5">#770000</color>
</resources>
mainactivity.java如下:
public class MainActivity extends Activity {
private int currentColor = 0;
//定义一个颜色数组
final int[] colors = new int[]
{
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
};
final int[] names = new int[]
{
R.id.View01,
R.id.View02,
R.id.View03,
R.id.View04,
R.id.View05
};
TextView[] views = new TextView[7];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i = 0 ; i < 5 ; i++)
{
views[i] = (TextView)findViewById(names[i]);
}
final Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
//表明消息来自本程序锁发送
if(msg.what == 0x1122)
{
//依次改变5个TextView的背景色
for(int i = 0 ; i < 5 - currentColor ; i++)
{
views[i].setBackgroundResource(colors[i+currentColor]);
}
for(int i = 5 - currentColor , j = 0 ; i < 5 ; i++ , j++)
{
views[i].setBackgroundResource(colors[j]);
}
}
super.handleMessage(msg);
}
};
//定义一个线程周期性地改变currentColor变量值
new Timer().schedule(new TimerTask()
{
public void run()
{
currentColor++;
if(currentColor >= 4)
{
currentColor = 0;
}
//发送一条消息通知系统改变5个TextView组件的背景色
Message m = new Message();
m.what = 0x1122;
handler.sendMessage(m);
}
} , 0 , 100);
}
@Override
public 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;
}
}
享受霓虹灯效果吧!
页面布局文件如下:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/View01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="210px"
android:width="50px"
android:background="#ff0000"
/>
<TextView
android:id="@+id/View02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="180px"
android:width="50px"
android:background="#dd0000"
/>
<TextView
android:id="@+id/View03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="150px"
android:width="50px"
android:background="#bb0000"
/>
<TextView
android:id="@+id/View04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="120px"
android:width="50px"
android:background="#990000"
/>
<TextView
android:id="@+id/View05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="90px"
android:width="50px"
android:background="#770000"
/>
</FrameLayout>
colors.xml如下:
<?xml
version="1.0"
encoding="utf-8"?>
<resources>
<color
name="color1">#ff0000</color>
<color
name="color2">#dd0000</color>
<color
name="color3">#bb0000</color>
<color
name="color4">#990000</color>
<color
name="color5">#770000</color>
</resources>
mainactivity.java如下:
public class MainActivity extends Activity {
private int currentColor = 0;
//定义一个颜色数组
final int[] colors = new int[]
{
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
};
final int[] names = new int[]
{
R.id.View01,
R.id.View02,
R.id.View03,
R.id.View04,
R.id.View05
};
TextView[] views = new TextView[7];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i = 0 ; i < 5 ; i++)
{
views[i] = (TextView)findViewById(names[i]);
}
final Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
//表明消息来自本程序锁发送
if(msg.what == 0x1122)
{
//依次改变5个TextView的背景色
for(int i = 0 ; i < 5 - currentColor ; i++)
{
views[i].setBackgroundResource(colors[i+currentColor]);
}
for(int i = 5 - currentColor , j = 0 ; i < 5 ; i++ , j++)
{
views[i].setBackgroundResource(colors[j]);
}
}
super.handleMessage(msg);
}
};
//定义一个线程周期性地改变currentColor变量值
new Timer().schedule(new TimerTask()
{
public void run()
{
currentColor++;
if(currentColor >= 4)
{
currentColor = 0;
}
//发送一条消息通知系统改变5个TextView组件的背景色
Message m = new Message();
m.what = 0x1122;
handler.sendMessage(m);
}
} , 0 , 100);
}
@Override
public 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;
}
}
享受霓虹灯效果吧!
相关文章推荐
- 【Android】使用FrameLayout布局实现霓虹灯效果
- Android:帧布局实现霓虹灯的效果
- Android开发6:布局管理器3(帧布局FrameLayout-案例霓虹灯效果用到的知识点)
- 【Android】使用FrameLayout布局实现霓虹灯效果
- Android开发——多线程使用Handler实现读诗效果
- Android笔记(2)---帧布局实现霓虹灯效果
- android布局之帧布局-----霓虹灯效果实现
- 几步实现Android 开发中的弹窗效果
- android开发游记:RecycleView 实现复杂首页布局三种方式
- Android商城开发系列(五)—— 商城首页回到顶部和搜索框布局实现
- android开发步步为营之104:文字加阴影效果和描边效果的实现
- 【Android UI设计与开发】第12期:顶部标题栏(三)ActionBar实现层级导航的返回效果
- Android自定义ViewGroup自动换行实现滑动任意布局及事件处理效果
- android 开发零起步学习笔记(十三):Android 实现多页界面左右滑动切换效果
- android新特性:使用RecyclerViewHeader一键添加RecyclerView头布局并实现轮播效果
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Android 布局手势缩放,可滑动查看,实现类似Imgview图片缩放效果
- Android相对布局实现各种梅花效果
- Android 三种布局实现上下回弹效果(普通布局,ListView,ScrollView)
- Android开发学习之卡片式布局的简单实现