Android 从网址设置自定义字体

标签 android fonts typeface

我想从 URL 设置自定义字体,如 HiFont,HiFont 应用程序截图如下!

here screenshot

请有人帮助我。 我想要的字体不是来自 asset/,而是来自 url,例如:http://example.com/fonts/test.ttf .

谢谢。

最佳答案

我编写了一个示例项目:

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>

在 list 文件中添加这些权限:

<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" />

注意:打开互联网来测试该项目:)

祝你好运。

关于Android 从网址设置自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51324591/

相关文章:

安卓 : How to create a custom textview?

android - 使用书法库的某些 Activity 的不同字体

android - Android 2.1 中无法加载外部字体

google-chrome - 我可以在 Chrome 中禁用本地字体吗?

android - 如何在小部件中使用自定义字体?

java - Android - 创建一些框架布局并以编程方式将它们与重力对齐

android - 使用 twitter4j 登录

ubuntu - Ubuntu的Sublime text 3中的Monaco ttf字体给出了难以辨认的矩形

css - 在CSS中设置字体大小时,为什么不将<body>设置为6.25%,使px和em单位相同?

android - 有没有一种简单的方法可以将我的苹果应用程序转换为安卓应用程序?