今天一大早,心情不錯,就打算將牌視圖的實現簡單說一下。java
首先,咱們先看下素材是如何的,以下圖所示:
工具
素材一開始是本人通過ps設計出來,而後再切成一個個小件,經過一個叫作TexturePackerGUI的軟件生成的一張整合圖片,配帶着一個說明各小圖片位置大小的文本文件,文本文件內容格式以下:this
編碼
10_b.png:29:370:25:3010_b.png爲圖片名稱,29:370分別是爲表明位置的x,y,25:30爲圖片寬高大小。url
爲此我寫了一個解析這兩個文件的工具類,代碼以下:spa
/** * <p>Title: AssetManager.java</p> * <p>Description: 素材管理,實現了單例</p> * <p>Copyright: Copyright (c) 2007</p> * @author Tunie * @date 2014年9月18日 * @version 1.0 */ public class AssertManager { private List<BufferedImage> images; private Map<String, Asset> asserts; private AssertManager() { images = new ArrayList<BufferedImage>(); asserts = new HashMap<String, AssertManager.Asset>(); } /** * 讀取文件 * @param imagePath 圖片文件路徑 * @param imageDescPath 圖片描述文件路徑 */ public void readFile(String imagePath , String imageDescPath) { BufferedImage image = null; try { URL url = AssertManager.class.getClassLoader().getResource( imagePath); image = ImageIO.read(url); } catch (IOException e) { e.printStackTrace(); System.out.println("讀取圖片出錯"); return; } images.add(image); try { String encoding = "GBK"; URL url = AssertManager.class.getClassLoader().getResource( imageDescPath); File file = new File(url.getPath()); Asset asset = null; if (file.isFile() && file.exists()) { // 判斷文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding);// 考慮到編碼格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { System.out.println(lineTxt); asset = new Asset(lineTxt, image); asserts.put(asset.getName(), asset); } read.close(); } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("讀取文件內容出錯"); e.printStackTrace(); } } public void clear() { asserts.clear(); images.clear(); } public BufferedImage getImage(String name) { Asset asset = asserts.get(name); if (asset != null) return asset.getImage(); return null; } public static void loadFile(String imagePath , String imageDescPath) { getInstance().readFile(imagePath, imageDescPath); } public static BufferedImage getAssert(String name) { return getInstance().getImage(name); } private static AssertManager instance; public static synchronized AssertManager getInstance() { if (instance == null) instance = new AssertManager(); return instance; } /** * <p>Title: Asset</p> * <p>Description:素材,主要爲圖片 </p> * @author Tunie * @date 2014年9月18日 */ private class Asset { private BufferedImage parentImage; private String name; private int x; private int y; private int width; private int height; public Asset(String descInfo , BufferedImage parentImage) { analyse(descInfo); this.parentImage = parentImage; } public BufferedImage getImage() { return parentImage.getSubimage(x, y, width, height); } private void analyse(String descInfo) { String[] descs = descInfo.split(":"); name = descs[0]; x = new Integer(descs[1]); y = new Integer(descs[2]); width = new Integer(descs[3]); height = new Integer(descs[4]); } public String getName() { return name; } } }
實現很簡單,沒什麼值得說明的地方。設計