cocos2d动画工具:-hd.plist生成plist

来源:未知 责任编辑:智问网络 发表时间:2013-09-26 23:53 点击:

直接上代码:


[java] /**
 * Java 正则表达式驱动,用这种,独立于网络~
 * @param input     旧数据读入文件
 * @param output    新数据写出文件
 */ 
public static void divideLogic(File input, File output) { 
    String raw = StringFileBridge.file2String(input, "UTF-8"); 
     
    Pattern pattern = Pattern.compile("\\{\\s*\\d+\\s*,\\s{0,}\\d+\\s{0,}\\}"); 
    Matcher matcher = pattern.matcher(raw); 
     
    StringBuffer sb = new StringBuffer(); 
    while (matcher.find()) { 
        String trimed = matcher.group().replaceAll("[\\s*|\\{|\\}]", ""); 
        String[] strs = trimed.split(","); 
        String s1, s2, handledItem; 
        s1 = String.valueOf(Integer.parseInt(strs[0]) / 2); 
        s2 = String.valueOf(Integer.parseInt(strs[1]) / 2); 
        handledItem = "{" + s1 + "," + s2 + "}"; 
        matcher.appendReplacement(sb, handledItem); 
    } 
    matcher.appendTail(sb); 
    StringFileBridge.string2File(sb.toString(), output); 

 /**
  * Java 正则表达式驱动,用这种,独立于网络~
  * @param input  旧数据读入文件
  * @param output 新数据写出文件
  */
 public static void divideLogic(File input, File output) {
  String raw = StringFileBridge.file2String(input, "UTF-8");
  
  Pattern pattern = Pattern.compile("\\{\\s*\\d+\\s*,\\s{0,}\\d+\\s{0,}\\}");
  Matcher matcher = pattern.matcher(raw);
  
  StringBuffer sb = new StringBuffer();
  while (matcher.find()) {
   String trimed = matcher.group().replaceAll("[\\s*|\\{|\\}]", "");
   String[] strs = trimed.split(",");
   String s1, s2, handledItem;
   s1 = String.valueOf(Integer.parseInt(strs[0]) / 2);
   s2 = String.valueOf(Integer.parseInt(strs[1]) / 2);
   handledItem = "{" + s1 + "," + s2 + "}";
      matcher.appendReplacement(sb, handledItem);
  }
  matcher.appendTail(sb);
  StringFileBridge.string2File(sb.toString(), output);
 }
StringFileBridge.java


[java] package org.bruce.image.handler.divider; 
 
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.LineNumberReader; 
import java.io.Reader; 
import java.io.StringReader; 
import java.io.UnsupportedEncodingException; 
 
/**
 * 字符串与文件相互转换工具
 * @author leizhimin 2009-7-14 15:54:18
 */ 
public class StringFileBridge { 
    /**
     * 读取文件为一个内存字符串,保持文件原有的换行格式
     * @param file 文件对象
     * @param charset 文件字符集编码
     * @return 文件内容的字符串
     */ 
    public static String file2String(File file, String charset) { 
        StringBuffer sb = new StringBuffer(); 
        try { 
            FileInputStream fis = new FileInputStream(file); 
            InputStreamReader isr = new InputStreamReader(fis, charset); 
            BufferedReader br = new BufferedReader(isr); 
            LineNumberReader reader = new LineNumberReader(br); 
             
            String line; 
            while ((line = reader.readLine()) != null) { 
                sb.append(line).append(System.getProperty("line.separator")); 
            } 
            reader.close(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return sb.toString(); 
    } 
     
     
    public static String changeEncode(String unicodeStr, String charset) { 
        String utf8Str = null; 
        try { 
            utf8Str = new String(unicodeStr.getBytes(charset)); 
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
        } 
        return utf8Str; 
    } 
     
     
    /**
     * 将字符串存储为一个文件,当文件不存在时候,自动创建该文件,当文件已存在时候,重写文件的内容,特定情况下,还与操作系统的权限有关。
     * @param text 字符串
     * @param distFile 存储的目标文件
     * @return 当存储正确无误时返回true,否则返回false
     */ 
    public static boolean string2File(String text, File distFile) { 
        if (!distFile.getParentFile().exists()) { 
            distFile.getParentFile().mkdirs(); 
        } 
        BufferedReader br = null; 
        BufferedWriter bw = null; 
        boolean flag = true; 
        try { 
            br = new BufferedReader(new StringReader(text)); 
            bw = new BufferedWriter(new FileWriter(distFile)); 
            char buf[] = new char[1024 * 64]; // 字符缓冲区  
            int len; 
            while ((len = br.read(buf)) != -1) { 
                bw.write(buf, 0, len); 
            } 
            bw.flush(); 
            br.close(); 
            bw.close(); 
        } catch (IOException e) { 
            flag = false; 
            e.printStackTrace(); 
            System.out.println("将字符串写入文件发生异常!"); 
        } 
        return flag; 
    } 
     
    /**
     * 文件转换为字符串
     * @param in        字节流
     * @param charset   文件的字符集
     * @return          文件内容
     */ 
    public static String stream2String(InputStream in, String charset) { 
        StringBuffer sb = new StringBuffer(); 
        try { 
            Reader reader = new InputStreamReader(in, charset); 
            int length = 0; 
            for (char[] c = new char[1024]; (length = reader.read(c)) != -1;) { 
                sb.append(c, 0, length); 
            } 
            reader.close(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return sb.toString(); 
    } 
     
     
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        String x = file2String(new File("/Users/user/Desktop/123.java"), "GBK"); 
        System.out.println(x); 
        boolean b = string2File(x, new File("/Users/user/Desktop/1234.java")); 
        System.out.println(b); 
    } 

package org.bruce.image.handler.divider;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

/**
 * 字符串与文件相互转换工具
 * @author leizhimin 2009-7-14 15:54:18
 */
public class StringFileBridge {
 /**
  * 读取文件为一个内存字符串,保持文件原有的换行格式
  * @param file 文件对象
  * @param charset 文件字符集编码
  * @return 文件内容的字符串
  */
 public static String file2String(File file, String charset) {
  StringBuffer sb = new StringBuffer();
  try {
   FileInputStream fis = new FileInputStream(file);
   InputStreamReader isr = new InputStreamReader(fis, charset);
   BufferedReader br = new BufferedReader(isr);
   LineNumberReader reader = new LineNumberReader(br);
   
   String line;
   while ((line = reader.readLine()) != null) {
    sb.append(line).append(System.getProperty("line.separator"));
   }
   reader.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return sb.toString();
 }
 
 
 public static String changeEncode(String unicodeStr, String charset) {
  String utf8Str = null;
  try {
   utf8Str = new String(unicodeStr.getBytes(charset));
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return utf8Str;
 }
 
 
 /**
  * 将字符串存储为一个文件,当文件不存在时候,自动创建该文件,当文件已存在时候,重写文件的内容,特定情况下,还与操作系统的权限有关。
  * @param text 字符串
  * @param distFile 存储的目标文件
  * @return 当存储正确无误时返回true,否则返回false
  */
 public static boolean string2File(String text, File distFile) {
  if (!distFile.getParentFile().exists()) {
   distFile.getParentFile().mkdirs();
  }
  BufferedReader br = null;
  BufferedWriter bw = null;
  boolean flag = true;
  try {
   br = new BufferedReader(new StringReader(text));
   bw = new BufferedWriter(new FileWriter(distFile));
   char buf[] = new char[1024 * 64]; // 字符缓冲区
   int len;
   while ((len = br.read(buf)) != -1) {
    bw.write(buf, 0, len);
   }
   bw.flush();
   br.close();
   bw.close();
  } catch (IOException e) {
   flag = false;
   e.printStackTrace();
   System.out.println("将字符串写入文件发生异常!");
  }
  return flag;
 }
 
 /**
  * 文件转换为字符串
  * @param in   字节流
  * @param charset  文件的字符集
  * @return    文件内容
  */
 public static String stream2String(InputStream in, String charset) {
  StringBuffer sb = new StringBuffer();
  try {
   Reader reader = new InputStreamReader(in, charset);
   int length = 0;
   for (char[] c = new char[1024]; (length = reader.read(c)) != -1;) {
    sb.append(c, 0, length);
   }
   reader.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return sb.toString();
 }
 
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  String x = file2String(new File("/Users/user/Desktop/123.java"), "GBK");
  System.out.println(x);
  boolean b = string2File(x, new File("/Users/user/Desktop/1234.java"));
  System.out.println(b);
 }
}程序效果:

输入文件:

blurCloud-hd.plist


[html] <?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>frames</key> 
    <dict> 
        <key>blurCloud0.png</key> 
        <dict> 
            <key>frame</key> 
            <string>{{0,8}, {177,92}}</string> 
            <key>offset</key> 
            <string>{0, 0}</string> 
        </dict> 
        <key>blurCloud1.png</key> 
        <dict> 
            <key>frame</key> 
            <string>{{185,13}, {137,80}}</string> 
            <key>offset</key> 
            <string>{0, 0}</string> 
        </dict> 
        <key>blurCloud2.png</key> 
        <dict> 
            <key>frame</key> 
            <string>{{340,1}, {172,112}}</string> 
            <key>offset</key> 
            <string>{0, 0}</string> 
        </dict> 
    </dict> 
    <key>metadata</key> 
    <dict> 
        <key>textureFileName</key> 
        <string>blurCloud-hd.pvr.ccz</string> 
        <key>format</key> 
        <integer>1</integer> 
        <key>size</key> 
        <string>{512, 313}</string> 
    </dict> 
</dict> 
</plist> 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>frames</key>
 <dict>
  <key>blurCloud0.png</key>
  <dict>
   <key>frame</key>
   <string>{{0,8}, {177,92}}</string>
   <key>offset</key>
   <string>{0, 0}</string>
  </dict>
  <key>blurCloud1.png</key>
  <dict>
   <key>frame</key>
   <string>{{185,13}, {137,80}}</string>
   <key>offset</key>
   <string>{0, 0}</string>
  </dict>
  <key>blurCloud2.png</key>
  <dict>
   <key>frame</key>
   <string>{{340,1}, {172,112}}</string>
   <key>offset</key>
   <string>{0, 0}</string>
  </dict>
 </dict>
 <key>metadata</key>
 <dict>
  <key>textureFileName</key>
  <string>blurCloud-hd.pvr.ccz</string>
  <key>format</key>
  <integer>1</integer>
  <key>size</key>
  <string>{512, 313}</string>
 </dict>
</dict>
</plist>

输出文件:

blueCloud.plist

[html] <?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>frames</key> 
    <dict> 
        <key>blurCloud0.png</key> 
        <dict> 
            <key>frame</key> 
            <string>{{0,4}, {88,46}}</string> 
            <key>offset</key> 
            <string>{0,0}</string> 
        </dict> 
        <key>blurCloud1.png</key> 
        <dict> 
            <key>frame</key> 
            <string>{{92,6}, {68,40}}</string> 
            <key>offset</key> 
            <string>{0,0}</string> 
        </dict> 
        <key>blurCloud2.png</key> 
        <dict> 
            <key>frame</key> 
            <string>{{170,0}, {86,56}}</string> 
            <key>offset</key> 
            <string>{0,0}</string> 
        </dict> 
    </dict> 
    <key>metadata</key> 
    <dict> 
        <key>textureFileName</key> 
        <string>blurCloud-hd.pvr.ccz</string> 
        <key>format</key> 
        <integer>1</integer> 
        <key>size</key> 
        <string>{256,156}</string> 
    </dict> 
</dict> 
</plist> 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>frames</key>
 <dict>
  <key>blurCloud0.png</key>
  <dict>
   <key>frame</key>
   <string>{{0,4}, {88,46}}</string>
   <key>offset</key>
   <string>{0,0}</string>
  </dict>
  <key>blurCloud1.png</key>
  <dict>
   <key>frame</key>
   <string>{{92,6}, {68,40}}</string>
   <key>offset</key>
   <string>{0,0}</string>
  </dict>
  <key>blurCloud2.png</key>
  <dict>
   <key>frame</key>
   <string>{{170,0}, {86,56}}</string>
   <key>offset</key>
   <string>{0,0}</string>
  </dict>
 </dict>
 <key>metadata</key>
 <dict>
  <key>textureFileName</key>
  <string>blurCloud-hd.pvr.ccz</string>
  <key>format</key>
  <integer>1</integer>
  <key>size</key>
  <string>{256,156}</string>
 </dict>
</dict>
</plist>

 

 

摘自 yang3wei的专栏

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Lexical or Preprocessor Issue 'xxx.h
    • ios学习笔记(二)xcode 4.3.2下实现基本交互
    • ios版本的helloworld
    • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
    • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
    • IOS类似iphone通讯录TableView的完整demo【附源码】
    • UITableView一些方法
    • [iPhone中级]iPhone团购信息客户端的开发 (二)
    • iphone(object-c)内存管理(1)
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1