50个Android开发技巧(11 为文字加入特效)
(原文地址:http://blog.csdn.net/vector_yi/article/details/24460227)
可是我们还须要解决显示的字体问题,让它看起来更像是一个真实的LED数字时钟。
我们在创建一个Androidproject时,会同一时候生成一个assets目录。用来存放须要利用到的资源。
在这里,我们首先须要在assets目录下放入一个我们须要用到的字体文件:digital-7.ttf
如图所看到的:
然后再编写LedTextView类,并在类中将字体设置为我们引入的字体digital-7.
public class LedTextView extends TextView { private static final String FONTS_FOLDER = "fonts";
private static final String FONT_DIGITAL_7 = FONTS_FOLDER + File.separator
+ "digital-7.ttf";
private void init(Context context) {
AssetManager assets = context. getAssets();
final Typeface font = Typeface . createFromAsset( assets, FONT_DIGITAL_7);
setTypeface (font );// 设置字体
}
public LedTextView (Context context) {
super(context );
init( context);
} public LedTextView (Context context, AttributeSet attrs) {
super(context , attrs );
init( context);
} public LedTextView (Context context, AttributeSet attrs, int defStyle) {
super(context , attrs , defStyle );
init( context);
}
}
<merge xmlns:android ="http://schemas.android.com/apk/res/android" > <com.vectoryi.hack011.view.LedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/default_time"
android:textColor="#3300ff00"
android:textSize="80sp" /> <com.vectoryi.hack011.view.LedTextView
android:id="@+id/main_clock_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#00ff00"
android:textSize="80sp" /> </merge>
试想一下,当你<include
/>这个布局main.xml时,假设main.xml是这种:
<<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <com.vectoryi.hack011.view.LedTextView
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_gravity ="center"
android:text ="@string/default_time"
android:textColor ="#3300ff00"
android:textSize ="80sp" /> <com.vectoryi.hack011.view.LedTextView
android:id ="@+id/main_clock_time"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_gravity ="center"
android:textColor ="#00ff00"
android:textSize ="80sp" /> </FrameLayout>
public class MainActivity extends Activity { private static final String DATE_FORMAT = "%02d:%02d:%02d" ;
private static final int REFRESH_DELAY = 500; // 因为Android不同意其它线程操作UI,所以在此创建了一个Handler
private final Handler mHandler = new Handler();
//用来实时更新LedTextView显示的线程
private final Runnable mTimeRefresher = new Runnable() {
@Override
public void run() {
Calendar calendar = Calendar. getInstance (TimeZone
. getTimeZone( "GMT+8")); // 设置时区
final Date d = new Date();
calendar .setTime (d );
mTextView. setText( String. format (DATE_FORMAT ,
calendar .get (Calendar .HOUR ), calendar. get( Calendar. MINUTE),
calendar .get (Calendar .SECOND )));
mHandler. postDelayed (this , REFRESH_DELAY );
}
}; private TextView mTextView ; @Override
public void onCreate(Bundle savedInstanceState ) {
super .onCreate (savedInstanceState );
setContentView (R .layout .main ); mTextView = (TextView ) findViewById (R .id .main_clock_time );
} @Override
protected void onResume() {
super .onResume ();
mHandler. post( mTimeRefresher );
} @Override
protected void onStop() {
super .onStop ();
mHandler. removeCallbacks (mTimeRefresher );
}
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdmVjdG9yX3lp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
<com.vectoryi.hack011.view.LedTextView
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" />
最后附上GitHub相关资料(https://github.com/VectorYi/LedTextView.git)和源代码下载链接(源代码点我).
50个Android开发技巧(11 为文字加入特效)的更多相关文章
- 50个android开发技巧
50个android开发技巧 http://blog.csdn.net/column/details/androidhacks.html
- 50一个Android开发技巧(01 利用好layout_weight属性)
问题:如何将一个Button放置在布局的中间,并设置其宽度parent的50%? 分析:问题想要达到的效果应该是这样: (原文地址:http://blog.csdn.net/vector_yi/art ...
- 50个Android开发技巧(09 避免用EditText对日期进行验证)
我们都知道,在表单中对数据进行验证不但无聊并且easy出错. (原文地址:http://blog.csdn.net/vector_yi/article/details/24424713) 想象一下,一 ...
- 50个Android开发技巧(02 延迟载入和避免反复渲染视图)
当你在Application中创建复杂的布局时.页面的渲染过程也变得更加缓慢. 此时,我们须要利用 <include />标签(避免反复渲染)和 ViewStub类(延迟载入)来优化我们的 ...
- 50个Android开发技巧(24 处理ListView数据为空的情况)
在移动平台上为用户展示数据的一个经常用法是将数据填充进一个List内,而此时须要注意的一点就是: 原文地址:(http://blog.csdn.net/vector_yi/article/d ...
- 50个Android开发技巧(10 为TextView加入样式)
首先来看一个控件的例子: (原文地址:http://blog.csdn.net/vector_yi/article/details/24428085) 手机上类似这种场景你一定已经见过非常多次了,但有 ...
- 50个Android开发技巧(03 自己定义ViewGroup)
问题:怎样创建一个例如以下图所看到的的布局? 图1 (原文地址:http://blog.csdn.net/vector_yi/article/details/244155 ...
- 50个Android开发技巧(12 为控件加入圆角边框)
控件的圆角边框能够使你的App看起来更美观,事实上实现起来也非常easy. (原文地址:http://blog.csdn.net/vector_yi/article/details/24463025) ...
- 50个Android开发人员必备UI效果源码[转载]
50个Android开发人员必备UI效果源码[转载] http://blog.csdn.net/qq1059458376/article/details/8145497 Android 仿微信之主页面 ...
随机推荐
- Linux和windows下的shutdown命令
Linux下的shutdown命令 shutdown [options] [time] [wall] options: --help 获取帮助 -h 关机 -r 重启 -c 取消 -k 仅显示警告信息 ...
- 物流追踪 - -GPS和GPRS应用
源码1: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<termios.h ...
- 我写的一个 Qt 显示图片的控件
Qt 中没有专门显示图片的控件.通常我们会使用QLabel来显示图片.可是QLabel 显示图片的能力还是有点弱.比方不支持图像的缩放一类的功能.使用起来不是非常方便. 因此我就自己写了个简单的类. ...
- 学习OpenBlas
编译 从OpenBlas Home Page 上下载源代码.make, make install 使用 level 1 向量-向量 操作 #include <iostream> #incl ...
- Spring IOC和DI原理讲解并制作LazyCoder版的Spring (一)
转载请注意出处:http://blog.csdn.net/zcm101 写在前面的话 最近,给项目组成员培训了Spring 控制反转和依赖注入的原理,并自己做了个Lazy Coder版的Spring, ...
- CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD
题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...
- [转]apache的源码安装详细过程全纪录
原文链接:http://www.jb51.net/article/59474.htm 文中 开机启动需要修改 而且特别麻烦 还的配置php 否则不认识php文件 郁闷!只能做参考了!
- a='1,2,3,4,5'如何转换为['1','2','3','4','5']
a='1,2,3,4,5'如何转换为['1','2','3','4','5'] b=a.split(',') split函数自动将分隔后的元素以逗号为分隔符存放到列表中 用处:有时需要用户输入多个数字 ...
- main函数的参数问题 (转载)
void main(int arg ,char *arv[]){} arg -- 命令行参数总个数arv[0] -- 参数1,程序名 arv[1] -- 参数2,字符串 arv[2] -- 参数3,字 ...
- guozhongCrawler的是一个无须配置、便于二次开发
guozhongCrawler的是一个无须配置.便于二次开发的爬虫开源框架,它提供简单灵活的API,只需少量代码即可实现一个爬虫.模块化设计完全 面向业务提供接口,功能覆盖整个爬虫的生命周期(链接提取 ...