Method for realizing hypertext markup language (HTML) rich text effect based on Android native code

By creating custom HTML parsers and views in the Android system, and directly using native code to achieve HTML rich text effects, solving the problem of low HTML text processing performance in the existing technology, achieving more efficient performance and user experience.

CN119690437APending Publication Date: 2025-03-25XIAMEN CHUNYOU INTERACTIVE TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411850804.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-16
Publication Date
2025-03-25

AI Technical Summary

Technical Problem

When processing HTML text content, existing Android applications need to rely on WebView or other browser components, resulting in high performance consumption and cannot meet the growing demand for mobile applications.

Method used

By creating a custom HTML parser, a list of Android native text components and a custom HTML view, you can directly use native code to achieve HTML rich text effects in the Android system, avoiding dependence on WebView.

Benefits of technology

Improves the display effect and performance of HTML rich text on Android devices, reduces memory consumption and operation delay, and brings a better experience to users.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119690437A_ABST
    Figure CN119690437A_ABST
Patent Text Reader

Abstract

The invention provides a method for realizing an HTML (Hypertext Markup Language) rich text effect based on an Android native code, which comprises the following steps of: analyzing an accepted HTML rich text by a self-defined HTML analyzer to obtain a plurality of Android native text components, and storing the Android native text components in an Android native text component list; the Android native text component list is used as an input of a self-defined HTML view, and all the Android native text components are assembled together and displayed in a screen of an Android system according to the layout and style requirements of HTML (Hypertext Markup Language). According to the method and the device, the HTML rich text effect is realized by using the native code in the Android system, the display effect and the performance of the HTML rich text on the Android equipment are improved, meanwhile, the dependence on WebView is avoided, the memory consumption and the operation delay are reduced, and better experience is brought to a user.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of mobile application development, and in particular to a method for realizing HTML rich text effects based on Android native code. Background Art

[0002] In the current mobile application development environment, HTML text is often used to store and display rich text content. As a markup language that describes the structure of a web page, HTML (HyperText Markup Language) can easily implement a series of rich text effects such as text formatting, color, and image links with its flexible tag system. This makes HTML text interface occupy an important position in the transmission of text information on the network.

[0003] However, when processing HTML text content, existing Android applications have to rely on an embedded WebView or other browser components to parse HTML text and convert it into a visual format. This method consumes too much performance: WebView's own initialization overhead, memory usage, and HTML parsing consumption all affect the overall software performance.

[0004] Another method is to use Android's native TextView to display HTML text, but TextView only supports a subset of HTML and has certain limitations in displaying rich text, which cannot meet the growing needs of mobile applications.

[0005] Typically, developers need to control memory and CPU consumption while maintaining the dynamic rich text display effect of the application, so that the application can maintain good performance in various system environments and hardware devices.

[0006] Therefore, a method for implementing HTML rich text effects using native code in an Android system is of great significance for improving mobile application performance, reducing memory and CPU loads, and improving the user experience of applications. Summary of the invention

[0007] In view of the defects of the prior art, the present invention provides a method for realizing HTML rich text effects based on Android native code, which can effectively solve the above problems.

[0008] The technical solution adopted by the present invention is as follows:

[0009] The present invention provides a method for realizing HTML rich text effect based on Android native code, comprising the following steps:

[0010] Step S1, creating a custom HTML parser, an Android native text component list, and a custom HTML view;

[0011] Step S2, the custom HTML parser has an HTML rich text input interface, which is used to accept HTML rich text through the HTML rich text input interface, and parse the accepted HTML rich text, and parse each HTML tag in the HTML rich text into an Android native text component, thereby obtaining multiple Android native text components;

[0012] Step S3, the custom HTML parser stores the outputted multiple Android native text components into the Android native text component list;

[0013] Step S4, the customized HTML view takes the Android native text component list as input, assembles the various Android native text components together according to the layout and style requirements of HTML, and displays them on the screen of the Android system.

