簡體   English   中英

Libgdx從Gdx.files.external()加載紋理顯示為黑色矩形

[英]Libgdx load texture from Gdx.files.external() showing as black rectangles

我已經添加了外部讀寫權限,並從用戶那里請求了它們的權限。 我已經使用libgdx文件系統生成了我要加載的圖片的文件句柄列表。 我這樣加載它們:

image = new Texture(fh); //fh is the filehandle

當我渲染任何圖像時,我嘗試加載的只是渲染一個黑色矩形。 我像通常那樣使用帶有.draw方法的spritebatch渲染它們。 知道為什么它們呈現為黑色矩形嗎? 提前致謝/

我的圖像模塊代碼:

package com.ggi.uparty.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.ggi.uparty.screens.ImagePicker;

public class ImagePickerModule {

public ImagePicker p;

public FileHandle fh;

public Texture image = null;

public TextureRegion load,preview;


public float theta = 0;

public Rectangle bounds = new Rectangle();

public ImagePickerModule(ImagePicker p, final FileHandle fh){
    theta = 0;
    bounds.width=.85f*Gdx.graphics.getWidth()/6f;
    bounds.height=bounds.width;

    load = new TextureRegion(p.u.assets.get("UI/Load.png", Texture.class));

    Thread t = new Thread(new Runnable(){

        @Override
        public void run() {
            try{
            //image = new Texture(fh);

            image = new Texture(fh);
            System.out.println(fh.path());
            float sqSize = image.getWidth()<image.getHeight()?image.getWidth():image.getHeight();
            preview = new TextureRegion(image,bounds.x+bounds.width/2-sqSize/2,bounds.y+bounds.height/2-sqSize/2,sqSize,sqSize);
            }catch(Exception e){
                e.printStackTrace();
            }

        }

    });
    t.start();
}

public void draw(SpriteBatch pic,float fade){
    theta++;
    pic.setColor(1, 1, 1, fade);
    if(image == null){
        pic.draw(load, bounds.x+bounds.width / 2 - bounds.height / 4, bounds.y + bounds.height / 4+theta, bounds.height / 4,
                bounds.height / 4, bounds.height / 2, bounds.height / 2, 1, 1, -theta);
    }
    else{
        pic.draw(image,bounds.x,bounds.y+theta,bounds.width,bounds.height);
    }
}

}

您正在嘗試在非UI線程上加載紋理。 如果要異步加載紋理,請使用assetmanager。

要對此進行測試,請嘗試將Thread.start更改為thread.run(立即在您現在所在的線程中運行,因此禁用該線程),圖像應該可以正常加載。

然后要獲取異步加載,請實施資產管理器:

https://github.com/libgdx/libgdx/wiki/Managing-your-assets

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM