本文来自 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" />