[0014] Preferably, step S2 specifically includes: the custom HTML parser identifies each HTML tag in the HTML rich text, and parses each HTML tag into an Android native text component.

[0015] Preferably, the custom HTML parser identifies each HTML tag in the HTML rich text and parses each HTML tag into an Android native text component, specifically:

[0016] The custom HTML parser is a Java class that uses Java string operation functions to identify characters in the HTML rich text one by one, and recognizes and locates HTML tags by identifying HTML tag start characters and HTML tag end characters; and converts the recognized HTML tags and their positions in the HTML rich text into the Android native text components.

[0017] Preferably, the Android native text component is an Android native rich text element.

[0018] Preferably, the custom HTML view is a custom view inherited from TextView, which accepts the Android native text component list as input, and uses SpannableStringBuilder to assemble the elements in the Android native text component list into a rich text, and displays it on the view, and finally displays it on the screen of the Android system.

[0019] The method for realizing HTML rich text effect based on Android native code provided by the present invention has the following advantages:

[0020] The present invention realizes the use of native code to achieve HTML rich text effects in the Android system, improves the display effect and performance of HTML rich text on Android devices, while avoiding dependence on WebView, reducing memory consumption and operation delays, and bringing a better experience to users. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1 A flowchart of a method for realizing HTML rich text effects based on Android native code provided by the present invention. DETAILED DESCRIPTION

[0022] In order to make the technical problems, technical solutions and beneficial effects solved by the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.

[0023] The present invention proposes a method for realizing HTML rich text effect based on Android native code, which is used for realizing HTML rich text effect using native code under Android system. Firstly, HTML text is parsed into a series of Android native text components through a custom parser, and these native text components can be displayed on Android system in full accordance with HTML format requirements; secondly, the layout, style and interaction of the entire HTML text are completely restored through the management and assembly of these native text components.

[0024] The present invention realizes the use of native code to achieve HTML rich text effects in the Android system, improves the display effect and performance of HTML rich text on Android devices, while avoiding dependence on WebView, reducing memory consumption and operation delays, and bringing a better experience to users.

[0025] See also Figure 1The present invention proposes a method for realizing HTML rich text effect based on Android native code, comprising the following steps:

[0026] Step S1, creating a custom HTML parser, an Android native text component list, and a custom HTML view;

[0027] Step S2, the custom HTML parser has an HTML rich text input interface, which is used to accept HTML rich text through the HTML rich text input interface, and parse the accepted HTML rich text, and parse each HTML tag in the HTML rich text into an Android native text component, thereby obtaining multiple Android native text components;

[0028] Step S2 specifically includes: the custom HTML parser identifies each HTML tag in the HTML rich text, and parses each HTML tag into an Android native text component.

[0029] Furthermore, the custom HTML parser identifies each HTML tag in the HTML rich text and parses each HTML tag into an Android native text component, specifically:

[0030] The custom HTML parser is a Java class that uses Java string operation functions to identify characters in the HTML rich text one by one, and recognizes and locates HTML tags by identifying HTML tag start characters and HTML tag end characters; and converts the recognized HTML tags and their positions in the HTML rich text into the Android native text components.

[0031] In the present invention, the Android native text component is an Android native rich text element.

[0032] Step S3, the custom HTML parser stores the outputted multiple Android native text components into the Android native text component list;

[0033] Step S4, the customized HTML view takes the Android native text component list as input, assembles the various Android native text components together according to the layout and style requirements of HTML, and displays them on the screen of the Android system.

[0034] In this step, the custom HTML view is a custom view inherited from TextView, which accepts the Android native text component list as input, and uses SpannableStringBuilder to assemble the elements in the Android native text component list into a rich text, and displays it on the view, and finally displays it on the screen of the Android system. Among them, TextView is a control used to display text in Android development, inherited from the View class, and is mainly used to display text information in the user interface.

[0035] An embodiment is described below:

