我想从屏幕快照下方的HiFont,HiFont应用程序之类的URL中设置自定义字体!
请帮助我。 我想从字体/字体,而不是从URL,例如http://example.com/fonts/test.ttf来字体字体。
谢谢。
答案 0 :(得分:0)
我写了一个示例项目:
JAVA 代码:
import android.app.*;
import android.os.*;
import android.widget.*;
import android.graphics.*;
import java.io.*;
import android.view.*;
import java.net.*;
import android.util.*;
/*************
CODED BY : SIROS BAGHBAN
DATE : 13/7/2018
*/
public class MainActivity extends Activity
{
private TextView MyText;
private Button MyButt;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyText =(TextView)findViewById(R.id.fonta);
MyButt =(Button)findViewById(R.id.butt);
loadfont();
MyButt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadfont();
}
});
}
// Load font if available on SD
private void loadfont () {
File file = new File("/mnt/sdcard/myfont.ttf");
if(file.exists()){
// Display font from SD
Typeface typeFace = Typeface.createFromFile(
new File(Environment.getExternalStorageDirectory(), "/myfont.ttf"));
MyText.setTypeface(typeFace);
}
else {
download();
}
}
// Download the custom font from the URL
private void download () {
new DownloadFileFromURL().execute("http://webpagepublicity.com/free-fonts/x/Xtrusion%20(BRK).ttf"); // Downlod LINK !
}
// File download process from URL
private class DownloadFileFromURL extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/myfont.ttf");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
// Display the custom font after the File was downloaded !
loadfont();
}
}
}
XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Show me the custom font !"
android:id="@+id/butt"
android:layout_centerInParent="true"/>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:text="Custom FONT !"
android:layout_above="@id/butt"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:id="@+id/fonta"/>
</RelativeLayout>
在清单文件中添加以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
注意:打开Internet来测试项目:)
祝你好运。