編輯:Android開發實例
在圖片簡單加密解密篇中介紹了通過簡單的異或來對圖片資源進行加密解密。
但是,當運行程序時出現鎖屏,運行其他程序而轉入後台時,將會出現問題。當我們resume時,發現圖片都失效了。
為什麼?
個人理解為原資源不被resume後的程序管理了,這時我們想到在resume中我們重新再加載一遍資源,這的卻是個辦法。
嘗試後確實解決了這個問題,但是新的問題出現了。測試中會出現NullPointerException的異常,但出現概率較小,最後未查到原因。
又重新查看了AssetManager的使用,它有很多AssetLoader,作者也提供了添加自定義AssetLoader的方法setLoader。
參考TextureLoader完成一個TextureExLoader擴展類
- public class TextureExLoader extends
- AsynchronousAssetLoader<TextureEx, TextureExLoader.TextureExParameter> {
- TextureData data;
- TextureEx texture;
- int size = 0;
- String key = "";
- public TextureExLoader(FileHandleResolver resolver) {
- super(resolver);
- }
- @Override
- public void loadAsync(AssetManager manager, String fileName,
- TextureExParameter parameter) {
- Pixmap pixmap = null;
- Format format = null;
- boolean genMipMaps = false;
- texture = null;
- size = parameter.size;
- key = parameter.key;
- FileHandle handle = resolve(fileName);
- pixmap = DecipherPixmap(handle);
- data = new FileTextureExData(handle, pixmap, format, genMipMaps);
- }
- @Override
- public TextureEx loadSync(AssetManager manager, String fileName,
- TextureExParameter parameter) {
- if (texture != null) {
- texture.load(data);
- return texture;
- } else {
- return new TextureEx(data);
- }
- }
- @SuppressWarnings("rawtypes")
- @Override
- public Array<AssetDescriptor> getDependencies(String fileName,
- TextureExParameter parameter) {
- return null;
- }
- public Pixmap DecipherPixmap(FileHandle handle) {
- Pixmap pixmap = null;
- int value = -1;
- byte[] bs = new byte[size];
- InputStream is = handle.read();
- int cx = 0;
- try {
- while ((value = is.read()) != -1) {
- bs[cx] = (byte) (value ^ key.hashCode());
- cx++;
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- is.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- pixmap = new Pixmap(bs, 0, bs.length);
- return pixmap;
- }
- static public class TextureExParameter extends
- AssetLoaderParameters<TextureEx> {
- public int size = 0;
- public String key = "";
- }
- ......
- }
再繼承一個紋理類
- public class TextureEx extends Texture{
- public TextureEx(Pixmap pixmap) {
- super(pixmap);
- }
- public TextureEx (TextureData data) {
- super(data);
- }
- }
大家可以對比看下,和原來的TextureLoader對比,我們修改了loadAsync方法,新增了DecipherPixmap方法,和調整了參數結構。其中DecipherPixmap
通過傳入文件句柄來獲得解密後的圖片Pixmap。
在loadAsync中我們通過FileTextureExData生成紋理數據。FileTextureExData也是一個擴展類。
在prepare方法中修改了pixmap的生成方法,需要解密,而非直接解析文件。
- public class FileTextureExData implements TextureData {
- final FileHandle file;
- int width = 0;
- int height = 0;
- Format format;
- Pixmap pixmap;
- boolean useMipMaps;
- boolean isPrepared = false;
- public FileTextureExData(FileHandle file, Pixmap preloadedPixmap,
- Format format, boolean useMipMaps) {
- this.file = file;
- this.pixmap = preloadedPixmap;
- this.format = format;
- this.useMipMaps = useMipMaps;
- if (pixmap != null) {
- width = pixmap.getWidth();
- height = pixmap.getHeight();
- if (format == null)
- this.format = pixmap.getFormat();
- }
- }
- @Override
- public boolean isPrepared() {
- return isPrepared;
- }
- @Override
- public void prepare() {
- if (isPrepared)
- throw new GdxRuntimeException("Already prepared");
- if (pixmap == null) {
- pixmap = DecipherPixmap(file);
- width = pixmap.getWidth();
- height = pixmap.getHeight();
- if (format == null)
- format = pixmap.getFormat();
- }
- isPrepared = true;
- }
- @Override
- public Pixmap consumePixmap() {
- if (!isPrepared)
- throw new GdxRuntimeException(
- "Call prepare() before calling getPixmap()");
- isPrepared = false;
- Pixmap pixmap = this.pixmap;
- this.pixmap = null;
- return pixmap;
- }
- @Override
- public boolean disposePixmap() {
- return true;
- }
- @Override
- public int getWidth() {
- return width;
- }
- @Override
- public int getHeight() {
- return height;
- }
- @Override
- public Format getFormat() {
- return format;
- }
- @Override
- public boolean useMipMaps() {
- return useMipMaps;
- }
- @Override
- public boolean isManaged() {
- return true;
- }
- public FileHandle getFileHandle() {
- return file;
- }
- @Override
- public TextureDataType getType() {
- return TextureDataType.Pixmap;
- }
- @Override
- public void consumeCompressedData() {
- throw new GdxRuntimeException(
- "This TextureData implementation does not upload data itself");
- }
- }
最後我們來加載想要的資源
- @Override
- public void create() {
- manager = new AssetManager();
- manager.setLoader(TextureEx.class, new TextureExLoader(
- new InternalFileHandleResolver()));
- TextureExParameter parameter1 = new TextureExParameter();
- parameter1.size = 411517;
- parameter1.key = "key";
- manager.load("test.dat", TextureEx.class, parameter1);
- }
上一節的顯示賬單明細 上中,賬單明細的顯示已經基本實現,本文主要整理下代碼,實
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