I picked this code up from the web and it is public domain. However it is written in what i think is Java or maybe even C coz i am not familiar with either of them.
I have translated the comments from vietnamese by a web translator. As you can see it is pretty well commented.Anyone able to convert to B4A?
I have translated the comments from vietnamese by a web translator. As you can see it is pretty well commented.Anyone able to convert to B4A?
B4X:
. About StaticLayout:
+ Inherited from class android.text.Layout
+ Constructor:
StaticLayout (CharSequence source, TextPaint paint, int width, align Layout.Alignment, spacingmult float, float spacingadd, boolean includepad)
StaticLayout work is arranged according to your text layout has been established. It uses the parameters:
- CharSequence source: your text
- TextPaint paint: specified text color, font, font size, style ... and used to draw text on the canvas (see the Canvas and the method of draw).
- Int width: the width of the pages (the text is, regardless of alignment) that you want.
- Layout.Alignment align:
- Spacingmult float, float spacingadd: line spacing, ...
- Boolean includepad:
+ StaticLayout "write" your words ntn?
StaticLayout use the method draw (Canvas canvas) to render text on a canvas. Thus we can get a Bitmap object as follows:
Bitmap.createBitmap bitmap = (width, height, Bitmap.Config.ARGB_8888);
/ / Create a canvas to paint on
Canvas c = new Canvas (bitmap);
/ / Draw on the canvas
staticlayout.draw (c);
II. Implementation:
1. Preparation:
- As you saw, StaticLayout will draw text on the canvas and we obtained a bitmap DC. For simplicity, m going to use this bitmap ImageView to view.
- The first is to create a new project, named here m BitmapTest.
- The layout of main.xml you edit as follows:
<? Xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android: orientation = "vertical"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
>
<ImageView
android: id = "@ + id / imgview"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: src = "# ffFDF8A6"
> </ ImageView>
</ LinearLayout>
▲ Here, the id is the image ImageView will undertake the tasks display bitmap collection of DC.
▲ Run at this time will give you a yellow screen (ffFDF8A6).
-Fine-tuning a little better for the program:
+ The correct method you onCreate () as follows for the program in full screen:
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
requestWindowFeature (Window.FEATURE_NO_TITLE);
getWindow (). setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView (R.layout.main);
}
-Next, we need a text. You can select any text, but enough to ensure long-fai.
String story = "<noi dung>"
-Declare a few more required parameters:
/ / Width, height: length, wide-screen
int width, height;
/ / Other parameters
int textSize = 20 / / font size
int textColor = 0xffA7573E / / text color
0xffFDF8A6 pageColor = int / / background color
topPadding int = 30, leftPadding = 10 / / based on, based as
/ / Paint to draw text
TextPaint myTextPaint;
-Add the following line of activity onCreate method to find the width, height of the screen:
Display display = ((WindowManager) this.getSystemService (Context.WINDOW_SERVICE)). GetDefaultDisplay ();
width = display.getWidth ();
display.getHeight height = ();
TextPaint-declaration:
+ You can add fonts for more vivid:
/ / Set the font
Tf = Typeface.createFromAsset typeface (this.getAssets (), "fonts / UVNDaLat_R.TTF");
/ / Set the parameters of the font
TextPaint myTextPaint = new ();
myTextPaint.setColor (textColor);
myTextPaint.setTextSize (textSize);
myTextPaint.bgColor = pageColor;
myTextPaint.setAntiAlias (true);
myTextPaint.setTypeface (tf);
2.Tien of
-Here m going to use SL to draw the text on the screen:
/ /
getPageBitmap public Bitmap () {
Bitmap pageContentBitmap;
/ / Bitmap containing the text
Bitmap.createBitmap pageContentBitmap = (width, height, Bitmap.Config.ARGB_8888);
/ / Create a canvas to paint on
Canvas c = new Canvas (pageContentBitmap);
/ / Create text StaticLayout content
Layout = new StaticLayout StaticLayout (story, myTextPaint, width - (leftPadding <<1), Layout.Alignment.ALIGN_NORMAL, 0.5f, 10F, false);
/ / Draw on the canvas
layout.draw (c);
pageContentBitmap return;
}
▲ Here width parameter is passed to the width - (leftPadding <<1) for the purpose of base left for the text (margins writing, kok wrote the entire width of the screen).
-Displaying bitmap on the screen:
declared and set pictures for ImageView:
image = (ImageView) findViewById (R.id.imgview);
image.setImageBitmap (getPageBitmap ());
▲ You'll run the Address text was drawn up. NHG Address drawing position (0.0) of the screen. We need to adjust a little method getPageBitmap ():
/ /
getPageBitmap public Bitmap () {
Bitmap pageBitmap, pageContentBitmap;
/ / Bitmap background
Bitmap.createBitmap pageBitmap = (width, height, Bitmap.Config.ARGB_8888);
/ / Bitmap containing the text
Bitmap.createBitmap pageContentBitmap = (width-(leftPadding <<1), height-topPadding, Bitmap.Config.ARGB_8888);
/ / Create a canvas to paint on
Canvas c = new Canvas (pageContentBitmap);
/ / Create text StaticLayout content
Layout = new StaticLayout StaticLayout (story, myTextPaint, width
- (LeftPadding <<1), Layout.Alignment.ALIGN_NORMAL, 0.5f, 10F, false);
/ / Draw on the canvas
layout.draw (c);
/ / Canvas of background to draw on the image content
C2 = new Canvas Canvas (pageBitmap);
/ / Fill the background
c2.drawColor (pageColor);
/ / Draw image content
c2.drawBitmap (pageContentBitmap, leftPadding <<1, topPadding, myTextPaint);
pageBitmap return;
}
▲ Here, we use a background bitmap, then draw a bitmap that contains text to the location (leftPadding <<1, topPadding). Then return the bitmap to show on screen.
H-Bi, run the program would result in re-aligns the text. However, there are extra lines at the bottom of the screen. This is because SL only works WRAP-TEXT so you get the same results as DC display long text in a TextView.
So how to solve this problem ntn?
If you want to remove the extra text at the bottom line, then your text fai subdivided so that they match the page size. In other words, you know fai Add locations to cut text. Position at the bottom line-the last line of page (gold position).
Luckily, SL gives us two method:
- Public int getLineForVertical (vertical int): Returns the line is located at coordinates (0, vertical)
- Public int getOffsetForHorizontal (int line, float horiz): Returns the index of the text lies in the line and the coordinates (horiz, 0);
-And this is what m did:
/ /
public void splitTextIntoPages () {
/ / The index used to cut text
offsetI int = 0, offsetII = 0;
/ / Create the Static Layout for the entire text
Layout = new StaticLayout StaticLayout (story, myTextPaint, width
- (LeftPadding <<1), Layout.Alignment.ALIGN_NORMAL, 0.5f, 10F, false);
/ / Total lines
layout.getLineCount totalLines = int ();
/ / Number of lines per page
layout.getLineForVertical linePerPage = int (height - (topPadding <<1));
/ / Loop to the end
int i = 0;
do {
/ /
Log.i ("Notice", "Dang divided ...");
/ /
int line = Math.min (linePerPage * (i +1), totalLines-1);
/ / Character position of the page
offsetII = layout.getOffsetForHorizontal (line, width - (leftPadding <<1));
/ / Get substring
String sub = story.substring (offsetI, offsetII);
/ /
offsetI = offsetII;
/ / Add to Favorites
listOfPages.add (sub);
/ /
i + +;
} While (offsetII <story.length ());
}
▲ In each iteration, we will get "gold position" and cut the text based on it. The text is cut (to match the page size) are stored in order to Vector <String> listOfPages.
-Try to display the first page:
+ Method onCreate (): add splitTextIntoPages () in front of the set photos to ImageView.
GetPageBitmap + method (), you edit the report SL:
Layout = new StaticLayout StaticLayout (listOfPages.elementAt (0), myTextPaint, width-(leftPadding <<1), Layout.Alignment.ALIGN_NORMAL, 0.5f, 10F, false);
▲ Run program will see the surplus spent.
-To display the following pages you can do the following:
+ Added variable index page: int index = 0;
+ OnCreate method:
image.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
/ /
if (index> listOfPages.size () -1) {
index = 0;
}
/ / Set image
image.setImageBitmap (getPageBitmap ());
/ /
index + +;
/ /
}
});
GetPageBitmap + method (): fix declaration SL:
Layout = new StaticLayout StaticLayout (listOfPages.elementAt (index), myTextPaint, width
- (LeftPadding <<1), Layout.Alignment.ALIGN_NORMAL, 0.5f, 10F, false);
So every time you click on the image, a new page will be frequently displayed.
Tut to this end. Hope this information helps you.
Download source code: http://www.mediafire.com/?bt9p65ud4nf59wm