This article illustrates the effect of Android implementing text and picture blending (text wrapping). Share to everyone for your reference, specific as follows:
In peacetime we do projects, may have to a picture or a certain thing for text and picture description, this time the requirements of typesetting beautiful, so there will be text and pictures mixed row situation, as shown:
In this case, the top and bottom two text descriptions are contiguous together, this requires us to calculate how the above text description and the following text is coherent together, which requires us to calculate, the following code, the code also has detailed comments, the principle is very simple.
Because it is relatively simple, it is calculated directly in the activity:
Package com.example.test;
Import android.app.Activity;
Import Android.graphics.Paint;
Import Android.os.Bundle;
Import Android.view.ViewTreeObserver;
Import Android.widget.ImageView;
Import Android.widget.TextView;
public class Mainactivity extends activity {Boolean imagemeasured = false;
TextView Tv_right;
TextView Tv_bottom; Static final String Text = "Yefan: The protagonist of the novel, with the old students in Taishan gathering together by the Kowloon pull coffin away from the earth," + "into the Big Dipper field, learned that they are Argu san Yefan Yefan body. Adventure Forbidden, acquisition of source surgery, fighting the Holy Family, war too paleontology, "+" to restructure the heavens, Yefan and the quartet get many encounters and challenges, the ability to surge, the vision is gradually open. A vast fairy world, "+" in his perspective in front of the reader. Guizi month: Miss Ji Jia, 17 years of age. Yefan hijacked by the experience of bronze ancient palace adventure, "+" rely on the fragmentation of the God light to lift the ban, in turn, holding Yefan together into the Xuan sent to seek secret surgery. "+" in Yefan fled too Xuan Guizi month in the Peacock King Chaos was Hua Yunfei hunted, and Yefan [2] meet, was Yefan escort back ji Jia "+", gradually to yefan produce subtle feelings. After becoming the wife of Yefan, after thousands of stars in the Flying Immortal, in Yefan also into the Fairy road after Goodbye Pambo: "+" leaves where the best friend of the university, strong Shuo burly, forthright loyalty. To reach the Big Dipper after taking the holy fruit by the Spirit Market Cave as a fairy Miao, "+" in the tomb of the Qing emperor for the 19 generation Sun, the body was tempered to four pole realm. After the Yefan and the Black Emperor suppressed the old demon God Knowledge, "+" Pambo regain control of their body, to obtain the Demon Emperor Quiching and the old demon body offering to the Qinglian Magic weapon, acquisition demon Emperor Nine chopped and day demon eight-style, "+" but still disguised as an old demon left in the Demon clan. After the close to find Yefan, many times with his retreat. Star Ancient Road opened after this from the Beidou, "+" by the leaves from the tomb of the Demon Emperor rescued, have to yefan the word, a gasification three clear, and Yefan withTry the ancient road, together to build heaven ";
The height of the screen int screenwidth = 0;
How many words can be put in total int count = 0;
TextView the width of all characters float texttotalwidth = 0.0f;
TextView the width of a word float textWidth = 0.0f;
Paint Paint = new Paint ();
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Tv_right = (TextView) Findviewbyid (r.id.test_tv_right);
Tv_bottom = (TextView) Findviewbyid (R.id.test_tv_bottom);
Final ImageView ImageView = (imageview) Findviewbyid (r.id.test_image);
Imageview.setimageresource (r.drawable.ee);
ScreenWidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth ();
/** * Get the width of a word * * * textWidth = tv_right.gettextsize ();
Paint.settextsize (TextWidth); /** * Because the picture at the beginning, the height is not measured, by adding a listener, you can get its picture height and length/viewtreeobserver vto = Imageview.getviewtreeobserve
R (); Vto.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener (){public Boolean Onpredraw () {if (!imagemeasured) {imagemeasured = true;
int height = imageview.getmeasuredheight ();
int width = imageview.getmeasuredwidth ();
Drawimageviewdone (width, height);
return imagemeasured;
}
}); } private void Drawimageviewdone (int width, int height) {//one line of font height int lineheight = tv_right.getlineheight (
);
How many rows can i put int linecount = (int) math.ceil (double) height/(double) lineheight);
The width of a row is float rowwidth = Screenwidth-width-tv_right.getpaddingleft ()-tv_right.getpaddingright ();
How many words int columnCount = (int) (rowwidth/textwidth) can be placed on a line;
Total number of fonts equals rows * Number of rows per line count = LineCount * COLUMNCOUNT;
The width of all strings in a textview and (font number * Width of each word) Texttotalwidth = (float) ((float) count * textWidth);
Measuretext ();
Tv_right.settext (text.substring (0, Count)); Check to see if the number of rows is greater than the set number of rows, and if greater than one character at a time, recalculate the number of rows and setConsistent while (Tv_right.getlinecount () > LineCount) {count-= 1;
Tv_right.settext (text.substring (0, Count));
} tv_bottom.setpadding (0, LineCount * lineheight-height, 0, 0);
Tv_bottom.settext (Text.substring (count));
/** * Measure the length of the already filled, calculate its remaining length * * private void Measuretext () {String string = text.substring (0, Count);
Float size = Paint.measuretext (string);
int remaincount = (int) ((texttotalwidth-size)/textWidth);
if (Remaincount > 0) {count + = Remaincount;
Measuretext (); }
}
}
Where the XML file is laid out as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <scrollview xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G"
T <relativelayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <ImageVi EW android:id= "@+id/test_image" android:layout_width= "wrap_content" android:layout_height= "Wrap_conte" NT "android:scaletype=" Fitxy "/> <textview android:id=" @+id/test_tv_right "Android:layout_" Width= "Fill_parent" android:layout_height= "wrap_content" android:layout_torightof= "@id/test_image" an
Droid:gravity= "Fill_horizontal" android:paddingleft= "7DP" android:textsize= "16sp"/> <textview Android:id= "@+id/test_tv_bottom" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" "Android:layout_below= "@id/test_image" android:gravity= "Fill_horizontal" android:textsize= "16sp"/> </relativelayout>
; </ScrollView>
The code is very small, the principle is simple, and later found that this approach in most mobile phone operation is perfect, but a small number of mobile phones still have some problems. What is the problem, is that we measure the length of the TextView, because we have just carried settext, and then immediately to the measurement, so that the result is not correct, so we can optimize it. Warm hint, when we settext, can delay some time to measure again, so get the value is to make money, of course that delay time is very short 50 milliseconds, because we have to believe that TextView drawing speed is still very fast.
I hope this article will help you with the Android program.