首先帧布局大家都知道是一个层层叠加在一起的,所以。我们弄7个textview,最外层的比里层的宽度少点,高度一样,比如宽度从210,180,150.。一直到30,最下层都留一点出来。
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="210dp"
android:height="60dp"
android:background="#ff0000"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180dp"
android:height="60dp"
android:background="#dd0000"/>
。。。后边的5个省略了。背景颜色可以不用设置,我们后边设置。
现在是代码部分,动态修改背景颜色。。
public class MainActivity extends Activity {
int currentcolor=0;
int[] colors={Color.BLACK,Color.BLUE,Color.RED,Color.GREEN,Color.YELLOW,Color.GRAY,Color.CYAN};
int[] names={R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView4,R.id.textView5,R.id.textView6,R.id.textView7,};
TextView[] tv=new TextView[7];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<7;i++){
tv[i]=(TextView) findViewById(names[i]);
}
final Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==0x123){
//举例:如果当前currentcolor为3,那么说明颜色整体后移了3 // 位,下标为0的组件颜色应该为颜色数组里
//0+currentcolor的颜色对吧。如果不理解可以自己画图移动一次看看结果就明白了
for(int i=0;i<7-currentcolor;i++){
tv[i].setBackgroundColor(colors[i+currentcolor]);
}
for(int i=7-currentcolor,j=0;i<7;i++,j++){
tv[i].setBackgroundColor(colors[j]);
}
}
super.handleMessage(msg);
}
};
new Timer().schedule(new TimerTask() {
@Override
public void run() {
currentcolor++;
if(currentcolor>=6){
currentcolor=0;
}
//不能在这里改变组件的颜色,是因为View和UI组件不是线程安全的
//所以开发者不能自己启动一个线程来修改UI组件
Message msg=new Message();
msg.what=0x123;
handler.sendMessage(msg);
}
}, 0, 100);
}
学的快,忘的更快,所以只好抽空就复习下学过的知识。还是得多写代码,多打几遍就记住了,要不听懂很快就忘了。