[0036] The present invention provides a method for realizing HTML rich text effect based on Android native code, and the main steps include the following:

[0037] First, create a custom HTML parser. A custom HTML parser is a Java class that accepts HTML rich text as input and outputs a list of Android native rich text elements.

[0038] Specifically, the custom HTML parser uses Java's string operation functions to process the characters in HTML rich text one by one. When encountering the HTML tag start character "<", the parser will record the name of the HTML tag and determine whether the HTML tag is a single tag or a double tag. If it is a double tag, the parser will also record the start and end positions of the HTML tag. Then, when encountering the HTML tag end character ">", the parser will generate a corresponding Android native rich text element and add it to the output list.

[0039] For example, when parsed into HTML rich text " Hello ", the parser generates an UnderlineSpan and sets its start and end positions to the positions of Hello.

[0040] When the parser encounters " <strong>" tag, a StyleSpan object is generated and its style is set to Typeface.BOLD. This StyleSpan object is then applied to the <strong> "and"< / strong> " on the HTML rich text between them, making this part of the text appear in bold.

[0041] Similarly, for the <div style='text-align:center'> ... "HTML rich text, the parser will generate an AlignmentSpan object and set its alignment to Layout.Alignment.ALIGN_CENTER. Then, apply this AlignmentSpan object to" "and" " on the HTML rich text between the two to center the text.

[0042] For the inclusion of " Hello ", the parser will generate an UnderlineSpan object and set its start and end positions to the positions of Hello, so that this part of the text is underlined.

[0043] For the inclusion of " <blockquote> ...< / blockquote> ", the parser generates a LeadingMarginSpan object and sets its margin value to simulate the indentation effect of blockquote.

[0044] For style properties implemented via CSS, such as font type, " <div style='font-family:monospace'> Monospaced ", the parser can create a TypefaceSpan object and set its font style, such as "monospace". Similarly, apply this TypefaceSpan object to " "and" " on the HTML rich text between .

[0045] In this process, the parser also needs to handle some special HTML tags, such as " "and" ".for" " tag, the parser will add a LineBreakSpan to the output list. For " " tag, the parser creates a new list and recursively parses the " "Tag for each" " tag generates a BulletSpan, and then adds these BulletSpans to the new list.

[0046] Next, a custom HTML view is created, which is a custom view inherited from TextView. It accepts the Android native rich text element list generated by the HTML parser as input, and uses SpannableStringBuilder to assemble these elements into a rich text and display it on the view.

[0047] In the implementation, some native interfaces and classes provided by Android are also used, such as Html.fromHtml() function and Html.TagHandler interface. These native interfaces and classes can help us parse HTML rich text more conveniently and convert it into Android native rich text elements.

[0048] Finally, perform the above conversion where HTML rich text is needed, and use a custom HTML view to display the converted rich text.

[0049] This method not only achieves the HTML rich text effect using native code in the Android system, but also solves the performance problem caused by using WebView. The implementation method is native and easy to use, developer-friendly, and because it avoids the use of WebView, it reduces memory and CPU consumption, and can bring a better user experience to most Android devices.

[0050] Therefore, the method for implementing HTML rich text effects based on Android native code provided in this embodiment mainly comprises the following steps:

[0051] (1) Create a custom HTML parser to parse HTML rich text. The custom HTML parser accepts HTML rich text as input and outputs the corresponding Android native text component.

[0052] The custom HTML parser can recognize and process various tags in HTML source code. These tags include but are not limited to 、 、 , When receiving HTML rich text input, the custom HTML parser is first called to parse and process the input HTML rich text, and convert the HTML tags in the HTML rich text into Android native rich text elements, such as SpannableString or SpannableStringBuilder.

[0053] (2) Create an Android native text component list to store all Android native text components obtained after parsing.

[0054] (3) Create a custom HTML view to manage and display these native text components. The custom HTML view accepts a list of Android native text components as input, and assembles and displays the elements in the list according to HTML layout and style requirements. Use the custom HTML view where HTML rich text is needed.

