กำลังโหลดแบบอักษรแบบไดนามิกจาก url หรือแบบคงที่จาก lib

ฉันใช้แอปพลิเคชัน Android และฉันต้องการโหลดแบบอักษรแบบไดนามิกและใช้งานระหว่างรันไทม์ ฉันจะทำเช่นนี้ได้อย่างไร?

และฉันจะรวมแบบอักษรใน SDK ที่ฉันเขียน อ้างอิง sdk ในแอปที่ฉันเขียน และใช้แบบอักษรที่รวมอยู่ใน SDK ได้อย่างไร

แก้ไข: ขอบคุณที่โหวต -1 ในเรื่องนี้ ใครก็ตามที่ทำสิ่งนี้ ฉันจะหยุดแบ่งปันความรู้ นั่นเป็นวิธีที่ดีในการปิดฉันลง


person Whitebear    schedule 03.07.2016    source แหล่งที่มา
comment
แบ่งปันความรู้ต่อไป :) +1   -  person Zeeshan Shabbir    schedule 01.08.2017
comment
ขอบคุณ! ฉันจะ :)   -  person Whitebear    schedule 02.08.2017


คำตอบ (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 ที่เราใช้ หรือจากไลบรารีที่เราเขียน เราสามารถรวมฟอนต์ไว้ในส่วน raw ที่วาดได้ และจากแอปพลิเคชันที่ใช้ 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);
person Whitebear    schedule 03.07.2016
comment
แต่นี่เก็บอยู่ในการ์ด SD เหรอ? - person saiRam89; 01.08.2017
comment
SDK จะทิ้งมันไว้ตรงนั้นใช่ - person Whitebear; 02.08.2017
comment
แต่ฉันต้องการที่จะเก็บไว้ในระบบท้องถิ่นจากนั้นฉันต้องการเข้าถึงในโครงการของฉัน - person saiRam89; 02.08.2017
comment
คำตอบที่ฉันให้มาอ้างอิงถึงตัวเลือกที่คุณต้องการโหลดแบบอักษรจาก SDK ที่คุณเขียนเองและใช้ภายในแอปพลิเคชัน - person Whitebear; 02.08.2017