• 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>
    
  • 相关阅读:
    Linux系统中pssh系列工具的使用
    Linux软件包管理和磁盘管理实践
    Linux系统自动化安装之pxe实现
    Linux系统SSH服务基于key认证实践
    Linux系统文本处理之awk数组实践
    Linux访问控制列表
    Linux系统中SSH端口转发
    (转)python的range()函数用法
    Python 学习之list和Tuple类型
    List<String> 和 ArrayList<String>的区别
  • 原文地址:https://www.cnblogs.com/Anita9002/p/3976752.html
Copyright © 2020-2023  润新知