TextView 字体(TypeFace) 交通灯Led效果

15 篇文章 1 订阅
本文来自 Android开发必知的50诀窍 hack11

1.1预期目标

1,学会给 TextView 设置不同字体
2,利用字体实现交通灯效果

如下如下


1.2 TextView 设置字体样式
1.2.1 首先要有样式文件 *.ttf
网友提供的下载地址
本demo用到的四个ttf
1.2.2 把要用到 ttf 放到 工程的 assets 文件目录下
如图

1.2.3给 textview 设置样式 只需一句setTypeface

private void initType() {
        String fontsPath = "fonts" + File.separator;
        AssetManager assets = getAssets();
        typefaces[0] = Typeface.createFromAsset(assets, fontsPath + "digital-7.ttf");
        typefaces[1] = Typeface.createFromAsset(assets, fontsPath + "ltjh.ttf");
        typefaces[2] = Typeface.createFromAsset(assets, fontsPath + "Roboto-Light.ttf");
        typefaces[3] = Typeface.createFromAsset(assets, fontsPath + "Roboto-Thin.ttf");
        tv_one.setTypeface(typefaces[0]);//
        tv_two.setTypeface(typefaces[1]);
        tv_three.setTypeface(typefaces[2]);
        tv_four.setTypeface(typefaces[3]);
    }

1.3 源码
1.3.1左侧效果图源码

public class Hack12Activity extends Activity {

    private static final String DATE_FORMAT = "%02d:%02d:%02d";
    private static final int REFRESH_DELAY = 500;

    private final Handler mHandler = new Handler();
    private final Runnable mTimeRefresher = new Runnable() {
        @Override
        public void run() {
            final Date d = new Date();
            tv_one.setText(String.format(DATE_FORMAT, d.getHours(),
                    d.getMinutes(), d.getSeconds()));
            tv_two.setText(String.format(DATE_FORMAT, d.getHours(),
                    d.getMinutes(), d.getSeconds()));
            tv_three.setText(String.format(DATE_FORMAT, d.getHours(),
                    d.getMinutes(), d.getSeconds()));
            tv_four.setText(String.format(DATE_FORMAT, d.getHours(),
                    d.getMinutes(), d.getSeconds()));
            mHandler.postDelayed(this, REFRESH_DELAY);
        }
    };

    private Typeface[] typefaces = new Typeface[5];
    private TextView tv_one;
    private TextView tv_two;
    private TextView tv_three;
    private TextView tv_four;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hack12);
        initView();
        initType();
    }

    private void initType() {
        String fontsPath = "fonts" + File.separator;
        AssetManager assets = getAssets();
        typefaces[0] = Typeface.createFromAsset(assets, fontsPath + "digital-7.ttf");
        typefaces[1] = Typeface.createFromAsset(assets, fontsPath + "ltjh.ttf");
        typefaces[2] = Typeface.createFromAsset(assets, fontsPath + "Roboto-Light.ttf");
        typefaces[3] = Typeface.createFromAsset(assets, fontsPath + "Roboto-Thin.ttf");
        tv_one.setTypeface(typefaces[0]);
        tv_two.setTypeface(typefaces[1]);
        tv_three.setTypeface(typefaces[2]);
        tv_four.setTypeface(typefaces[3]);
    }

    private void initView() {
        tv_one = (TextView) findViewById(R.id.tv_one);
        tv_two = (TextView) findViewById(R.id.tv_two);
        tv_three = (TextView) findViewById(R.id.tv_three);
        tv_four = (TextView) findViewById(R.id.tv_four);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.post(mTimeRefresher);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mHandler.removeCallbacks(mTimeRefresher);
    }
}

1.3.2交通灯源码

用到的是第一种字体,只需要改变字体颜色,并且把两个 TextView 叠加起来就可以了

//作为背景显示 88:88:88
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/default_time"
        android:textColor="#2200ff00"
        android:textSize="80sp" />
//动态显示时间
      <TextView
        android:id="@+id/main_clock_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:shadowColor="#00ff00"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="10"
        android:textColor="#00ff00"
        android:textSize="80sp" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值