DanmakuFlameMaster Documentation

repository·master·Indexed 27 days ago

https://github.com/bilibili/danmakuflamemaster

An open-source Android engine for parsing and rendering danmaku (bullet chat) content, supporting Bilibili's XML format. It provides the IDanmakus interface for collection management, IRenderer for custom rendering logic, and tools for tracking rendering statistics via RenderingState.

Tokens
1.7K
Snippets
3
Records
9
Agent score
94%

What's inside DanmakuFlameMaster

  1. Install DanmakuFlameMaster via Gradle

    master

    To include DanmakuFlameMaster in your Android project using Gradle, add the jcenter() repository and the following dependencies. Note that ndkbitmap-armv7a is required, while other ABIs are optional depending on your target architecture.

    repositories {
        jcenter()
    }
    
    dependencies {
        compile 'com.github.ctiao:DanmakuFlameMaster:0.9.25'
        compile 'com.github.ctiao:ndkbitmap-armv7a:0.9.21'
    
        # Other ABIs: optional
        compile 'com.github.ctiao:ndkbitmap-armv5:0.9.21'
        compile 'com.github.ctiao:ndkbitmap-x86:0.9.21'
    }
  2. Manage danmaku collections with IDanmakus

    master

    The IDanmakus interface provides methods for managing collections of BaseDanmaku objects. It supports adding/removing items, slicing collections by time range, and iterating through elements.

    Collection Operations

    • addItem(BaseDanmaku item): Adds an item to the collection.
    • removeItem(BaseDanmaku item): Removes an item from the collection.
    • size(): Returns the number of items.
    • clear(): Removes all items.
    • isEmpty(): Checks if the collection is empty.
    • contains(BaseDanmaku item): Checks if an item exists in the collection.
    • first(): Returns the first danmaku.
    • last(): Returns the last danmaku.
    • getCollection(): Returns the underlying Collection<BaseDanmaku>.

    Slicing and Sub-collections

    • sub(long startTime, long endTime): Returns a sub-view of the danmaku collection within the specified time range.
    • subnew(long startTime, long endTime): Returns a new danmaku collection containing items within the specified time range.

    Iteration

    • forEach(Consumer<? super BaseDanmaku, ?> consumer): Iterates through the collection using the provided consumer.
    • forEachSync(Consumer<? super BaseDanmaku, ?> consumer): Performs a synchronous iteration.

    Configuration

    • setSubItemsDuplicateMergingEnabled(boolean enable): Enables or disables duplicate merging for sub-items.
  3. Sort danmaku using IDanmakus Comparators

    master

    The IDanmakus interface provides several comparator classes for sorting BaseDanmaku objects. All comparators support an optional duplicateMergingEnabled flag via their constructor to treat duplicate danmaku as equal (returning 0 in compare).

    Available Comparators

    • TimeComparator(boolean duplicateMergingEnabled): Sorts danmaku by time.
    • YPosComparator(boolean duplicateMergingEnabled): Sorts danmaku by their vertical position (getTop()) in ascending order.
    • YPosDescComparator(boolean duplicateMergingEnabled): Sorts danmaku by their vertical position (getTop()) in descending order.
  4. Use IDanmakus.Consumer for iteration control

    master

    When using forEach or forEachSync on an IDanmakus instance, you can provide an implementation of IDanmakus.Consumer. The accept method determines the flow of the loop by returning an action constant.

    Action Constants

    • ACTION_CONTINUE (0): Continue the loop.
    • ACTION_BREAK (1): Stop the loop.
    • ACTION_REMOVE (2): Remove the current item and continue.
    • ACTION_REMOVE_AND_BREAK (3): Remove the current item and stop the loop.
  5. Implement the IRenderer interface for custom danmaku rendering

    master

    To implement custom danmaku rendering logic, implement the IRenderer interface. The interface provides methods for drawing danmaku, managing the lifecycle of the renderer, and handling listeners.

    Key methods to implement:

    • draw(IDisplayer disp, IDanmakus danmaku, long startRenderTime, RenderingState renderingState): The core method for rendering a collection of danmakus.
    • clear(): Clears the current rendering state.
    • clearRetainer(): Clears the retainer.
    • release(): Releases resources used by the renderer.

    Rendering types are defined by constants:

    • NOTHING_RENDERING (0)
    • CACHE_RENDERING (1)
    • TEXT_RENDERING (2)
    public interface IRenderer {
        void draw(IDisplayer disp, IDanmakus danmaku, long startRenderTime, RenderingState renderingState);
        void clear();
        void clearRetainer();
        void release();
        // ... other methods
    }
  6. Configure IRenderer settings and listeners

    master

    The IRenderer interface provides several configuration methods to control its behavior and interaction with other components:

    • setCacheManager(ICacheManager cacheManager): Sets the manager used for caching.
    • setVerifierEnabled(boolean enabled): Enables or disables the verifier.
    • setOnDanmakuShownListener(OnDanmakuShownListener onDanmakuShownListener): Sets a listener that triggers onDanmakuShown(BaseDanmaku danmaku) when a danmaku is displayed.
    • removeOnDanmakuShownListener(): Removes the current listener.
    • alignBottom(boolean enable): Enables or disables bottom alignment.
  7. Use Area to manage rendering boundaries

    master

    The Area class defines the rectangular region used for rendering and refresh updates.

    Key methods:

    • set(float left, float top, float right, float bottom): Sets the specific coordinates for the area.
    • setEdge(int maxWidth, int maxHeight): Sets the maximum boundaries for the area.
    • reset(): Resets the area to the maximum width and height.
    • resizeToMax(): Resizes the area to fill the maximum width and height.

    The mRefreshRect field (a float[4]) stores the current boundaries as [left, top, right, bottom].

  8. Use RenderingState to track danmaku statistics

    master

    The RenderingState class is used to track the current state and statistics of the rendering process. It can be used to monitor performance and danmaku distribution.

    Key fields include:

    • totalDanmakuCount: Total number of danmakus.
    • r2lDanmakuCount: Count of Right-to-Left scrolling danmakus.
    • l2rDanmakuCount: Count of Left-to-Right scrolling danmakus.
    • ftDanmakuCount: Count of Fixed Top danmakus.
    • fbDanmakuCount: Count of Fixed Bottom danmakus.
    • specialDanmakuCount: Count of special type danmakus.
    • cacheHitCount / cacheMissCount: Cache performance metrics.
    • isRunningDanmakus: Boolean indicating if danmakus are currently running.

    You can update counts using addCount(int type, int count) where type corresponds to BaseDanmaku constants (e.g., BaseDanmaku.TYPE_SCROLL_RL).