android 开发技巧(9)--为文本添加发亮的效果
2016-03-01 15:24
399 查看
把一个界面做成类似于LED灯一样的效果,需要定义一个类,加载特殊字体。为了实现发光效果,需要实现TextView类中的public void setShadowLayer (float radius, float dx, float dy, int color),也可以通过 XML 中的 android:shadowColor, android:shadowDx、
android:shadowDy 和 android:shadowRadius 属 性 使 用 这 种 效 果。修改 android:shadowDx 和 android:shadowDy 属性的值可以改
变阴影与文本之间的偏移。指定 android:shadowRadius 属性可以让
用户产生一种文本更亮的错觉
先将digital-7.ttf字体放在assets下的fonts文件夹下,自定义一个LedTextView继承自TextView
activity_hack11.xml
字体下载
android:shadowDy 和 android:shadowRadius 属 性 使 用 这 种 效 果。修改 android:shadowDx 和 android:shadowDy 属性的值可以改
变阴影与文本之间的偏移。指定 android:shadowRadius 属性可以让
用户产生一种文本更亮的错觉
先将digital-7.ttf字体放在assets下的fonts文件夹下,自定义一个LedTextView继承自TextView
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"; 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); } private void init(Context context) { AssetManager assets = context.getAssets(); final Typeface font = Typeface.createFromAsset(assets, FONT_DIGITAL_7); setTypeface(font); } }
public class Hack11Activity 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(); mTextView.setText(String.format(DATE_FORMAT, d.getHours(), d.getMinutes(), d.getSeconds())); mHandler.postDelayed(this, REFRESH_DELAY); } }; private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hack11); 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); } }
activity_hack11.xml
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" > <boerpower.com.android50hacks.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" /> <boerpower.com.android50hacks.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" /> </merge>
<string name="default_time">88:88:88</string>
字体下载
相关文章推荐
- Android照片墙完整版,完美结合LruCache和DiskLruCache
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- Android学习历程16-TabHost的基本用法
- Android实现文件上传(支持多文件)
- Android splitActionBarWhenNarrow ActionBar背景颜色设置
- Android性能优化典范之Render Performance
- 【安卓笔记】成长第三步,Android IPC简介与Android中的多进程模式
- android 开发技巧(8)--格式化 TextView 的文本
- Android开发之创建桌面快捷方式
- 国内主要Android应用市场包名大全
- android java文件调用.so文件
- android phonegap js <-> java交互原理
- Android APK反编译详解(附图)
- Android性能优化之过渡绘制
- Jpush 在android studio 客户端的配置,接收服务器的推送消息
- 快速掌握 Android Studio 中 Gradle 的使用方法
- android自定义主题样式
- android onNewIntent
- android 自定义ViewGroup实现仿淘宝的商品详情页
- Android自定义控件-仿淘宝ios客户端天猫商品详情界面