[0055] The customized HTML view is used to display the Android native rich text elements in the Android native text component list on the screen of the Android device.

[0056] In practical applications, for example, for a and The traditional way of using WebView or TextView to display HTML rich text with tags may cause differences or delays in display effects. However, using the method of the present invention, these HTML tags can be correctly parsed and processed by a custom parser, and then converted into Android native rich text elements, so that the expected list format and line break effect can be correctly displayed on the device.

[0057] The method for realizing HTML rich text effect based on Android native code provided by the present invention has the following advantages:

[0058] (1) The present invention realizes the use of native code to achieve HTML rich text effects in the Android system, improves the display effect and performance of HTML rich text on Android devices, while avoiding dependence on WebView, reducing memory consumption and operation delays, and bringing a better experience to users.

[0059] (2) The invention provides a new option for mobile developers. By using Android native code to implement HTML rich text effects, developers no longer need to introduce heavyweight WebView or other third-party libraries to process complex HTML rich text, which greatly improves application performance, reduces memory and CPU usage, and thus brings a smoother user experience.

[0060] (3) Since the method of the present invention does not need to rely on WebView or other third-party libraries, it is helpful to reduce the size of the application and save the user's storage space. In addition, since all HTML rich text processing and rendering processes are completed locally, no network request is required, thereby reducing the consumption of data traffic and strengthening the protection of some user privacy information.

[0061] (4) The custom HTML parser of the present invention also provides developers with greater flexibility. Developers can customize and optimize the parsing rules of HTML tags as needed, add support for new HTML tags, or improve the parsing efficiency of commonly used HTML tags.

[0062] In summary, the present invention improves the display effect and performance of HTML rich text on Android devices while respecting the user's storage space, traffic and privacy, bringing a fully improved experience to users.

[0063] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be considered as the scope of protection of the present invention. < / strong>

Claims

1. A method for implementing HTML rich text effects based on Android native code, characterized in that: The following steps are involved: Step S1, creating a custom HTML parser, an Android native text component list, and a custom HTML view; Step S2, the custom HTML parser has an HTML rich text input interface, which is used to accept HTML rich text through the HTML rich text input interface, and parse the accepted HTML rich text, and parse each HTML tag in the HTML rich text into an Android native text component, thereby obtaining multiple Android native text components; Step S3, the custom HTML parser stores the outputted multiple Android native text components into the Android native text component list; Step S4, the customized HTML view takes the Android native text component list as input, assembles the various Android native text components together according to the layout and style requirements of HTML, and displays them on the screen of the Android system.

2. The method for realizing HTML rich text effect based on Android native code according to claim 1, characterized in that: Step S2 specifically includes: the custom HTML parser identifies each HTML tag in the HTML rich text, and parses each HTML tag into an Android native text component.

3. The method for realizing HTML rich text effect based on Android native code according to claim 2, characterized in that: The custom HTML parser identifies each HTML tag in the HTML rich text and parses each HTML tag into an Android native text component, specifically: The custom HTML parser is a Java class that uses Java string operation functions to identify characters in the HTML rich text one by one, and recognizes and locates HTML tags by identifying HTML tag start characters and HTML tag end characters; and converts the recognized HTML tags and their positions in the HTML rich text into the Android native text components.

4. The method for realizing HTML rich text effect based on Android native code according to claim 1, characterized in that: The Android native text component is an Android native rich text element.

5. The method for realizing HTML rich text effect based on Android native code according to claim 1, characterized in that: The custom HTML view is a custom view inherited from TextView, which accepts the Android native text component list as input, and uses SpannableStringBuilder to assemble the elements in the Android native text component list into a rich text, and displays it on the view, and finally displays it on the screen of the Android system.

Citation Information

Cited By

  • Hyper text markup language (html) label processing method, system equipment and storage medium

    CN121117349A