github项目之仿小猪巴士车牌显示
2016-12-15 16:28 阅读(223)

这里写图片描述

小猪巴士车牌验票显示

最近坐车,用的是小猪巴士。看到小猪巴士的车牌验票显示非常的特点,觉得非常有意思,并且实现又简单,就自己将其实现了。

效果图

这里写图片描述

核心代码

我觉得这个显示有自己特点的地方有二个: 
1.背景显示 
开始觉得这个是不是比较难啊,后来发现,这个直接设置背景图片的方式就可以了,非常简单:

XML文件实现:

android:background="@drawable/ticket_intercity_gray_bg"

Java实现:

myTextView01.setBackground(getResources().getDrawable(R.drawable.ticket_intercity_light_bg));

2.实现车牌颜色的闪烁 
这个也是比较简单,我们先将车牌颜色初始化为一种颜色来显示:

canvas.drawText(firstTitle,
        paddingLeft + (contentWidth - mFirstTextWidth) / 2,
        paddingTop + (contentHeight + mFirstTextHeight) / 2,
        mTextPaint);

然后,每隔一段时间来用不同的颜色重纷此车牌,如此简单:

使用了一个handler技术,每次都将此颜色标志位(color_type )改变

private  final Handler myHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case update_color_first:
                    color_type = update_color_first;
                    invalidate();
                    myHandler.sendEmptyMessageDelayed(update_color_second, flash_time);
                    break;
                case update_color_second:
                    color_type = update_color_second;
                    invalidate();
                    myHandler.sendEmptyMessageDelayed(update_color_first, flash_time);
                    break;
                default:
                    break;
            }
        }
    };

再在onDraw方法中,给设置不同的颜色:

if(color_type == update_color_first){
    mTextPaint.setColor(firstColor);
}else if(color_type == update_color_second){
    mTextPaint.setColor(secondColor);
}

参考地址

1.CustomerTextView 
https://github.com/hfreeman2008/CustomerTextView


作者:hfreeman2008