android脚步---使用framelayout实现霓虹灯效果

轮换帧布局中7个TextView的背景颜色,会出现上面颜色渐变不断变换。

首先在main.xml文件中进行布局

总体布局为framelayout 中间有7个Textview,代表7种不同的颜色,可以看到高度相同,宽度逐渐减少,则最新添加的textvIEW不会被完全遮挡,设置了颜色渐变

 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
 <!-- 依次定义7个TextView,先定义的TextView位于底层 
     后定义的TextView位于上层 --> 
 <TextView android:id="@+id/View01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:width="210px" 
     android:height="50px" 
     android:background="#ff0000" 
     /> 
 <TextView android:id="@+id/View02" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:width="180px" 
     android:height="50px" 
     android:background="#dd0000"     
     /> 
 <TextView android:id="@+id/View03" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:width="150px" 
     android:height="50px" 
     android:background="#bb0000"     
     /> 
 <TextView android:id="@+id/View04" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:width="120px" 
     android:height="50px" 
     android:background="#990000"     
     /> 
 <TextView android:id="@+id/View05" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:width="90px" 
     android:height="50px" 
     android:background="#770000"     
     /> 
 <TextView android:id="@+id/View06" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:width="60px" 
     android:height="50px" 
     android:background="#550000"     
     /> 
 <TextView android:id="@+id/View07" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:width="30px" 
     android:height="50px" 
     android:background="#330000"     
     />        
 </FrameLayout> 

 在mainactivity里定义

package com.example.framlayout;

 import java.util.Timer; 
 import java.util.TimerTask; 
  
 import android.app.Activity; 
 import android.os.Bundle; 
 import android.os.Handler; 
 import android.os.Message; 
 import android.widget.TextView; 
  
 public class MainActivity extends Activity 
 { 
     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,  
     }; 
     //颜色显示数组,view为TextView控件  
     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 
     public void onCreate(Bundle savedInstanceState) 
     { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_main);   
         for (int i = 0 ; i < 7 ; i++) 
         { 
             views[i] = (TextView)findViewById(names[i]); 
         } 
         //使用Handler进行消息处理  
         final Handler handler = new Handler() 
         { 
             @Override 
             public void handleMessage(Message msg) 
             { 
                 //表明消息来自本程序所发送  
                 if(msg.what == 0x1122) 
                 { 
                     //依次改变7个TextView的背景色  
                     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; 
                 } 
                 //发送一条消息通知系统改变7个TextView组件的背景色  
                 Message m = new Message(); 
                 //给该消息定义一个标识  
                 m.what = 0x1122; 
                 handler.sendMessage(m);  
             }        
         }, 0 , 100); //周期为100毫秒  
     } 
 } 

 使用handler来处理textview背景色的更新,定义一个线程周期性0.1秒执行一次任务,该任务仅仅改变currentcolor变量的值,然后向handler发送一条消息,通知它更新7个textview的背景色。

出现问题:colors数组那个地方在EC里面提示的是color cannot be resolved or is not a field???

colors数组那个地方在EC里面提示的是color cannot be resolved or is not a field???
因为:你缺少颜色的资源文件,在String.xml中添加如下<color.../>子元素即可!!!

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">framlayout</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <color name="color1">#0f0</color>  
    <color name="color2">#00f</color>  
    <color name="color3">#0ff</color>   
    <color name="color4">#f0f</color>  
    <color name="color5">#ff0</color>   
    <color name="color6">#07f</color>   
    <color name="color7">#70f</color>
</resources>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值