我正在運行一個Android應用程序,我想動態加載字體並在運行時使用它。我怎樣才能做到這一點?從url動態加載字體或從lib靜態加載字體
而且我該如何在我編寫的SDK中包含字體,在我編寫的應用程序中引用sdk,並使用SDK中包含的字體?
編輯:感謝您對此投了-1票,誰這樣做,我會停止分享知識,這是關閉我的好方法。
我正在運行一個Android應用程序,我想動態加載字體並在運行時使用它。我怎樣才能做到這一點?從url動態加載字體或從lib靜態加載字體
而且我該如何在我編寫的SDK中包含字體,在我編寫的應用程序中引用sdk,並使用SDK中包含的字體?
編輯:感謝您對此投了-1票,誰這樣做,我會停止分享知識,這是關閉我的好方法。
下面是我該怎麼做:(使用AsyncTask,這並不完美) 如果你想要比AsyncTask更穩定的東西RxAndroid提供了其他更好的變體,更穩定。 在這個示例中,我正在做「doInBackground」部分中的所有內容,但您可以在任務完成後的任何位置以相同方式使用它。 這個例子還假設我們有從外部存儲器寫入和讀取的保留。
private class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
public DownloadTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... sUrl) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// download the file
input = connection.getInputStream();
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/fonts");
dir.mkdirs();
File file = new File(dir, "font.ttf");
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=input.read(buf))>0){
out.write(buf,0,len);
}
out.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
File sdcard = Environment.getExternalStorageDirectory();
File dirs = new File(sdcard.getAbsolutePath()+"/fonts");
if(dirs.exists()) {
File[] files = dirs.listFiles();
Log.d("s","files");
}
final Typeface typeface = Typeface.createFromFile(
new File(Environment.getExternalStorageDirectory()+"/fonts", "font.ttf"));
Log.d("a","created");
// Now I'm starting with an example that shows how to use
// this font on a textview of my choice.
// Assumptions: font has characters uF102 and uF104
final TextView tv = (TextView) findViewById(R.id.myTextView);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (tv != null && typeface != null) {
tv.setTypeface(typeface);
tv.setText("\uF102");
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tv.getText().equals("\uF102")){
tv.setText("\uF104");
} else {
tv.setText("\uF102");
}
}
});
}
}
});
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
}
如果我們想從我們使用,從我們已經寫了一個庫,我們可以包括在繪製原料部分的字體的SDK加載字體,並從應用程序使用此SDK/lib下,我們可以參考的字體像這樣: (我用的amaticobold字體在這種情況下,只是舉例)
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/fonts");
dir.mkdirs();
File file = new File(dir, "font.ttf");
InputStream is = getResources().openRawResource(getResources().getIdentifier("amaticbold","raw", getPackageName()));
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=is.read(buf))>0){
out.write(buf,0,len);
}
out.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
File sdcard = Environment.getExternalStorageDirectory();
File dirs = new File(sdcard.getAbsolutePath()+"/fonts");
if(dirs.exists()) {
File[] files = dirs.listFiles();
Log.d("s","files");
}
final Typeface typeface = Typeface.createFromFile(
new File(Environment.getExternalStorageDirectory()+"/fonts", "font.ttf"));
editText.setTypeface(typeface);
請分享知識:) +1 –
謝謝!我會:) – Whitebear