Use DIffent Font and Size.

================================

Download project link : http://ishare.iask.sina.com.cn/f/33394396.html
included files are:
result_1.png
result_2.png
result_3.png
result_4.png
test.Typefac.apk
test.Typefac.zip( source code )
Use DIffent Font and Size.docx
================================

(1)SIZE : To change the size of TextView ,we just need to find the object and call the method : .setTextSize(int) to do this task .

○But we here we add a function to it : let the size From 12 TO 29 and then From 29 to 12 , and so on .

if( flagSize <30 && flagSizeDEC == 0 )

{

tv.setTextSize(flagSize);

String size =String.valueOf(flagSize);

tv.setText( "Change me if you can ! " +" " + size);

flagSize++ ;

if(flagSize == 30)

{

flagSizeDEC = 1 ;

}

}

if( flagSize >12 && flagSizeDEC == 1 )

{

tv.setTextSize(flagSize);

flagSize-- ;

String size =String.valueOf(flagSize);

tv.setText( "Change me if you can ! " +" " + size);

if(flagSize == 12)

{

flagSizeDEC = 0 ;

}

}

(2)Font : to change the font of text, we need to call the

 method : .setTypeface(tf)

the key point is to create a Typeface object. It can be done below :

tv.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"));

Now we see how it was created:

FIRST STEP :

clip_image002

We can write:

tv.setTypeface(tf);

the question how to create Typeface ?

We goto the TextView reference and find the setTypeface Method ,Click the Typeface Link.

clip_image004

SECOND STEP:

In the Methods of Typeface reference , we see several Methods which return parameter are Typeface.

clip_image006

the simple way to created it are :

  1. ** *creatFromFile(String path).
  2. ***creatFromFile(File path).
  3. *** creatFromAsset(AssetManager, String )

THIRD STEP:

Try to use the first and second methods ,but Failed.

/* Method 1

* BELOW CANNOT RUN.

String path = "/assets/fonts/BOCBI.ttf";

tv.setTypeface(Typeface.createFromFile(path));

*/

/*Method 2

*BELOW CANTOT RUN.

String path = "fonts/BOCBI.ttf";

File file = new File(path);

tv.setTypeface(Typeface.createFromFile(file));

*/

So we choose the third method.

When we goto the referance , we noticed :

clip_image007

The path parameter is in the assets dirctory ,DOES IT MEAN THAT THE PATH IN THE METHORD ABOVE IS IN THE SDCARD?

Fourth STEP:

After upload the font file “BOCIB.ttf” to the sdcard ,and then changed the path to :

“String path = "/mnt/sdcard/BOCBI.ttf";”

the first and second methods can run normally .

/*Method ONE, the font file is upload to "sdcard/BOCBI.ttf" */

String pathA = "/mnt/sdcard/BOCBI.ttf";

tv.setTypeface(Typeface.createFromFile(pathA));

/*Method TWO, upload file to "sdcard/HandmadeTypewriter.ttf " as same.*/

String pathB = "/mnt/sdcard/HandmadeTypewriter.ttf";

File file = new File(pathB);

tv.setTypeface(Typeface.createFromFile(file));

Further Step

Now we have solve the prolem , but how to use the third method ? we take a further step on the THIRD STEP .

We can write below using the third method:

tv.setTypeface(Typeface.createFromAsset( AssetManager mgr , String path ));

when we goto the referance page of AssetManager , it turned out that no method have a return parameter which is AssetManager. So how to get an AssetManager ?

clip_image009

So we have to search the web, the result is using method “getAssets()” of context , we can get an instance of our package.

clip_image010

clip_image012

So we can write it complete as follow :

/*Method THREE, the font fiel is in assets/fonts/tahoma.ttf */

String pathC = "fonts/tahoma.ttf" ;

tv.setTypeface(Typeface.createFromAsset(getAssets(), pathC));

Now we complete this project.

 clip_image014

result_1 result_2 result_4 result_3

原文地址:https://www.cnblogs.com/xilifeng/p/2624151.html