【问题标题】:Loading typeface dynamically from url or statically from lib从 url 动态加载字体或从 lib 静态加载字体
【发布时间】:2016-11-05 04:07:27
【问题描述】:

我正在运行一个 Android 应用程序,我想动态加载字体并在运行时使用它。我该怎么做?

还有如何在我编写的 SDK 中包含字体,在我编写的应用程序中引用 sdk,并使用 SDK 中包含的字体?

编辑:感谢您对此投 -1 票,无论谁这样做,我都会停止分享知识,这是让我关闭的好方法。

【问题讨论】:

  • 继续分享知识 :) +1
  • 谢谢!我会的:)

标签: android fonts sdk typeface


【解决方案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);

【讨论】:

  • 但这是存储在 SD 卡中吗?
  • SDK 把它留在那里,是的
  • 但我想从那里存储在本地系统中,我想在我的项目中访问
  • 我提供的答案引用了您希望从您自己编写并在应用程序中使用的 SDK 加载字体的选项。
猜你喜欢
  • 2017-06-10
  • 2021-05-21
  • 2018-02-06
  • 2012-10-25
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多