小编mal*_*c19的帖子

如何从 Android 库 (AAR) 加载资产(文本文件)?

我在 src/main/assets 文件夹中创建了一个带有一些文本文件的 Android 库 (AAR)。文本文件仅在库中使用,而不在使用该库的应用程序中使用,后者位于同一个 Android Studio 项目中。访问这些文本文件应该很简单,但我做不到。

每个文本文件包含许多行,这些行将被解析为一个列表,该列表仅供库使用。使用一个Context对象,应该很容易;但是我尝试在静态上下文中加载文件,但我无权访问任何Context对象。这是我尝试过的一些代码:

static {

    InputStream is = null;
    try {

        is = Resources.getSystem().getAssets().open("assets/stopwords.txt");

        Request.stopwords = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String stopword;

        while ((stopword = reader.readLine()) != null) {
            Request.stopwords.add(stopword);
        }
    }
    catch (IOException e) {

        logger.error("Could not open input stream");
        logger.error("Exception: {}", e.getMessage());
    }
    catch (Exception e) {

        logger.error("Input stream opened but something else happened");
        logger.error("Exception: {}", e.getMessage());
    }
    finally {

        try {
            is.close(); …
Run Code Online (Sandbox Code Playgroud)

java android assets gradle aar

5
推荐指数
0
解决办法
932
查看次数

使用自定义碎片着色器(Cocos2d-x)进行奇怪的y位置偏移

我正试图掩盖一个精灵,所以我写了一个简单的片段着色器,它只渲染未隐藏在另一个纹理(掩码)下的像素.问题是,在通过着色器后,我的纹理似乎有y坐标偏移.

这是我要屏蔽的精灵(GroundZone)的init方法:

bool GroundZone::initWithSize(Size size) {

    // [...]

    // Setup the mask of the sprite
    m_mask = RenderTexture::create(textureWidth, textureHeight);
    m_mask->retain();
    m_mask->setKeepMatrix(true);


    Texture2D *maskTexture = m_mask->getSprite()->getTexture();
    maskTexture->setAliasTexParameters(); // Disable linear interpolation on the mask

    // Load the custom frag shader with a default vert shader as the sprite’s program
    FileUtils *fileUtils = FileUtils::getInstance();
    string vertexSource = ccPositionTextureA8Color_vert;
    string fragmentSource = fileUtils->getStringFromFile(
                            fileUtils->fullPathForFilename("CustomShader_AlphaMask_frag.fsh"));


    GLProgram *shader = new GLProgram;
    shader->initWithByteArrays(vertexSource.c_str(), fragmentSource.c_str());

    shader->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
    shader->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
    shader->link();
    CHECK_GL_ERROR_DEBUG();
    shader->updateUniforms();
    CHECK_GL_ERROR_DEBUG();

    int maskTexUniformLoc = shader->getUniformLocationForName("u_alphaMaskTexture"); …
Run Code Online (Sandbox Code Playgroud)

c++ opengl-es cocos2d-x cocos2d-x-3.0

2
推荐指数
1
解决办法
1477
查看次数

标签 统计

aar ×1

android ×1

assets ×1

c++ ×1

cocos2d-x ×1

cocos2d-x-3.0 ×1

gradle ×1

java ×1

opengl-es ×1