Android-TTF font

Source: Internet
Author: User

Android does not support layout XML to directly use TTF and other custom fonts.

The Custom font must be placed in the asset directory and context must be called. the getassets () method is used to obtain the resources of custom fonts. Because Android widgets depend on other processes, fonts cannot be used in widgets. the settypeface method. Therefore, you can use the Custom font in the widget to convert the font to the image output, and then use views. the setimageviewbitmap method can implement custom functions. Refer to the Code:

Static bitmap buildupdate (string time, context ){
Bitmap mybitmap = bitmap. createbitmap (240, 80, bitmap. config. argb_4444 );

Canvas mycanvas = new canvas (mybitmap );
Paint paint = new paint ();
Typeface TF = typeface. createfromasset (context. getassets (), "fonts/clockopia. TTF ");

Paint. setantialias (true );
Paint. setalpha (110); // value range: 0 ~ 255, the smaller the value, the more transparent
Paint. setsubpixeltext (true );
Paint. settypeface (TF );
Paint. setstyle (paint. style. Fill );
Paint. setcolor (color. White );
Paint. settextsize (80 );
Paint. settextalign (align. center );
Mycanvas. drawtext (time, 100, 60, paint );
Return mybitmap;
}

// Call the display time desktop widget in the widget
Remoteviews mviews = new remoteviews (context. getpackagename (), R. layout. Main );
Mviews. setimageviewbitmap (R. Id. imageview1, buildupdate (time, context ));

PS: Because the paint brush and image are used for font, <imageview> is used for layout.

 

PPS:
Widgets only support simple components, such as imageview and textvew. They do not support advanced and complex components, such as listview and edittext. However, widgets can click to trigger an activity that contains any components. The procedure is as follows:

[HTML] the Code must be placed in the onupdate () method for execution.

Remoteviews (context. getpackagename (), R. layout. Main );
Intent intent = new intent (context, called activity. Class );
Pendingintent pend = pendinginent. getactivity (context, 0, intent, 0 );
Mremoteviews. setonclickpendingintent (R. Id. widget, pend );.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.