(待修改)FrameLayout实现霓虹灯效果+handleMessage+TimerTask

本文介绍了一种在Android应用中实现渐变颜色切换的方法。通过帧布局放置多个重叠的TextView,并利用TimerTask周期性地更新这些TextView的背景颜色,从而达到颜色渐变的效果。文章提供了完整的代码示例。

在布局文件中利用帧布局建立7个重叠的宽度递减的TextView
建立颜色渐变的颜色id值的数组
利用TimerTask周期性的更改当前颜色值以及发送消息,通知系统执行改变7个组件的颜色

public class ColorChangeLightActivity extends AppCompatActivity {

    private int currentColor=0;
    //了利用colors.xml中设定好的颜色来建立颜色数组
    private 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
            };
    //建立布局中7个textview的id的数组
    private int[] viewNames=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
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_change_light);
        for(int i=0;i<7;++i)
        {
            views[i]=(TextView)findViewById(viewNames[i]);
        }
        final Handler handler=new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                if(msg.what==0x1122)
                {
                    for(int i=0;i<7-currentColor;++i)
                    {
                        //为什么setBackgroundColor不行
                        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);
            }
        };
        new Timer().schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                currentColor++;
                if(currentColor>=6)
                {
                    currentColor=0;
                }
                Message m=new Message();
                m.what=0x1122;
                handler.sendMessage(m);
            }
        },0,100);
    }
}

这里写图片描述

这里写图片描述

那个handle,Timer还完全不知道是什么==

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值