2007/03/04開始。ソフトウェア管理やDelerer SEの作者であるおにぎりくんの試行錯誤の記録です。
- [Vue.js]時計?タイムコードを表示するWebアプリ
- (2021/11/16)
- ポエム
- (2020/09/30)
- メトロノームアプリをリリース
- (2017/04/22)
- Sikulix勉強中:便利関数
- (2017/02/25)
- [JavaFX]学習メモ
- (2016/09/22)
フォントを読み込むサンプル。 意外と簡単にフォントを読み込んでくれる。
MainActivity.java
package sample.customfont;
import java.io.File;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
/**
* フォントをSDカードから読み込み、TextViewに適用するサンプル。
*
* フォントファイルをファイルパスで指定し、読み込む。
* ファイルが見つからなければ、フォントを適用しない。
* フォントファイルの読み込みは簡単で、ただ
* Typeface.createFromFile(File)
* を使うだけである。
* ファイルが読み込めないときに、例外RuntimeExceptionが投げられる。
* E/AndroidRuntime( 1000): java.lang.RuntimeException:
* Unable to start activity ComponentInfo{sample.customfont/sample.customfont.MainActivity}:
* java.lang.RuntimeException: native typeface cannot be made
* (DDMSを使ってフォントファイルを転送しようとしたが、失敗していたため例外が起きた。
* adb pushを使うと上手くいくのだが。。。)
*
* @author LapisCactus
*
*/
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// load UI widgets
TextView textview = (TextView) findViewById(R.id.main_text);
// load a font and apply it to a TextView
File file = new File("/mnt/sdcard/fonts/KozGoPro-Light.otf");
System.out.println("exists: " + file.exists());
System.out.println("canRead: " + file.canRead());
if (file.exists() && file.canRead()) {
Typeface tf = Typeface.createFromFile(file);
textview.setTypeface(tf);
}
}
}
PR
スポンサード・リンク
この記事にコメントする
フリーソフト指向::開発日記 by LapisCactus
