您的位置:首页 > Web前端

Use DIffent Font and Size.

2012-08-05 20:18 232 查看
================================

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)

[b]█the key point is to create a Typeface object. It can be done below : [/b]

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

Now we see how it was created:

FIRST STEP :





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.





SECOND STEP:

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





the simple way to created it are :

** *creatFromFile(String path).

***creatFromFile(File path).

*** 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 :





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 ?





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









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.













内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