Android textview设置ttf字体库本地库和网络库使用的两种方式

在这里插入图片描述

一:从assets中加载
将相关ttf字体库放入assets/fonts文件夹下,注意ttf文件名称不能是中文

// 加载assets中的字体
TextView textView1 = (TextView) findViewById(R.id.textView1);

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/1.ttf");

textView1.setTypeface(typeface);

二:因第一种方式占用的体积很大,编译后的包会很大所以可以动态从服务器中拿到下载地址进行下载至本地后使用
下载工具类

public class EditImageFontUtls {

    /**
     * 获取对应字体
     *
     * @param context
     * @param name    存储本地名称(英文命名)
     * @return url 下载地址
     */
    public static void getTypeface(Activity context, String name, String url, OnDownloadListener onDownloadListener) {
        try {
            boolean isExists = fileIsExists(getFontPath(context) + name + ".ttf");
            if (isExists) {
                if (onDownloadListener != null) {
                    onDownloadListener.onTypeface(Typeface.createFromFile(getFontPath(context) + name + ".ttf"));
                }
            } else {
                downLoadFont(context, name, url, new OnDownloadListener() {
                    @Override
                    public void onTypeface(Typeface typeface) {
                        if (onDownloadListener != null) {
                            onDownloadListener.onTypeface(typeface);
                        }
                    }

                    @Override
                    public void onProgress(double progress) {
                        //下载中
                        if (onDownloadListener != null) {
                            onDownloadListener.onProgress(progress);
                        }
                    }
                });
            }
        } catch (Exception e) {

        }
    }

    //fileName 为文件名称 返回true为存在
    public static boolean fileIsExists(String fileName) {
        try {
            File f = new File(fileName);
            if (f.exists()) {
                Log.i("fileIsExists", "有这个文件");
                return true;
            } else {
                Log.i("fileIsExists", "没有这个文件");
                return false;
            }
        } catch (Exception e) {
            Log.i("fileIsExists", "崩溃");
            return false;
        }
    }

    //获取存取字体的路径
    public static String getFontPath(Context context) {
        return "/data/data/" + context.getPackageName() + "/ttf_files/";
    }

    /**
     * 下载字体库 保存在本地 例如: /data/data/包名/ttf_files/huawencaiyun.ttf
     * @param context
     * @param name 保存到本地文件名称(不能是中文命名)
     * @param url 字体库下载地址
     * @param onDownloadListener
     */
    public static void downLoadFont(Activity context, String name, String url, OnDownloadListener onDownloadListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FileOutputStream outputStream = null;
                try {
                    OkHttpClient client = new OkHttpClient.Builder().build();
                    ;
                    Request request = (new Request.Builder()).url(url).get().build();
                    Call call = client.newCall(request);
                    Response response = call.execute();
                    ResponseBody body = response.body();
                    InputStream inputStream = body.byteStream();
                    long lengh = body.contentLength();
                    String file_name = getFontPath(context) + name + ".ttf";
                    File file = new File(getFontPath(context));
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    outputStream = new FileOutputStream(file_name);
                    int losing = 0;
                    byte[] bytes = new byte[1024];
                    while (true) {
                        int read = inputStream.read(bytes);
                        if (read == -1) {
                            outputStream.flush();
                            inputStream.close();
                            outputStream.close();
                            Log.i("downLoadFont", "下载完成***********");
                            Log.i("downLoadFont", "下载地址" + file_name);
                            context.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (onDownloadListener != null) {
                                        onDownloadListener.onTypeface(Typeface.createFromFile(getFontPath(context) + name + ".ttf"));
                                    }
                                }
                            });
                            return;
                        }

                        outputStream.write(bytes, 0, read);
                        losing += read;
                        double i = (double) losing / (double) lengh;
                        context.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (onDownloadListener != null) {
                                    Log.i("downLoadFont", "下载进度:" + i * (double) 100 + '%');
                                    onDownloadListener.onProgress(i * (double) 100);
                                }
                            }
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

}

需要使用字体的时候点击调用获取字体的方法,会根据情况走本地获取或者下载

 holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditImageFontUtls.getTypeface(context, fontStyle.get(position).ttfName, fontStyle.get(position).url, new OnDownloadListener() {
                    @Override
                    public void onTypeface(Typeface typeface) {
                    //设置字体库
                        holder.tvName.setTypeface(typeface);
                    }

                    @Override
                    public void onProgress(double progress) {
                    //下载进度
                        holder.progressLayout.setProgress((float) progress);
                    }
                });
            }
        });

接口回调

public interface OnDownloadListener {
    void onTypeface(Typeface typeface);//字体样式

    void onProgress(double progress);//下载进度
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值