Android Imagespan Center alignment with text in TextView (regardless of whether the TextView set line spacing or not)

Source: Internet
Author: User
Tags translate function

First, explain a class: Paint.fontmetrics, which represents the metric when a font is drawn. Google's Official API documentation describes its fields as follows:

ascent: The distance from the top of the font to the baseline, which is a negative value.
descent: The distance from the bottom of the font to the baseline, which is a positive value. See:The middle line is the baseline, and the distance from the baseline to the line above is ascent, and the distance from the baseline to the line below is descent.
 back to the theme, we want to align the imagespan with the text, just put the imagespan in the middle between the descent line and the ascent line. The implementation is to override the draw method of the Imagespan class. The final implementation method is as follows:
@Override Public voidDraw (@NonNull canvas canvas, charsequence text,intStartintEndfloatx,intTopintYintBottom, @NonNull paint paint) {     //image to drawdrawable B =getdrawable (); //font metrics of text to be replacedPaint.fontmetricsint FM =Paint.getfontmetricsint (); intTransy = (y + fm.descent + y + fm.ascent)/2-B.getbounds (). Bottom/2;    Canvas.save ();    Canvas.translate (x, Transy);    B.draw (canvas); Canvas.restore ();}
Explanation of the next parameter: x, the distance to the left border of the image to be drawn to the left border of the TextView. Y, the baseline coordinate of the text to replace, which is the distance from the baseline to the top border of the TextView. Top, replacing the top position of the row. Bottom, replacing the bottom-most position of the line. Note that the line spacing between the two lines in TextView is the previous row, so here bottom is the bottom position of the row interval. Paint, a brush that contains metric information for the font to be drawn. The meaning of these parameters in the code can not find the description, wrote a demo test out. The top and bottom parameters simply explain that the function is not used. Then explain the code logic:
Getdrawable gets the size of the rectangular box to draw the image of the image,getbounds is to get the parcel;
Y + fm.descent Get the descent line coordinates of the font;
Y + fm.ascent Get the ascent line coordinates of the font;
The sum of the two is divided by 2, which is the coordinate of the two line.
B.getbounds (). Bottom is the height of the image (imagine placing the image at the origin), divided by 2, which is half the height;
The median coordinate minus the image height is half the target position to be drawn at the top of the image;
Finally, the target coordinates are passed to the Canvas.translate function, and the understanding of the function is not a matter of first.
This is basically the case, and finally provides the final solution to the problem, using the custom Imagespan class, just rewrite its draw function, the code is as follows:
 Public classCenteredimagespanextendsImagespan { PublicCenteredimagespan (Context context,Final intdrawableres) {        Super(context, drawableres); } @Override Public voidDraw (@NonNull canvas canvas, charsequence text,intStartintEndfloatx,intTopintYintBottom, @NonNull paint paint) {        //image to drawdrawable B =getdrawable (); //font metrics of text to be replacedPaint.fontmetricsint FM =Paint.getfontmetricsint (); intTransy = (y + fm.descent + y + fm.ascent)/2-b.getbounds (). BOTTOM/2;        Canvas.save ();        Canvas.translate (x, Transy);        B.draw (canvas);    Canvas.restore (); }}

Finally look at:

The text center alignment problem in Android Imagespan and TextView (regardless of whether the TextView set line spacing or not)

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.