帧布局FrameLayout实现霓虹灯效果

首先帧布局大家都知道是一个层层叠加在一起的,所以。我们弄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);
		
		
	}

学的快,忘的更快,所以只好抽空就复习下学过的知识。还是得多写代码,多打几遍就记住了,要不听懂很快就忘了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值